BuildLibCalls.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements some functions that will create standard C libcalls.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Utils/BuildLibCalls.h"
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/IR/Constants.h"
  16. #include "llvm/IR/DataLayout.h"
  17. #include "llvm/IR/Function.h"
  18. #include "llvm/IR/IRBuilder.h"
  19. #include "llvm/IR/Intrinsics.h"
  20. #include "llvm/IR/LLVMContext.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/IR/Type.h"
  23. #include "llvm/Analysis/TargetLibraryInfo.h"
  24. using namespace llvm;
  25. /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
  26. Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
  27. unsigned AS = V->getType()->getPointerAddressSpace();
  28. return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
  29. }
  30. /// EmitStrLen - Emit a call to the strlen function to the builder, for the
  31. /// specified pointer. This always returns an integer value of size intptr_t.
  32. Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
  33. const TargetLibraryInfo *TLI) {
  34. if (!TLI->has(LibFunc::strlen))
  35. return nullptr;
  36. Module *M = B.GetInsertBlock()->getParent()->getParent();
  37. AttributeSet AS[2];
  38. AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
  39. Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
  40. AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
  41. LLVMContext &Context = B.GetInsertBlock()->getContext();
  42. Constant *StrLen = M->getOrInsertFunction(
  43. "strlen", AttributeSet::get(M->getContext(), AS),
  44. DL.getIntPtrType(Context), B.getInt8PtrTy(), nullptr);
  45. CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
  46. if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
  47. CI->setCallingConv(F->getCallingConv());
  48. return CI;
  49. }
  50. /// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
  51. /// specified pointer. Ptr is required to be some pointer type, MaxLen must
  52. /// be of size_t type, and the return value has 'intptr_t' type.
  53. Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
  54. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  55. if (!TLI->has(LibFunc::strnlen))
  56. return nullptr;
  57. Module *M = B.GetInsertBlock()->getParent()->getParent();
  58. AttributeSet AS[2];
  59. AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
  60. Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
  61. AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
  62. LLVMContext &Context = B.GetInsertBlock()->getContext();
  63. Constant *StrNLen =
  64. M->getOrInsertFunction("strnlen", AttributeSet::get(M->getContext(), AS),
  65. DL.getIntPtrType(Context), B.getInt8PtrTy(),
  66. DL.getIntPtrType(Context), nullptr);
  67. CallInst *CI = B.CreateCall(StrNLen, {CastToCStr(Ptr, B), MaxLen}, "strnlen");
  68. if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
  69. CI->setCallingConv(F->getCallingConv());
  70. return CI;
  71. }
  72. /// EmitStrChr - Emit a call to the strchr function to the builder, for the
  73. /// specified pointer and character. Ptr is required to be some pointer type,
  74. /// and the return value has 'i8*' type.
  75. Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
  76. const TargetLibraryInfo *TLI) {
  77. if (!TLI->has(LibFunc::strchr))
  78. return nullptr;
  79. Module *M = B.GetInsertBlock()->getParent()->getParent();
  80. Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
  81. AttributeSet AS =
  82. AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
  83. Type *I8Ptr = B.getInt8PtrTy();
  84. Type *I32Ty = B.getInt32Ty();
  85. Constant *StrChr = M->getOrInsertFunction("strchr",
  86. AttributeSet::get(M->getContext(),
  87. AS),
  88. I8Ptr, I8Ptr, I32Ty, nullptr);
  89. CallInst *CI = B.CreateCall(
  90. StrChr, {CastToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr");
  91. if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
  92. CI->setCallingConv(F->getCallingConv());
  93. return CI;
  94. }
  95. /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
  96. Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
  97. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  98. if (!TLI->has(LibFunc::strncmp))
  99. return nullptr;
  100. Module *M = B.GetInsertBlock()->getParent()->getParent();
  101. AttributeSet AS[3];
  102. AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
  103. AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
  104. Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
  105. AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
  106. LLVMContext &Context = B.GetInsertBlock()->getContext();
  107. Value *StrNCmp = M->getOrInsertFunction(
  108. "strncmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
  109. B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
  110. CallInst *CI = B.CreateCall(
  111. StrNCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "strncmp");
  112. if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
  113. CI->setCallingConv(F->getCallingConv());
  114. return CI;
  115. }
  116. /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
  117. /// specified pointer arguments.
  118. Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
  119. const TargetLibraryInfo *TLI, StringRef Name) {
  120. if (!TLI->has(LibFunc::strcpy))
  121. return nullptr;
  122. Module *M = B.GetInsertBlock()->getParent()->getParent();
  123. AttributeSet AS[2];
  124. AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
  125. AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
  126. Attribute::NoUnwind);
  127. Type *I8Ptr = B.getInt8PtrTy();
  128. Value *StrCpy = M->getOrInsertFunction(Name,
  129. AttributeSet::get(M->getContext(), AS),
  130. I8Ptr, I8Ptr, I8Ptr, nullptr);
  131. CallInst *CI =
  132. B.CreateCall(StrCpy, {CastToCStr(Dst, B), CastToCStr(Src, B)}, Name);
  133. if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
  134. CI->setCallingConv(F->getCallingConv());
  135. return CI;
  136. }
  137. /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
  138. /// specified pointer arguments.
  139. Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
  140. const TargetLibraryInfo *TLI, StringRef Name) {
  141. if (!TLI->has(LibFunc::strncpy))
  142. return nullptr;
  143. Module *M = B.GetInsertBlock()->getParent()->getParent();
  144. AttributeSet AS[2];
  145. AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
  146. AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
  147. Attribute::NoUnwind);
  148. Type *I8Ptr = B.getInt8PtrTy();
  149. Value *StrNCpy = M->getOrInsertFunction(Name,
  150. AttributeSet::get(M->getContext(),
  151. AS),
  152. I8Ptr, I8Ptr, I8Ptr,
  153. Len->getType(), nullptr);
  154. CallInst *CI = B.CreateCall(
  155. StrNCpy, {CastToCStr(Dst, B), CastToCStr(Src, B), Len}, "strncpy");
  156. if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
  157. CI->setCallingConv(F->getCallingConv());
  158. return CI;
  159. }
  160. /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
  161. /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
  162. /// are pointers.
  163. Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
  164. IRBuilder<> &B, const DataLayout &DL,
  165. const TargetLibraryInfo *TLI) {
  166. if (!TLI->has(LibFunc::memcpy_chk))
  167. return nullptr;
  168. Module *M = B.GetInsertBlock()->getParent()->getParent();
  169. AttributeSet AS;
  170. AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
  171. Attribute::NoUnwind);
  172. LLVMContext &Context = B.GetInsertBlock()->getContext();
  173. Value *MemCpy = M->getOrInsertFunction(
  174. "__memcpy_chk", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
  175. B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
  176. DL.getIntPtrType(Context), nullptr);
  177. Dst = CastToCStr(Dst, B);
  178. Src = CastToCStr(Src, B);
  179. CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
  180. if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
  181. CI->setCallingConv(F->getCallingConv());
  182. return CI;
  183. }
  184. /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
  185. /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
  186. Value *llvm::EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
  187. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  188. if (!TLI->has(LibFunc::memchr))
  189. return nullptr;
  190. Module *M = B.GetInsertBlock()->getParent()->getParent();
  191. AttributeSet AS;
  192. Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
  193. AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
  194. LLVMContext &Context = B.GetInsertBlock()->getContext();
  195. Value *MemChr = M->getOrInsertFunction(
  196. "memchr", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
  197. B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context), nullptr);
  198. CallInst *CI = B.CreateCall(MemChr, {CastToCStr(Ptr, B), Val, Len}, "memchr");
  199. if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
  200. CI->setCallingConv(F->getCallingConv());
  201. return CI;
  202. }
  203. /// EmitMemCmp - Emit a call to the memcmp function.
  204. Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
  205. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  206. if (!TLI->has(LibFunc::memcmp))
  207. return nullptr;
  208. Module *M = B.GetInsertBlock()->getParent()->getParent();
  209. AttributeSet AS[3];
  210. AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
  211. AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
  212. Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
  213. AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
  214. LLVMContext &Context = B.GetInsertBlock()->getContext();
  215. Value *MemCmp = M->getOrInsertFunction(
  216. "memcmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
  217. B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
  218. CallInst *CI = B.CreateCall(
  219. MemCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "memcmp");
  220. if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
  221. CI->setCallingConv(F->getCallingConv());
  222. return CI;
  223. }
  224. /// Append a suffix to the function name according to the type of 'Op'.
  225. static void AppendTypeSuffix(Value *Op, StringRef &Name, SmallString<20> &NameBuffer) {
  226. if (!Op->getType()->isDoubleTy()) {
  227. NameBuffer += Name;
  228. if (Op->getType()->isFloatTy())
  229. NameBuffer += 'f';
  230. else
  231. NameBuffer += 'l';
  232. Name = NameBuffer;
  233. }
  234. return;
  235. }
  236. /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
  237. /// 'floor'). This function is known to take a single of type matching 'Op' and
  238. /// returns one value with the same type. If 'Op' is a long double, 'l' is
  239. /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
  240. Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
  241. const AttributeSet &Attrs) {
  242. SmallString<20> NameBuffer;
  243. AppendTypeSuffix(Op, Name, NameBuffer);
  244. Module *M = B.GetInsertBlock()->getParent()->getParent();
  245. Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
  246. Op->getType(), nullptr);
  247. CallInst *CI = B.CreateCall(Callee, Op, Name);
  248. CI->setAttributes(Attrs);
  249. if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
  250. CI->setCallingConv(F->getCallingConv());
  251. return CI;
  252. }
  253. /// EmitBinaryFloatFnCall - Emit a call to the binary function named 'Name'
  254. /// (e.g. 'fmin'). This function is known to take type matching 'Op1' and 'Op2'
  255. /// and return one value with the same type. If 'Op1/Op2' are long double, 'l'
  256. /// is added as the suffix of name, if 'Op1/Op2' is a float, we add a 'f'
  257. /// suffix.
  258. Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
  259. IRBuilder<> &B, const AttributeSet &Attrs) {
  260. SmallString<20> NameBuffer;
  261. AppendTypeSuffix(Op1, Name, NameBuffer);
  262. Module *M = B.GetInsertBlock()->getParent()->getParent();
  263. Value *Callee = M->getOrInsertFunction(Name, Op1->getType(),
  264. Op1->getType(), Op2->getType(), nullptr);
  265. CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
  266. CI->setAttributes(Attrs);
  267. if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
  268. CI->setCallingConv(F->getCallingConv());
  269. return CI;
  270. }
  271. /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
  272. /// is an integer.
  273. Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B,
  274. const TargetLibraryInfo *TLI) {
  275. if (!TLI->has(LibFunc::putchar))
  276. return nullptr;
  277. Module *M = B.GetInsertBlock()->getParent()->getParent();
  278. Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
  279. B.getInt32Ty(), nullptr);
  280. CallInst *CI = B.CreateCall(PutChar,
  281. B.CreateIntCast(Char,
  282. B.getInt32Ty(),
  283. /*isSigned*/true,
  284. "chari"),
  285. "putchar");
  286. if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
  287. CI->setCallingConv(F->getCallingConv());
  288. return CI;
  289. }
  290. /// EmitPutS - Emit a call to the puts function. This assumes that Str is
  291. /// some pointer.
  292. Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B,
  293. const TargetLibraryInfo *TLI) {
  294. if (!TLI->has(LibFunc::puts))
  295. return nullptr;
  296. Module *M = B.GetInsertBlock()->getParent()->getParent();
  297. AttributeSet AS[2];
  298. AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
  299. AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
  300. Attribute::NoUnwind);
  301. Value *PutS = M->getOrInsertFunction("puts",
  302. AttributeSet::get(M->getContext(), AS),
  303. B.getInt32Ty(),
  304. B.getInt8PtrTy(),
  305. nullptr);
  306. CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
  307. if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
  308. CI->setCallingConv(F->getCallingConv());
  309. return CI;
  310. }
  311. /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
  312. /// an integer and File is a pointer to FILE.
  313. Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
  314. const TargetLibraryInfo *TLI) {
  315. if (!TLI->has(LibFunc::fputc))
  316. return nullptr;
  317. Module *M = B.GetInsertBlock()->getParent()->getParent();
  318. AttributeSet AS[2];
  319. AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
  320. AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
  321. Attribute::NoUnwind);
  322. Constant *F;
  323. if (File->getType()->isPointerTy())
  324. F = M->getOrInsertFunction("fputc",
  325. AttributeSet::get(M->getContext(), AS),
  326. B.getInt32Ty(),
  327. B.getInt32Ty(), File->getType(),
  328. nullptr);
  329. else
  330. F = M->getOrInsertFunction("fputc",
  331. B.getInt32Ty(),
  332. B.getInt32Ty(),
  333. File->getType(), nullptr);
  334. Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
  335. "chari");
  336. CallInst *CI = B.CreateCall(F, {Char, File}, "fputc");
  337. if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
  338. CI->setCallingConv(Fn->getCallingConv());
  339. return CI;
  340. }
  341. /// EmitFPutS - Emit a call to the puts function. Str is required to be a
  342. /// pointer and File is a pointer to FILE.
  343. Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
  344. const TargetLibraryInfo *TLI) {
  345. if (!TLI->has(LibFunc::fputs))
  346. return nullptr;
  347. Module *M = B.GetInsertBlock()->getParent()->getParent();
  348. AttributeSet AS[3];
  349. AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
  350. AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
  351. AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
  352. Attribute::NoUnwind);
  353. StringRef FPutsName = TLI->getName(LibFunc::fputs);
  354. Constant *F;
  355. if (File->getType()->isPointerTy())
  356. F = M->getOrInsertFunction(FPutsName,
  357. AttributeSet::get(M->getContext(), AS),
  358. B.getInt32Ty(),
  359. B.getInt8PtrTy(),
  360. File->getType(), nullptr);
  361. else
  362. F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
  363. B.getInt8PtrTy(),
  364. File->getType(), nullptr);
  365. CallInst *CI = B.CreateCall(F, {CastToCStr(Str, B), File}, "fputs");
  366. if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
  367. CI->setCallingConv(Fn->getCallingConv());
  368. return CI;
  369. }
  370. /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
  371. /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
  372. Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
  373. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  374. if (!TLI->has(LibFunc::fwrite))
  375. return nullptr;
  376. Module *M = B.GetInsertBlock()->getParent()->getParent();
  377. AttributeSet AS[3];
  378. AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
  379. AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
  380. AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
  381. Attribute::NoUnwind);
  382. LLVMContext &Context = B.GetInsertBlock()->getContext();
  383. StringRef FWriteName = TLI->getName(LibFunc::fwrite);
  384. Constant *F;
  385. if (File->getType()->isPointerTy())
  386. F = M->getOrInsertFunction(
  387. FWriteName, AttributeSet::get(M->getContext(), AS),
  388. DL.getIntPtrType(Context), B.getInt8PtrTy(), DL.getIntPtrType(Context),
  389. DL.getIntPtrType(Context), File->getType(), nullptr);
  390. else
  391. F = M->getOrInsertFunction(FWriteName, DL.getIntPtrType(Context),
  392. B.getInt8PtrTy(), DL.getIntPtrType(Context),
  393. DL.getIntPtrType(Context), File->getType(),
  394. nullptr);
  395. CallInst *CI =
  396. B.CreateCall(F, {CastToCStr(Ptr, B), Size,
  397. ConstantInt::get(DL.getIntPtrType(Context), 1), File});
  398. if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
  399. CI->setCallingConv(Fn->getCallingConv());
  400. return CI;
  401. }