handle_call.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 " << 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 " << 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::IntMakeType32:
  89. case SemIR::BuiltinFunctionKind::IntMakeTypeSigned:
  90. case SemIR::BuiltinFunctionKind::IntMakeTypeUnsigned:
  91. context.SetLocal(inst_id, context.GetTypeAsValue());
  92. return;
  93. case SemIR::BuiltinFunctionKind::IntSNegate: {
  94. // Lower `-x` as `0 - x`.
  95. auto* operand = context.GetValue(arg_ids[0]);
  96. context.SetLocal(
  97. inst_id,
  98. context.builder().CreateSub(
  99. llvm::ConstantInt::getNullValue(operand->getType()), operand, "",
  100. /*HasNUW=*/false,
  101. /*HasNSW=*/SignedOverflowIsUB));
  102. return;
  103. }
  104. case SemIR::BuiltinFunctionKind::IntUNegate: {
  105. // Lower `-x` as `0 - x`.
  106. auto* operand = context.GetValue(arg_ids[0]);
  107. context.SetLocal(
  108. inst_id,
  109. context.builder().CreateSub(
  110. llvm::ConstantInt::getNullValue(operand->getType()), operand));
  111. return;
  112. }
  113. case SemIR::BuiltinFunctionKind::IntComplement: {
  114. // Lower `^x` as `-1 ^ x`.
  115. auto* operand = context.GetValue(arg_ids[0]);
  116. context.SetLocal(
  117. inst_id,
  118. context.builder().CreateXor(
  119. llvm::ConstantInt::getSigned(operand->getType(), -1), operand));
  120. return;
  121. }
  122. case SemIR::BuiltinFunctionKind::IntSAdd: {
  123. context.SetLocal(
  124. inst_id, context.builder().CreateAdd(context.GetValue(arg_ids[0]),
  125. context.GetValue(arg_ids[1]), "",
  126. /*HasNUW=*/false,
  127. /*HasNSW=*/SignedOverflowIsUB));
  128. return;
  129. }
  130. case SemIR::BuiltinFunctionKind::IntSSub: {
  131. context.SetLocal(
  132. inst_id, context.builder().CreateSub(context.GetValue(arg_ids[0]),
  133. context.GetValue(arg_ids[1]), "",
  134. /*HasNUW=*/false,
  135. /*HasNSW=*/SignedOverflowIsUB));
  136. return;
  137. }
  138. case SemIR::BuiltinFunctionKind::IntSMul: {
  139. context.SetLocal(
  140. inst_id, context.builder().CreateMul(context.GetValue(arg_ids[0]),
  141. context.GetValue(arg_ids[1]), "",
  142. /*HasNUW=*/false,
  143. /*HasNSW=*/SignedOverflowIsUB));
  144. return;
  145. }
  146. case SemIR::BuiltinFunctionKind::IntSDiv: {
  147. context.SetLocal(
  148. inst_id, context.builder().CreateSDiv(context.GetValue(arg_ids[0]),
  149. context.GetValue(arg_ids[1])));
  150. return;
  151. }
  152. case SemIR::BuiltinFunctionKind::IntSMod: {
  153. context.SetLocal(
  154. inst_id, context.builder().CreateSRem(context.GetValue(arg_ids[0]),
  155. context.GetValue(arg_ids[1])));
  156. return;
  157. }
  158. case SemIR::BuiltinFunctionKind::IntUAdd: {
  159. context.SetLocal(
  160. inst_id, context.builder().CreateAdd(context.GetValue(arg_ids[0]),
  161. context.GetValue(arg_ids[1])));
  162. return;
  163. }
  164. case SemIR::BuiltinFunctionKind::IntUSub: {
  165. context.SetLocal(
  166. inst_id, context.builder().CreateSub(context.GetValue(arg_ids[0]),
  167. context.GetValue(arg_ids[1])));
  168. return;
  169. }
  170. case SemIR::BuiltinFunctionKind::IntUMul: {
  171. context.SetLocal(
  172. inst_id, context.builder().CreateMul(context.GetValue(arg_ids[0]),
  173. context.GetValue(arg_ids[1])));
  174. return;
  175. }
  176. case SemIR::BuiltinFunctionKind::IntUDiv: {
  177. context.SetLocal(
  178. inst_id, context.builder().CreateUDiv(context.GetValue(arg_ids[0]),
  179. context.GetValue(arg_ids[1])));
  180. return;
  181. }
  182. case SemIR::BuiltinFunctionKind::IntUMod: {
  183. context.SetLocal(
  184. inst_id, context.builder().CreateURem(context.GetValue(arg_ids[0]),
  185. context.GetValue(arg_ids[1])));
  186. return;
  187. }
  188. case SemIR::BuiltinFunctionKind::IntAnd: {
  189. context.SetLocal(
  190. inst_id, context.builder().CreateAnd(context.GetValue(arg_ids[0]),
  191. context.GetValue(arg_ids[1])));
  192. return;
  193. }
  194. case SemIR::BuiltinFunctionKind::IntOr: {
  195. context.SetLocal(
  196. inst_id, context.builder().CreateOr(context.GetValue(arg_ids[0]),
  197. context.GetValue(arg_ids[1])));
  198. return;
  199. }
  200. case SemIR::BuiltinFunctionKind::IntXor: {
  201. context.SetLocal(
  202. inst_id, context.builder().CreateXor(context.GetValue(arg_ids[0]),
  203. context.GetValue(arg_ids[1])));
  204. return;
  205. }
  206. case SemIR::BuiltinFunctionKind::IntLeftShift: {
  207. context.SetLocal(
  208. inst_id, context.builder().CreateShl(context.GetValue(arg_ids[0]),
  209. context.GetValue(arg_ids[1])));
  210. return;
  211. }
  212. case SemIR::BuiltinFunctionKind::IntRightShift: {
  213. context.SetLocal(
  214. inst_id,
  215. IsSignedInt(context, inst_id)
  216. ? context.builder().CreateAShr(context.GetValue(arg_ids[0]),
  217. context.GetValue(arg_ids[1]))
  218. : context.builder().CreateLShr(context.GetValue(arg_ids[0]),
  219. context.GetValue(arg_ids[1])));
  220. return;
  221. }
  222. case SemIR::BuiltinFunctionKind::IntEq:
  223. case SemIR::BuiltinFunctionKind::IntNeq:
  224. case SemIR::BuiltinFunctionKind::IntLess:
  225. case SemIR::BuiltinFunctionKind::IntLessEq:
  226. case SemIR::BuiltinFunctionKind::IntGreater:
  227. case SemIR::BuiltinFunctionKind::IntGreaterEq: {
  228. context.SetLocal(
  229. inst_id,
  230. context.builder().CreateICmp(
  231. GetBuiltinICmpPredicate(builtin_kind,
  232. IsSignedInt(context, arg_ids[0])),
  233. context.GetValue(arg_ids[0]), context.GetValue(arg_ids[1])));
  234. return;
  235. }
  236. case SemIR::BuiltinFunctionKind::FloatNegate: {
  237. context.SetLocal(
  238. inst_id, context.builder().CreateFNeg(context.GetValue(arg_ids[0])));
  239. return;
  240. }
  241. case SemIR::BuiltinFunctionKind::FloatAdd: {
  242. context.SetLocal(
  243. inst_id, context.builder().CreateFAdd(context.GetValue(arg_ids[0]),
  244. context.GetValue(arg_ids[1])));
  245. return;
  246. }
  247. case SemIR::BuiltinFunctionKind::FloatSub: {
  248. context.SetLocal(
  249. inst_id, context.builder().CreateFSub(context.GetValue(arg_ids[0]),
  250. context.GetValue(arg_ids[1])));
  251. return;
  252. }
  253. case SemIR::BuiltinFunctionKind::FloatMul: {
  254. context.SetLocal(
  255. inst_id, context.builder().CreateFMul(context.GetValue(arg_ids[0]),
  256. context.GetValue(arg_ids[1])));
  257. return;
  258. }
  259. case SemIR::BuiltinFunctionKind::FloatDiv: {
  260. context.SetLocal(
  261. inst_id, context.builder().CreateFDiv(context.GetValue(arg_ids[0]),
  262. context.GetValue(arg_ids[1])));
  263. return;
  264. }
  265. case SemIR::BuiltinFunctionKind::FloatEq:
  266. case SemIR::BuiltinFunctionKind::FloatNeq:
  267. case SemIR::BuiltinFunctionKind::FloatLess:
  268. case SemIR::BuiltinFunctionKind::FloatLessEq:
  269. case SemIR::BuiltinFunctionKind::FloatGreater:
  270. case SemIR::BuiltinFunctionKind::FloatGreaterEq: {
  271. context.SetLocal(inst_id, context.builder().CreateFCmp(
  272. GetBuiltinFCmpPredicate(builtin_kind),
  273. context.GetValue(arg_ids[0]),
  274. context.GetValue(arg_ids[1])));
  275. return;
  276. }
  277. }
  278. CARBON_FATAL() << "Unsupported builtin call.";
  279. }
  280. auto HandleCall(FunctionContext& context, SemIR::InstId inst_id,
  281. SemIR::Call inst) -> void {
  282. llvm::ArrayRef<SemIR::InstId> arg_ids =
  283. context.sem_ir().inst_blocks().Get(inst.args_id);
  284. auto callee_function =
  285. SemIR::GetCalleeFunction(context.sem_ir(), inst.callee_id);
  286. CARBON_CHECK(callee_function.function_id.is_valid());
  287. if (auto builtin_kind = context.sem_ir()
  288. .functions()
  289. .Get(callee_function.function_id)
  290. .builtin_kind;
  291. builtin_kind != SemIR::BuiltinFunctionKind::None) {
  292. HandleBuiltinCall(context, inst_id, builtin_kind, arg_ids);
  293. return;
  294. }
  295. auto* callee = context.GetFunction(callee_function.function_id);
  296. std::vector<llvm::Value*> args;
  297. if (SemIR::GetInitRepr(context.sem_ir(), inst.type_id).has_return_slot()) {
  298. args.push_back(context.GetValue(arg_ids.back()));
  299. arg_ids = arg_ids.drop_back();
  300. }
  301. for (auto arg_id : arg_ids) {
  302. auto arg_type_id = context.sem_ir().insts().Get(arg_id).type_id();
  303. if (SemIR::GetValueRepr(context.sem_ir(), arg_type_id).kind !=
  304. SemIR::ValueRepr::None) {
  305. args.push_back(context.GetValue(arg_id));
  306. }
  307. }
  308. context.SetLocal(inst_id, context.builder().CreateCall(callee, args));
  309. }
  310. } // namespace Carbon::Lower