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 LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
16 #define LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
18 #include "llvm/Analysis/TargetLibraryInfo.h"
19 #include "llvm/IR/IRBuilder.h"
24 class TargetLibraryInfo;
26 /// Analyze the name and prototype of the given function and set any
27 /// applicable attributes.
28 /// If the library function is unavailable, this doesn't modify it.
30 /// Returns true if any attributes were set and false otherwise.
31 bool inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI);
33 /// Check whether the overloaded unary floating point function
34 /// corresponding to \a Ty is available.
35 bool hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
36 LibFunc DoubleFn, LibFunc FloatFn,
37 LibFunc LongDoubleFn);
39 /// Return V if it is an i8*, otherwise cast it to i8*.
40 Value *castToCStr(Value *V, IRBuilder<> &B);
42 /// Emit a call to the strlen function to the builder, for the specified
43 /// pointer. Ptr is required to be some pointer type, and the return value has
45 Value *emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
46 const TargetLibraryInfo *TLI);
48 /// Emit a call to the strnlen function to the builder, for the specified
49 /// pointer. Ptr is required to be some pointer type, MaxLen must be of size_t
50 /// type, and the return value has 'intptr_t' type.
51 Value *emitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
52 const DataLayout &DL, const TargetLibraryInfo *TLI);
54 /// Emit a call to the strchr function to the builder, for the specified
55 /// pointer and character. Ptr is required to be some pointer type, and the
56 /// return value has 'i8*' type.
57 Value *emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
58 const TargetLibraryInfo *TLI);
60 /// Emit a call to the strncmp function to the builder.
61 Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
62 const DataLayout &DL, const TargetLibraryInfo *TLI);
64 /// Emit a call to the strcpy function to the builder, for the specified
65 /// pointer arguments.
66 Value *emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
67 const TargetLibraryInfo *TLI, StringRef Name = "strcpy");
69 /// Emit a call to the strncpy function to the builder, for the specified
70 /// pointer arguments and length.
71 Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
72 const TargetLibraryInfo *TLI, StringRef Name = "strncpy");
74 /// Emit a call to the __memcpy_chk function to the builder. This expects that
75 /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers.
76 Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
77 IRBuilder<> &B, const DataLayout &DL,
78 const TargetLibraryInfo *TLI);
80 /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
81 /// Val is an i32 value, and Len is an 'intptr_t' value.
82 Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
83 const DataLayout &DL, const TargetLibraryInfo *TLI);
85 /// Emit a call to the memcmp function.
86 Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
87 const DataLayout &DL, const TargetLibraryInfo *TLI);
89 /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This
90 /// function is known to take a single of type matching 'Op' and returns one
91 /// value with the same type. If 'Op' is a long double, 'l' is added as the
92 /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
93 Value *emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
94 const AttributeList &Attrs);
96 /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
97 /// function is known to take type matching 'Op1' and 'Op2' and return one
98 /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
99 /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
100 Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
101 IRBuilder<> &B, const AttributeList &Attrs);
103 /// Emit a call to the putchar function. This assumes that Char is an integer.
104 Value *emitPutChar(Value *Char, IRBuilder<> &B, const TargetLibraryInfo *TLI);
106 /// Emit a call to the puts function. This assumes that Str is some pointer.
107 Value *emitPutS(Value *Str, IRBuilder<> &B, const TargetLibraryInfo *TLI);
109 /// Emit a call to the fputc function. This assumes that Char is an i32, and
110 /// File is a pointer to FILE.
111 Value *emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
112 const TargetLibraryInfo *TLI);
114 /// Emit a call to the fputc_unlocked function. This assumes that Char is an
115 /// i32, and File is a pointer to FILE.
116 Value *emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B,
117 const TargetLibraryInfo *TLI);
119 /// Emit a call to the fputs function. Str is required to be a pointer and
120 /// File is a pointer to FILE.
121 Value *emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
122 const TargetLibraryInfo *TLI);
124 /// Emit a call to the fputs_unlocked function. Str is required to be a
125 /// pointer and File is a pointer to FILE.
126 Value *emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B,
127 const TargetLibraryInfo *TLI);
129 /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
130 /// Size is an 'intptr_t', and File is a pointer to FILE.
131 Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
132 const DataLayout &DL, const TargetLibraryInfo *TLI);
134 /// Emit a call to the malloc function.
135 Value *emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL,
136 const TargetLibraryInfo *TLI);
138 /// Emit a call to the calloc function.
139 Value *emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
140 IRBuilder<> &B, const TargetLibraryInfo &TLI);
142 /// Emit a call to the fwrite_unlocked function. This assumes that Ptr is a
143 /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
144 Value *emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
145 IRBuilder<> &B, const DataLayout &DL,
146 const TargetLibraryInfo *TLI);
148 /// Emit a call to the fgetc_unlocked function. File is a pointer to FILE.
149 Value *emitFGetCUnlocked(Value *File, IRBuilder<> &B,
150 const TargetLibraryInfo *TLI);
152 /// Emit a call to the fgets_unlocked function. Str is required to be a
153 /// pointer, Size is an i32 and File is a pointer to FILE.
154 Value *emitFGetSUnlocked(Value *Str, Value *Size, Value *File, IRBuilder<> &B,
155 const TargetLibraryInfo *TLI);
157 /// Emit a call to the fread_unlocked function. This assumes that Ptr is a
158 /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
159 Value *emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
160 IRBuilder<> &B, const DataLayout &DL,
161 const TargetLibraryInfo *TLI);