BuildLibCalls.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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/Constants.h"
  16. #include "llvm/DataLayout.h"
  17. #include "llvm/Function.h"
  18. #include "llvm/IRBuilder.h"
  19. #include "llvm/Intrinsics.h"
  20. #include "llvm/Intrinsics.h"
  21. #include "llvm/LLVMContext.h"
  22. #include "llvm/LLVMContext.h"
  23. #include "llvm/Module.h"
  24. #include "llvm/Target/TargetLibraryInfo.h"
  25. #include "llvm/Type.h"
  26. using namespace llvm;
  27. /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
  28. Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
  29. return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
  30. }
  31. /// EmitStrLen - Emit a call to the strlen function to the builder, for the
  32. /// specified pointer. This always returns an integer value of size intptr_t.
  33. Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
  34. const TargetLibraryInfo *TLI) {
  35. if (!TLI->has(LibFunc::strlen))
  36. return 0;
  37. Module *M = B.GetInsertBlock()->getParent()->getParent();
  38. AttributeWithIndex AWI[2];
  39. AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
  40. Attribute::AttrVal AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
  41. AWI[1] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
  42. ArrayRef<Attribute::AttrVal>(AVs, 2));
  43. LLVMContext &Context = B.GetInsertBlock()->getContext();
  44. Constant *StrLen = M->getOrInsertFunction("strlen",
  45. AttributeSet::get(M->getContext(),
  46. AWI),
  47. TD->getIntPtrType(Context),
  48. B.getInt8PtrTy(),
  49. NULL);
  50. CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
  51. if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
  52. CI->setCallingConv(F->getCallingConv());
  53. return CI;
  54. }
  55. /// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
  56. /// specified pointer. Ptr is required to be some pointer type, MaxLen must
  57. /// be of size_t type, and the return value has 'intptr_t' type.
  58. Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
  59. const DataLayout *TD, const TargetLibraryInfo *TLI) {
  60. if (!TLI->has(LibFunc::strnlen))
  61. return 0;
  62. Module *M = B.GetInsertBlock()->getParent()->getParent();
  63. AttributeWithIndex AWI[2];
  64. AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
  65. Attribute::AttrVal AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
  66. AWI[1] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
  67. ArrayRef<Attribute::AttrVal>(AVs, 2));
  68. LLVMContext &Context = B.GetInsertBlock()->getContext();
  69. Constant *StrNLen = M->getOrInsertFunction("strnlen",
  70. AttributeSet::get(M->getContext(),
  71. AWI),
  72. TD->getIntPtrType(Context),
  73. B.getInt8PtrTy(),
  74. TD->getIntPtrType(Context),
  75. NULL);
  76. CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
  77. if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
  78. CI->setCallingConv(F->getCallingConv());
  79. return CI;
  80. }
  81. /// EmitStrChr - Emit a call to the strchr function to the builder, for the
  82. /// specified pointer and character. Ptr is required to be some pointer type,
  83. /// and the return value has 'i8*' type.
  84. Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
  85. const DataLayout *TD, const TargetLibraryInfo *TLI) {
  86. if (!TLI->has(LibFunc::strchr))
  87. return 0;
  88. Module *M = B.GetInsertBlock()->getParent()->getParent();
  89. Attribute::AttrVal AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
  90. AttributeWithIndex AWI =
  91. AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
  92. ArrayRef<Attribute::AttrVal>(AVs, 2));
  93. Type *I8Ptr = B.getInt8PtrTy();
  94. Type *I32Ty = B.getInt32Ty();
  95. Constant *StrChr = M->getOrInsertFunction("strchr",
  96. AttributeSet::get(M->getContext(),
  97. AWI),
  98. I8Ptr, I8Ptr, I32Ty, NULL);
  99. CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
  100. ConstantInt::get(I32Ty, C), "strchr");
  101. if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
  102. CI->setCallingConv(F->getCallingConv());
  103. return CI;
  104. }
  105. /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
  106. Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
  107. IRBuilder<> &B, const DataLayout *TD,
  108. const TargetLibraryInfo *TLI) {
  109. if (!TLI->has(LibFunc::strncmp))
  110. return 0;
  111. Module *M = B.GetInsertBlock()->getParent()->getParent();
  112. AttributeWithIndex AWI[3];
  113. AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
  114. AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attribute::NoCapture);
  115. Attribute::AttrVal AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
  116. AWI[2] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
  117. ArrayRef<Attribute::AttrVal>(AVs, 2));
  118. LLVMContext &Context = B.GetInsertBlock()->getContext();
  119. Value *StrNCmp = M->getOrInsertFunction("strncmp",
  120. AttributeSet::get(M->getContext(),
  121. AWI),
  122. B.getInt32Ty(),
  123. B.getInt8PtrTy(),
  124. B.getInt8PtrTy(),
  125. TD->getIntPtrType(Context), NULL);
  126. CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
  127. CastToCStr(Ptr2, B), Len, "strncmp");
  128. if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
  129. CI->setCallingConv(F->getCallingConv());
  130. return CI;
  131. }
  132. /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
  133. /// specified pointer arguments.
  134. Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
  135. const DataLayout *TD, const TargetLibraryInfo *TLI,
  136. StringRef Name) {
  137. if (!TLI->has(LibFunc::strcpy))
  138. return 0;
  139. Module *M = B.GetInsertBlock()->getParent()->getParent();
  140. AttributeWithIndex AWI[2];
  141. AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attribute::NoCapture);
  142. AWI[1] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
  143. Attribute::NoUnwind);
  144. Type *I8Ptr = B.getInt8PtrTy();
  145. Value *StrCpy = M->getOrInsertFunction(Name,
  146. AttributeSet::get(M->getContext(), AWI),
  147. I8Ptr, I8Ptr, I8Ptr, NULL);
  148. CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
  149. Name);
  150. if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
  151. CI->setCallingConv(F->getCallingConv());
  152. return CI;
  153. }
  154. /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
  155. /// specified pointer arguments.
  156. Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
  157. IRBuilder<> &B, const DataLayout *TD,
  158. const TargetLibraryInfo *TLI, StringRef Name) {
  159. if (!TLI->has(LibFunc::strncpy))
  160. return 0;
  161. Module *M = B.GetInsertBlock()->getParent()->getParent();
  162. AttributeWithIndex AWI[2];
  163. AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attribute::NoCapture);
  164. AWI[1] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
  165. Attribute::NoUnwind);
  166. Type *I8Ptr = B.getInt8PtrTy();
  167. Value *StrNCpy = M->getOrInsertFunction(Name,
  168. AttributeSet::get(M->getContext(),
  169. AWI),
  170. I8Ptr, I8Ptr, I8Ptr,
  171. Len->getType(), NULL);
  172. CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
  173. Len, "strncpy");
  174. if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
  175. CI->setCallingConv(F->getCallingConv());
  176. return CI;
  177. }
  178. /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
  179. /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
  180. /// are pointers.
  181. Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
  182. IRBuilder<> &B, const DataLayout *TD,
  183. const TargetLibraryInfo *TLI) {
  184. if (!TLI->has(LibFunc::memcpy_chk))
  185. return 0;
  186. Module *M = B.GetInsertBlock()->getParent()->getParent();
  187. AttributeWithIndex AWI;
  188. AWI = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
  189. Attribute::NoUnwind);
  190. LLVMContext &Context = B.GetInsertBlock()->getContext();
  191. Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
  192. AttributeSet::get(M->getContext(), AWI),
  193. B.getInt8PtrTy(),
  194. B.getInt8PtrTy(),
  195. B.getInt8PtrTy(),
  196. TD->getIntPtrType(Context),
  197. TD->getIntPtrType(Context), NULL);
  198. Dst = CastToCStr(Dst, B);
  199. Src = CastToCStr(Src, B);
  200. CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
  201. if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
  202. CI->setCallingConv(F->getCallingConv());
  203. return CI;
  204. }
  205. /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
  206. /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
  207. Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
  208. Value *Len, IRBuilder<> &B, const DataLayout *TD,
  209. const TargetLibraryInfo *TLI) {
  210. if (!TLI->has(LibFunc::memchr))
  211. return 0;
  212. Module *M = B.GetInsertBlock()->getParent()->getParent();
  213. AttributeWithIndex AWI;
  214. Attribute::AttrVal AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
  215. AWI = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
  216. ArrayRef<Attribute::AttrVal>(AVs, 2));
  217. LLVMContext &Context = B.GetInsertBlock()->getContext();
  218. Value *MemChr = M->getOrInsertFunction("memchr",
  219. AttributeSet::get(M->getContext(), AWI),
  220. B.getInt8PtrTy(),
  221. B.getInt8PtrTy(),
  222. B.getInt32Ty(),
  223. TD->getIntPtrType(Context),
  224. NULL);
  225. CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
  226. if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
  227. CI->setCallingConv(F->getCallingConv());
  228. return CI;
  229. }
  230. /// EmitMemCmp - Emit a call to the memcmp function.
  231. Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
  232. Value *Len, IRBuilder<> &B, const DataLayout *TD,
  233. const TargetLibraryInfo *TLI) {
  234. if (!TLI->has(LibFunc::memcmp))
  235. return 0;
  236. Module *M = B.GetInsertBlock()->getParent()->getParent();
  237. AttributeWithIndex AWI[3];
  238. AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
  239. AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attribute::NoCapture);
  240. Attribute::AttrVal AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
  241. AWI[2] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
  242. ArrayRef<Attribute::AttrVal>(AVs, 2));
  243. LLVMContext &Context = B.GetInsertBlock()->getContext();
  244. Value *MemCmp = M->getOrInsertFunction("memcmp",
  245. AttributeSet::get(M->getContext(), AWI),
  246. B.getInt32Ty(),
  247. B.getInt8PtrTy(),
  248. B.getInt8PtrTy(),
  249. TD->getIntPtrType(Context), NULL);
  250. CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
  251. Len, "memcmp");
  252. if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
  253. CI->setCallingConv(F->getCallingConv());
  254. return CI;
  255. }
  256. /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
  257. /// 'floor'). This function is known to take a single of type matching 'Op' and
  258. /// returns one value with the same type. If 'Op' is a long double, 'l' is
  259. /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
  260. Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
  261. const AttributeSet &Attrs) {
  262. SmallString<20> NameBuffer;
  263. if (!Op->getType()->isDoubleTy()) {
  264. // If we need to add a suffix, copy into NameBuffer.
  265. NameBuffer += Name;
  266. if (Op->getType()->isFloatTy())
  267. NameBuffer += 'f'; // floorf
  268. else
  269. NameBuffer += 'l'; // floorl
  270. Name = NameBuffer;
  271. }
  272. Module *M = B.GetInsertBlock()->getParent()->getParent();
  273. Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
  274. Op->getType(), NULL);
  275. CallInst *CI = B.CreateCall(Callee, Op, Name);
  276. CI->setAttributes(Attrs);
  277. if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
  278. CI->setCallingConv(F->getCallingConv());
  279. return CI;
  280. }
  281. /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
  282. /// is an integer.
  283. Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
  284. const TargetLibraryInfo *TLI) {
  285. if (!TLI->has(LibFunc::putchar))
  286. return 0;
  287. Module *M = B.GetInsertBlock()->getParent()->getParent();
  288. Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
  289. B.getInt32Ty(), NULL);
  290. CallInst *CI = B.CreateCall(PutChar,
  291. B.CreateIntCast(Char,
  292. B.getInt32Ty(),
  293. /*isSigned*/true,
  294. "chari"),
  295. "putchar");
  296. if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
  297. CI->setCallingConv(F->getCallingConv());
  298. return CI;
  299. }
  300. /// EmitPutS - Emit a call to the puts function. This assumes that Str is
  301. /// some pointer.
  302. Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
  303. const TargetLibraryInfo *TLI) {
  304. if (!TLI->has(LibFunc::puts))
  305. return 0;
  306. Module *M = B.GetInsertBlock()->getParent()->getParent();
  307. AttributeWithIndex AWI[2];
  308. AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
  309. AWI[1] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
  310. Attribute::NoUnwind);
  311. Value *PutS = M->getOrInsertFunction("puts",
  312. AttributeSet::get(M->getContext(), AWI),
  313. B.getInt32Ty(),
  314. B.getInt8PtrTy(),
  315. NULL);
  316. CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
  317. if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
  318. CI->setCallingConv(F->getCallingConv());
  319. return CI;
  320. }
  321. /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
  322. /// an integer and File is a pointer to FILE.
  323. Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
  324. const DataLayout *TD, const TargetLibraryInfo *TLI) {
  325. if (!TLI->has(LibFunc::fputc))
  326. return 0;
  327. Module *M = B.GetInsertBlock()->getParent()->getParent();
  328. AttributeWithIndex AWI[2];
  329. AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attribute::NoCapture);
  330. AWI[1] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
  331. Attribute::NoUnwind);
  332. Constant *F;
  333. if (File->getType()->isPointerTy())
  334. F = M->getOrInsertFunction("fputc",
  335. AttributeSet::get(M->getContext(), AWI),
  336. B.getInt32Ty(),
  337. B.getInt32Ty(), File->getType(),
  338. NULL);
  339. else
  340. F = M->getOrInsertFunction("fputc",
  341. B.getInt32Ty(),
  342. B.getInt32Ty(),
  343. File->getType(), NULL);
  344. Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
  345. "chari");
  346. CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
  347. if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
  348. CI->setCallingConv(Fn->getCallingConv());
  349. return CI;
  350. }
  351. /// EmitFPutS - Emit a call to the puts function. Str is required to be a
  352. /// pointer and File is a pointer to FILE.
  353. Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
  354. const DataLayout *TD, const TargetLibraryInfo *TLI) {
  355. if (!TLI->has(LibFunc::fputs))
  356. return 0;
  357. Module *M = B.GetInsertBlock()->getParent()->getParent();
  358. AttributeWithIndex AWI[3];
  359. AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
  360. AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attribute::NoCapture);
  361. AWI[2] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
  362. Attribute::NoUnwind);
  363. StringRef FPutsName = TLI->getName(LibFunc::fputs);
  364. Constant *F;
  365. if (File->getType()->isPointerTy())
  366. F = M->getOrInsertFunction(FPutsName,
  367. AttributeSet::get(M->getContext(), AWI),
  368. B.getInt32Ty(),
  369. B.getInt8PtrTy(),
  370. File->getType(), NULL);
  371. else
  372. F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
  373. B.getInt8PtrTy(),
  374. File->getType(), NULL);
  375. CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
  376. if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
  377. CI->setCallingConv(Fn->getCallingConv());
  378. return CI;
  379. }
  380. /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
  381. /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
  382. Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
  383. IRBuilder<> &B, const DataLayout *TD,
  384. const TargetLibraryInfo *TLI) {
  385. if (!TLI->has(LibFunc::fwrite))
  386. return 0;
  387. Module *M = B.GetInsertBlock()->getParent()->getParent();
  388. AttributeWithIndex AWI[3];
  389. AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
  390. AWI[1] = AttributeWithIndex::get(M->getContext(), 4, Attribute::NoCapture);
  391. AWI[2] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
  392. Attribute::NoUnwind);
  393. LLVMContext &Context = B.GetInsertBlock()->getContext();
  394. StringRef FWriteName = TLI->getName(LibFunc::fwrite);
  395. Constant *F;
  396. if (File->getType()->isPointerTy())
  397. F = M->getOrInsertFunction(FWriteName,
  398. AttributeSet::get(M->getContext(), AWI),
  399. TD->getIntPtrType(Context),
  400. B.getInt8PtrTy(),
  401. TD->getIntPtrType(Context),
  402. TD->getIntPtrType(Context),
  403. File->getType(), NULL);
  404. else
  405. F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
  406. B.getInt8PtrTy(),
  407. TD->getIntPtrType(Context),
  408. TD->getIntPtrType(Context),
  409. File->getType(), NULL);
  410. CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
  411. ConstantInt::get(TD->getIntPtrType(Context), 1), File);
  412. if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
  413. CI->setCallingConv(Fn->getCallingConv());
  414. return CI;
  415. }
  416. SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
  417. bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD,
  418. const TargetLibraryInfo *TLI) {
  419. // We really need DataLayout for later.
  420. if (!TD) return false;
  421. this->CI = CI;
  422. Function *Callee = CI->getCalledFunction();
  423. StringRef Name = Callee->getName();
  424. FunctionType *FT = Callee->getFunctionType();
  425. LLVMContext &Context = CI->getParent()->getContext();
  426. IRBuilder<> B(CI);
  427. if (Name == "__memcpy_chk") {
  428. // Check if this has the right signature.
  429. if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
  430. !FT->getParamType(0)->isPointerTy() ||
  431. !FT->getParamType(1)->isPointerTy() ||
  432. FT->getParamType(2) != TD->getIntPtrType(Context) ||
  433. FT->getParamType(3) != TD->getIntPtrType(Context))
  434. return false;
  435. if (isFoldable(3, 2, false)) {
  436. B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
  437. CI->getArgOperand(2), 1);
  438. replaceCall(CI->getArgOperand(0));
  439. return true;
  440. }
  441. return false;
  442. }
  443. // Should be similar to memcpy.
  444. if (Name == "__mempcpy_chk") {
  445. return false;
  446. }
  447. if (Name == "__memmove_chk") {
  448. // Check if this has the right signature.
  449. if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
  450. !FT->getParamType(0)->isPointerTy() ||
  451. !FT->getParamType(1)->isPointerTy() ||
  452. FT->getParamType(2) != TD->getIntPtrType(Context) ||
  453. FT->getParamType(3) != TD->getIntPtrType(Context))
  454. return false;
  455. if (isFoldable(3, 2, false)) {
  456. B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
  457. CI->getArgOperand(2), 1);
  458. replaceCall(CI->getArgOperand(0));
  459. return true;
  460. }
  461. return false;
  462. }
  463. if (Name == "__memset_chk") {
  464. // Check if this has the right signature.
  465. if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
  466. !FT->getParamType(0)->isPointerTy() ||
  467. !FT->getParamType(1)->isIntegerTy() ||
  468. FT->getParamType(2) != TD->getIntPtrType(Context) ||
  469. FT->getParamType(3) != TD->getIntPtrType(Context))
  470. return false;
  471. if (isFoldable(3, 2, false)) {
  472. Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
  473. false);
  474. B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
  475. replaceCall(CI->getArgOperand(0));
  476. return true;
  477. }
  478. return false;
  479. }
  480. if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
  481. // Check if this has the right signature.
  482. if (FT->getNumParams() != 3 ||
  483. FT->getReturnType() != FT->getParamType(0) ||
  484. FT->getParamType(0) != FT->getParamType(1) ||
  485. FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
  486. FT->getParamType(2) != TD->getIntPtrType(Context))
  487. return 0;
  488. // If a) we don't have any length information, or b) we know this will
  489. // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
  490. // st[rp]cpy_chk call which may fail at runtime if the size is too long.
  491. // TODO: It might be nice to get a maximum length out of the possible
  492. // string lengths for varying.
  493. if (isFoldable(2, 1, true)) {
  494. Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
  495. TLI, Name.substr(2, 6));
  496. if (!Ret)
  497. return false;
  498. replaceCall(Ret);
  499. return true;
  500. }
  501. return false;
  502. }
  503. if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
  504. // Check if this has the right signature.
  505. if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
  506. FT->getParamType(0) != FT->getParamType(1) ||
  507. FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
  508. !FT->getParamType(2)->isIntegerTy() ||
  509. FT->getParamType(3) != TD->getIntPtrType(Context))
  510. return false;
  511. if (isFoldable(3, 2, false)) {
  512. Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
  513. CI->getArgOperand(2), B, TD, TLI,
  514. Name.substr(2, 7));
  515. if (!Ret)
  516. return false;
  517. replaceCall(Ret);
  518. return true;
  519. }
  520. return false;
  521. }
  522. if (Name == "__strcat_chk") {
  523. return false;
  524. }
  525. if (Name == "__strncat_chk") {
  526. return false;
  527. }
  528. return false;
  529. }