LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BuildLibCalls.cpp
Go to the documentation of this file.
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 
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/Intrinsics.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/Type.h"
25 
26 using namespace llvm;
27 
28 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
30  return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
31 }
32 
33 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
34 /// specified pointer. This always returns an integer value of size intptr_t.
36  const TargetLibraryInfo *TLI) {
37  if (!TLI->has(LibFunc::strlen))
38  return 0;
39 
41  AttributeSet AS[2];
42  AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
44  AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
46 
47  LLVMContext &Context = B.GetInsertBlock()->getContext();
48  Constant *StrLen = M->getOrInsertFunction("strlen",
49  AttributeSet::get(M->getContext(),
50  AS),
51  TD->getIntPtrType(Context),
52  B.getInt8PtrTy(),
53  NULL);
54  CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
55  if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
56  CI->setCallingConv(F->getCallingConv());
57 
58  return CI;
59 }
60 
61 /// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
62 /// specified pointer. Ptr is required to be some pointer type, MaxLen must
63 /// be of size_t type, and the return value has 'intptr_t' type.
65  const DataLayout *TD, const TargetLibraryInfo *TLI) {
66  if (!TLI->has(LibFunc::strnlen))
67  return 0;
68 
70  AttributeSet AS[2];
71  AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
73  AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
75 
76  LLVMContext &Context = B.GetInsertBlock()->getContext();
77  Constant *StrNLen = M->getOrInsertFunction("strnlen",
78  AttributeSet::get(M->getContext(),
79  AS),
80  TD->getIntPtrType(Context),
81  B.getInt8PtrTy(),
82  TD->getIntPtrType(Context),
83  NULL);
84  CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
85  if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
86  CI->setCallingConv(F->getCallingConv());
87 
88  return CI;
89 }
90 
91 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
92 /// specified pointer and character. Ptr is required to be some pointer type,
93 /// and the return value has 'i8*' type.
95  const DataLayout *TD, const TargetLibraryInfo *TLI) {
96  if (!TLI->has(LibFunc::strchr))
97  return 0;
98 
101  AttributeSet AS =
102  AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
104 
105  Type *I8Ptr = B.getInt8PtrTy();
106  Type *I32Ty = B.getInt32Ty();
107  Constant *StrChr = M->getOrInsertFunction("strchr",
108  AttributeSet::get(M->getContext(),
109  AS),
110  I8Ptr, I8Ptr, I32Ty, NULL);
111  CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
112  ConstantInt::get(I32Ty, C), "strchr");
113  if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
114  CI->setCallingConv(F->getCallingConv());
115  return CI;
116 }
117 
118 /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
120  IRBuilder<> &B, const DataLayout *TD,
121  const TargetLibraryInfo *TLI) {
122  if (!TLI->has(LibFunc::strncmp))
123  return 0;
124 
125  Module *M = B.GetInsertBlock()->getParent()->getParent();
126  AttributeSet AS[3];
127  AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
128  AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
130  AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
132 
133  LLVMContext &Context = B.GetInsertBlock()->getContext();
134  Value *StrNCmp = M->getOrInsertFunction("strncmp",
135  AttributeSet::get(M->getContext(),
136  AS),
137  B.getInt32Ty(),
138  B.getInt8PtrTy(),
139  B.getInt8PtrTy(),
140  TD->getIntPtrType(Context), NULL);
141  CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
142  CastToCStr(Ptr2, B), Len, "strncmp");
143 
144  if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
145  CI->setCallingConv(F->getCallingConv());
146 
147  return CI;
148 }
149 
150 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
151 /// specified pointer arguments.
153  const DataLayout *TD, const TargetLibraryInfo *TLI,
154  StringRef Name) {
155  if (!TLI->has(LibFunc::strcpy))
156  return 0;
157 
158  Module *M = B.GetInsertBlock()->getParent()->getParent();
159  AttributeSet AS[2];
160  AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
161  AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
163  Type *I8Ptr = B.getInt8PtrTy();
164  Value *StrCpy = M->getOrInsertFunction(Name,
165  AttributeSet::get(M->getContext(), AS),
166  I8Ptr, I8Ptr, I8Ptr, NULL);
167  CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
168  Name);
169  if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
170  CI->setCallingConv(F->getCallingConv());
171  return CI;
172 }
173 
174 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
175 /// specified pointer arguments.
177  IRBuilder<> &B, const DataLayout *TD,
178  const TargetLibraryInfo *TLI, StringRef Name) {
179  if (!TLI->has(LibFunc::strncpy))
180  return 0;
181 
182  Module *M = B.GetInsertBlock()->getParent()->getParent();
183  AttributeSet AS[2];
184  AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
185  AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
187  Type *I8Ptr = B.getInt8PtrTy();
188  Value *StrNCpy = M->getOrInsertFunction(Name,
189  AttributeSet::get(M->getContext(),
190  AS),
191  I8Ptr, I8Ptr, I8Ptr,
192  Len->getType(), NULL);
193  CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
194  Len, "strncpy");
195  if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
196  CI->setCallingConv(F->getCallingConv());
197  return CI;
198 }
199 
200 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
201 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
202 /// are pointers.
203 Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
204  IRBuilder<> &B, const DataLayout *TD,
205  const TargetLibraryInfo *TLI) {
206  if (!TLI->has(LibFunc::memcpy_chk))
207  return 0;
208 
209  Module *M = B.GetInsertBlock()->getParent()->getParent();
210  AttributeSet AS;
211  AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
213  LLVMContext &Context = B.GetInsertBlock()->getContext();
214  Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
215  AttributeSet::get(M->getContext(), AS),
216  B.getInt8PtrTy(),
217  B.getInt8PtrTy(),
218  B.getInt8PtrTy(),
219  TD->getIntPtrType(Context),
220  TD->getIntPtrType(Context), NULL);
221  Dst = CastToCStr(Dst, B);
222  Src = CastToCStr(Src, B);
223  CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
224  if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
225  CI->setCallingConv(F->getCallingConv());
226  return CI;
227 }
228 
229 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
230 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
232  Value *Len, IRBuilder<> &B, const DataLayout *TD,
233  const TargetLibraryInfo *TLI) {
234  if (!TLI->has(LibFunc::memchr))
235  return 0;
236 
237  Module *M = B.GetInsertBlock()->getParent()->getParent();
238  AttributeSet AS;
240  AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
242  LLVMContext &Context = B.GetInsertBlock()->getContext();
243  Value *MemChr = M->getOrInsertFunction("memchr",
244  AttributeSet::get(M->getContext(), AS),
245  B.getInt8PtrTy(),
246  B.getInt8PtrTy(),
247  B.getInt32Ty(),
248  TD->getIntPtrType(Context),
249  NULL);
250  CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
251 
252  if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
253  CI->setCallingConv(F->getCallingConv());
254 
255  return CI;
256 }
257 
258 /// EmitMemCmp - Emit a call to the memcmp function.
260  Value *Len, IRBuilder<> &B, const DataLayout *TD,
261  const TargetLibraryInfo *TLI) {
262  if (!TLI->has(LibFunc::memcmp))
263  return 0;
264 
265  Module *M = B.GetInsertBlock()->getParent()->getParent();
266  AttributeSet AS[3];
267  AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
268  AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
270  AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
272 
273  LLVMContext &Context = B.GetInsertBlock()->getContext();
274  Value *MemCmp = M->getOrInsertFunction("memcmp",
275  AttributeSet::get(M->getContext(), AS),
276  B.getInt32Ty(),
277  B.getInt8PtrTy(),
278  B.getInt8PtrTy(),
279  TD->getIntPtrType(Context), NULL);
280  CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
281  Len, "memcmp");
282 
283  if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
284  CI->setCallingConv(F->getCallingConv());
285 
286  return CI;
287 }
288 
289 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
290 /// 'floor'). This function is known to take a single of type matching 'Op' and
291 /// returns one value with the same type. If 'Op' is a long double, 'l' is
292 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
294  const AttributeSet &Attrs) {
295  SmallString<20> NameBuffer;
296  if (!Op->getType()->isDoubleTy()) {
297  // If we need to add a suffix, copy into NameBuffer.
298  NameBuffer += Name;
299  if (Op->getType()->isFloatTy())
300  NameBuffer += 'f'; // floorf
301  else
302  NameBuffer += 'l'; // floorl
303  Name = NameBuffer;
304  }
305 
306  Module *M = B.GetInsertBlock()->getParent()->getParent();
307  Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
308  Op->getType(), NULL);
309  CallInst *CI = B.CreateCall(Callee, Op, Name);
310  CI->setAttributes(Attrs);
311  if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
312  CI->setCallingConv(F->getCallingConv());
313 
314  return CI;
315 }
316 
317 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
318 /// is an integer.
320  const TargetLibraryInfo *TLI) {
321  if (!TLI->has(LibFunc::putchar))
322  return 0;
323 
324  Module *M = B.GetInsertBlock()->getParent()->getParent();
325  Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
326  B.getInt32Ty(), NULL);
327  CallInst *CI = B.CreateCall(PutChar,
328  B.CreateIntCast(Char,
329  B.getInt32Ty(),
330  /*isSigned*/true,
331  "chari"),
332  "putchar");
333 
334  if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
335  CI->setCallingConv(F->getCallingConv());
336  return CI;
337 }
338 
339 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
340 /// some pointer.
342  const TargetLibraryInfo *TLI) {
343  if (!TLI->has(LibFunc::puts))
344  return 0;
345 
346  Module *M = B.GetInsertBlock()->getParent()->getParent();
347  AttributeSet AS[2];
348  AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
349  AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
351 
352  Value *PutS = M->getOrInsertFunction("puts",
353  AttributeSet::get(M->getContext(), AS),
354  B.getInt32Ty(),
355  B.getInt8PtrTy(),
356  NULL);
357  CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
358  if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
359  CI->setCallingConv(F->getCallingConv());
360  return CI;
361 }
362 
363 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
364 /// an integer and File is a pointer to FILE.
366  const DataLayout *TD, const TargetLibraryInfo *TLI) {
367  if (!TLI->has(LibFunc::fputc))
368  return 0;
369 
370  Module *M = B.GetInsertBlock()->getParent()->getParent();
371  AttributeSet AS[2];
372  AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
373  AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
375  Constant *F;
376  if (File->getType()->isPointerTy())
377  F = M->getOrInsertFunction("fputc",
378  AttributeSet::get(M->getContext(), AS),
379  B.getInt32Ty(),
380  B.getInt32Ty(), File->getType(),
381  NULL);
382  else
383  F = M->getOrInsertFunction("fputc",
384  B.getInt32Ty(),
385  B.getInt32Ty(),
386  File->getType(), NULL);
387  Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
388  "chari");
389  CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
390 
391  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
392  CI->setCallingConv(Fn->getCallingConv());
393  return CI;
394 }
395 
396 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
397 /// pointer and File is a pointer to FILE.
399  const DataLayout *TD, const TargetLibraryInfo *TLI) {
400  if (!TLI->has(LibFunc::fputs))
401  return 0;
402 
403  Module *M = B.GetInsertBlock()->getParent()->getParent();
404  AttributeSet AS[3];
405  AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
406  AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
407  AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
409  StringRef FPutsName = TLI->getName(LibFunc::fputs);
410  Constant *F;
411  if (File->getType()->isPointerTy())
412  F = M->getOrInsertFunction(FPutsName,
413  AttributeSet::get(M->getContext(), AS),
414  B.getInt32Ty(),
415  B.getInt8PtrTy(),
416  File->getType(), NULL);
417  else
418  F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
419  B.getInt8PtrTy(),
420  File->getType(), NULL);
421  CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
422 
423  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
424  CI->setCallingConv(Fn->getCallingConv());
425  return CI;
426 }
427 
428 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
429 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
431  IRBuilder<> &B, const DataLayout *TD,
432  const TargetLibraryInfo *TLI) {
433  if (!TLI->has(LibFunc::fwrite))
434  return 0;
435 
436  Module *M = B.GetInsertBlock()->getParent()->getParent();
437  AttributeSet AS[3];
438  AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
439  AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
440  AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
442  LLVMContext &Context = B.GetInsertBlock()->getContext();
443  StringRef FWriteName = TLI->getName(LibFunc::fwrite);
444  Constant *F;
445  if (File->getType()->isPointerTy())
446  F = M->getOrInsertFunction(FWriteName,
447  AttributeSet::get(M->getContext(), AS),
448  TD->getIntPtrType(Context),
449  B.getInt8PtrTy(),
450  TD->getIntPtrType(Context),
451  TD->getIntPtrType(Context),
452  File->getType(), NULL);
453  else
454  F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
455  B.getInt8PtrTy(),
456  TD->getIntPtrType(Context),
457  TD->getIntPtrType(Context),
458  File->getType(), NULL);
459  CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
460  ConstantInt::get(TD->getIntPtrType(Context), 1), File);
461 
462  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
463  CI->setCallingConv(Fn->getCallingConv());
464  return CI;
465 }
466 
468 
470  const TargetLibraryInfo *TLI) {
471  // We really need DataLayout for later.
472  if (!TD) return false;
473 
474  this->CI = CI;
475  Function *Callee = CI->getCalledFunction();
476  StringRef Name = Callee->getName();
477  FunctionType *FT = Callee->getFunctionType();
478  LLVMContext &Context = CI->getParent()->getContext();
479  IRBuilder<> B(CI);
480 
481  if (Name == "__memcpy_chk") {
482  // Check if this has the right signature.
483  if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
484  !FT->getParamType(0)->isPointerTy() ||
485  !FT->getParamType(1)->isPointerTy() ||
486  FT->getParamType(2) != TD->getIntPtrType(Context) ||
487  FT->getParamType(3) != TD->getIntPtrType(Context))
488  return false;
489 
490  if (isFoldable(3, 2, false)) {
491  B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
492  CI->getArgOperand(2), 1);
493  replaceCall(CI->getArgOperand(0));
494  return true;
495  }
496  return false;
497  }
498 
499  // Should be similar to memcpy.
500  if (Name == "__mempcpy_chk") {
501  return false;
502  }
503 
504  if (Name == "__memmove_chk") {
505  // Check if this has the right signature.
506  if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
507  !FT->getParamType(0)->isPointerTy() ||
508  !FT->getParamType(1)->isPointerTy() ||
509  FT->getParamType(2) != TD->getIntPtrType(Context) ||
510  FT->getParamType(3) != TD->getIntPtrType(Context))
511  return false;
512 
513  if (isFoldable(3, 2, false)) {
514  B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
515  CI->getArgOperand(2), 1);
516  replaceCall(CI->getArgOperand(0));
517  return true;
518  }
519  return false;
520  }
521 
522  if (Name == "__memset_chk") {
523  // Check if this has the right signature.
524  if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
525  !FT->getParamType(0)->isPointerTy() ||
526  !FT->getParamType(1)->isIntegerTy() ||
527  FT->getParamType(2) != TD->getIntPtrType(Context) ||
528  FT->getParamType(3) != TD->getIntPtrType(Context))
529  return false;
530 
531  if (isFoldable(3, 2, false)) {
532  Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
533  false);
534  B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
535  replaceCall(CI->getArgOperand(0));
536  return true;
537  }
538  return false;
539  }
540 
541  if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
542  // Check if this has the right signature.
543  if (FT->getNumParams() != 3 ||
544  FT->getReturnType() != FT->getParamType(0) ||
545  FT->getParamType(0) != FT->getParamType(1) ||
546  FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
547  FT->getParamType(2) != TD->getIntPtrType(Context))
548  return 0;
549 
550 
551  // If a) we don't have any length information, or b) we know this will
552  // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
553  // st[rp]cpy_chk call which may fail at runtime if the size is too long.
554  // TODO: It might be nice to get a maximum length out of the possible
555  // string lengths for varying.
556  if (isFoldable(2, 1, true)) {
557  Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
558  TLI, Name.substr(2, 6));
559  if (!Ret)
560  return false;
561  replaceCall(Ret);
562  return true;
563  }
564  return false;
565  }
566 
567  if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
568  // Check if this has the right signature.
569  if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
570  FT->getParamType(0) != FT->getParamType(1) ||
571  FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
572  !FT->getParamType(2)->isIntegerTy() ||
573  FT->getParamType(3) != TD->getIntPtrType(Context))
574  return false;
575 
576  if (isFoldable(3, 2, false)) {
577  Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
578  CI->getArgOperand(2), B, TD, TLI,
579  Name.substr(2, 7));
580  if (!Ret)
581  return false;
582  replaceCall(Ret);
583  return true;
584  }
585  return false;
586  }
587 
588  if (Name == "__strcat_chk") {
589  return false;
590  }
591 
592  if (Name == "__strncat_chk") {
593  return false;
594  }
595 
596  return false;
597 }
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:1180
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
unsigned getNumParams() const
Definition: DerivedTypes.h:133
static MemCmpOpt MemCmp
StringRef substr(size_t Start, size_t N=npos) const
Definition: StringRef.h:392
bool isDoubleTy() const
isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:149
Value * EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI)
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:116
F(f)
size_t strnlen(const char *s, size_t maxlen);
Value * EmitStrChr(Value *Ptr, char C, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI)
StringRef getName() const
Definition: Value.cpp:167
int puts(const char *s);
void setCallingConv(CallingConv::ID CC)
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:310
CallInst * CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align, bool isVolatile=false, MDNode *TBAATag=0)
Create and insert a memmove between the specified pointers.
Definition: IRBuilder.h:381
bool has(LibFunc::Func F) const
Value * EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, const AttributeSet &Attrs)
bool fold(CallInst *CI, const DataLayout *TD, const TargetLibraryInfo *TLI)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:421
virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp, bool isString) const =0
char *strchr(const char *s, int c);
Function creates no aliases of pointer.
Definition: Attributes.h:82
Value * EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI)
static StrNCmpOpt StrNCmp
CallInst * CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3, Value *Arg4, const Twine &Name="")
Definition: IRBuilder.h:1320
Value * EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI, StringRef Name="strncpy")
int memcmp(const void *s1, const void *s2, size_t n);
virtual void replaceCall(Value *With)=0
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:128
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeSet AttributeList)
Definition: Module.cpp:138
LLVM Constant Representation.
Definition: Constant.h:41
bool isFloatTy() const
isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:146
Value * EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI)
static MemCpyOpt MemCpy
void *memchr(const void *s, int c, size_t n);
Value * EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI)
Function doesn't unwind stack.
Definition: Attributes.h:90
bool isPointerTy() const
Definition: Type.h:220
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:284
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
Definition: IRBuilder.h:335
Value * EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI)
int fputs(const char *s, FILE *stream);
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Definition: DataLayout.cpp:610
Value * EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI, StringRef Name="strcpy")
void *__memcpy_chk(void *s1, const void *s2, size_t n, size_t s1size);
Value * CastToCStr(Value *V, IRBuilder<> &B)
CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1132
Type * getType() const
Definition: Value.h:111
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:77
Value * stripPointerCasts()
Strips off any unneeded pointer casts, all-zero GEPs and aliases from the specified value...
Definition: Value.cpp:385
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:492
Function * getCalledFunction() const
size_t strlen(const char *s);
Value * getArgOperand(unsigned i) const
bool isIntegerTy() const
Definition: Type.h:196
Function only reads from memory.
Definition: Attributes.h:94
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition: IRBuilder.h:300
Value * EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI)
int fputc(int c, FILE *stream);
static StrChrOpt StrChr
StringRef getName(LibFunc::Func F) const
FunctionType * getFunctionType() const
Definition: Function.cpp:171
CallInst * CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align, bool isVolatile=false, MDNode *TBAATag=0)
Create and insert a memset to the specified pointer and the specified value.
Definition: IRBuilder.h:353
static StrCpyOpt StrCpy
static StrNCpyOpt StrNCpy
CallInst * CreateCall2(Value *Callee, Value *Arg1, Value *Arg2, const Twine &Name="")
Definition: IRBuilder.h:1310
void setAttributes(const AttributeSet &Attrs)
Type * getReturnType() const
Definition: DerivedTypes.h:121
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:33
Value * EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI)
Module * getParent()
Definition: GlobalValue.h:286
LLVM Value Representation.
Definition: Value.h:66
static StrLenOpt StrLen
CallInst * CreateCall(Value *Callee, const Twine &Name="")
Definition: IRBuilder.h:1304
int putchar(int c);
char *strcpy(char *s1, const char *s2);
Value * EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI)
int strncmp(const char *s1, const char *s2, size_t n);
Value * EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI)
EmitMemCmp - Emit a call to the memcmp function.
Value * EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI)
EmitStrNCmp - Emit a call to the strncmp function to the builder.
const BasicBlock * getParent() const
Definition: Instruction.h:52
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD
Value * EmitFPutC(Value *Char, Value *File, IRBuilder<> &B, const DataLayout *TD, const TargetLibraryInfo *TLI)
CallInst * CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align, bool isVolatile=false, MDNode *TBAATag=0, MDNode *TBAAStructTag=0)
Create and insert a memcpy between the specified pointers.
Definition: IRBuilder.h:365
LLVMContext & getContext() const
Definition: Module.h:249
CallInst * CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3, const Twine &Name="")
Definition: IRBuilder.h:1315
char *strncpy(char *s1, const char *s2, size_t n);