1 //===- BuildLibCalls.h - Utility builder for libcalls -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes an interface to build some C language libcalls for
11 // optimization passes that need to call the various functions.
13 //===----------------------------------------------------------------------===//
15 #ifndef TRANSFORMS_UTILS_BUILDLIBCALLS_H
16 #define TRANSFORMS_UTILS_BUILDLIBCALLS_H
18 #include "llvm/Support/IRBuilder.h"
23 class TargetLibraryInfo;
25 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
26 Value *CastToCStr(Value *V, IRBuilder<> &B);
28 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
29 /// specified pointer. Ptr is required to be some pointer type, and the
30 /// return value has 'intptr_t' type.
31 Value *EmitStrLen(Value *Ptr, IRBuilder<> &B, const TargetData *TD);
33 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
34 /// specified pointer and character. Ptr is required to be some pointer type,
35 /// and the return value has 'i8*' type.
36 Value *EmitStrChr(Value *Ptr, char C, IRBuilder<> &B, const TargetData *TD);
38 /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
39 Value *EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
40 const TargetData *TD);
42 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
43 /// specified pointer arguments.
44 Value *EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
45 const TargetData *TD, StringRef Name = "strcpy");
47 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
48 /// specified pointer arguments and length.
49 Value *EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
50 const TargetData *TD, StringRef Name = "strncpy");
52 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
53 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
55 Value *EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
56 IRBuilder<> &B, const TargetData *TD);
58 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
59 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
60 Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
61 const TargetData *TD);
63 /// EmitMemCmp - Emit a call to the memcmp function.
64 Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
65 const TargetData *TD);
67 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name'
68 /// (e.g. 'floor'). This function is known to take a single of type matching
69 /// 'Op' and returns one value with the same type. If 'Op' is a long double,
70 /// 'l' is added as the suffix of name, if 'Op' is a float, we add a 'f'
72 Value *EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
73 const AttrListPtr &Attrs);
75 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
77 Value *EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD);
79 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
81 void EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD);
83 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
84 /// an i32, and File is a pointer to FILE.
85 void EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
86 const TargetData *TD);
88 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
89 /// pointer and File is a pointer to FILE.
90 void EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, const TargetData *TD,
91 const TargetLibraryInfo *TLI);
93 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
94 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
95 void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
96 const TargetData *TD, const TargetLibraryInfo *TLI);
98 /// SimplifyFortifiedLibCalls - Helper class for folding checked library
99 /// calls (e.g. __strcpy_chk) into their unchecked counterparts.
100 class SimplifyFortifiedLibCalls {
103 virtual void replaceCall(Value *With) = 0;
104 virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
105 bool isString) const = 0;
107 virtual ~SimplifyFortifiedLibCalls();
108 bool fold(CallInst *CI, const TargetData *TD);