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);
32 bool inferLibFuncAttributes(Module *M, StringRef Name, const TargetLibraryInfo &TLI);
34 /// Check whether the overloaded unary floating point function
35 /// corresponding to \a Ty is available.
36 bool hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
37 LibFunc DoubleFn, LibFunc FloatFn,
38 LibFunc LongDoubleFn);
40 /// Get the name of the overloaded unary floating point function
41 /// corresponding to \a Ty.
42 StringRef getUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
43 LibFunc DoubleFn, LibFunc FloatFn,
44 LibFunc LongDoubleFn);
46 /// Return V if it is an i8*, otherwise cast it to i8*.
47 Value *castToCStr(Value *V, IRBuilder<> &B);
49 /// Emit a call to the strlen function to the builder, for the specified
50 /// pointer. Ptr is required to be some pointer type, and the return value has
52 Value *emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
53 const TargetLibraryInfo *TLI);
55 /// Emit a call to the strnlen function to the builder, for the specified
56 /// pointer. Ptr is required to be some pointer type, MaxLen must be of size_t
57 /// type, and the return value has 'intptr_t' type.
58 Value *emitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
59 const DataLayout &DL, const TargetLibraryInfo *TLI);
61 /// Emit a call to the strchr function to the builder, for the specified
62 /// pointer and character. Ptr is required to be some pointer type, and the
63 /// return value has 'i8*' type.
64 Value *emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
65 const TargetLibraryInfo *TLI);
67 /// Emit a call to the strncmp function to the builder.
68 Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
69 const DataLayout &DL, const TargetLibraryInfo *TLI);
71 /// Emit a call to the strcpy function to the builder, for the specified
72 /// pointer arguments.
73 Value *emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
74 const TargetLibraryInfo *TLI, StringRef Name = "strcpy");
76 /// Emit a call to the strncpy function to the builder, for the specified
77 /// pointer arguments and length.
78 Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
79 const TargetLibraryInfo *TLI, StringRef Name = "strncpy");
81 /// Emit a call to the __memcpy_chk function to the builder. This expects that
82 /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers.
83 Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
84 IRBuilder<> &B, const DataLayout &DL,
85 const TargetLibraryInfo *TLI);
87 /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
88 /// Val is an i32 value, and Len is an 'intptr_t' value.
89 Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
90 const DataLayout &DL, const TargetLibraryInfo *TLI);
92 /// Emit a call to the memcmp function.
93 Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
94 const DataLayout &DL, const TargetLibraryInfo *TLI);
96 /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This
97 /// function is known to take a single of type matching 'Op' and returns one
98 /// value with the same type. If 'Op' is a long double, 'l' is added as the
99 /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
100 Value *emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
101 const AttributeList &Attrs);
103 /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn,
104 /// depending of the type of Op.
105 Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
106 LibFunc DoubleFn, LibFunc FloatFn,
107 LibFunc LongDoubleFn, IRBuilder<> &B,
108 const AttributeList &Attrs);
110 /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
111 /// function is known to take type matching 'Op1' and 'Op2' and return one
112 /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
113 /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
114 Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
115 IRBuilder<> &B, const AttributeList &Attrs);
117 /// Emit a call to the putchar function. This assumes that Char is an integer.
118 Value *emitPutChar(Value *Char, IRBuilder<> &B, const TargetLibraryInfo *TLI);
120 /// Emit a call to the puts function. This assumes that Str is some pointer.
121 Value *emitPutS(Value *Str, IRBuilder<> &B, const TargetLibraryInfo *TLI);
123 /// Emit a call to the fputc function. This assumes that Char is an i32, and
124 /// File is a pointer to FILE.
125 Value *emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
126 const TargetLibraryInfo *TLI);
128 /// Emit a call to the fputc_unlocked function. This assumes that Char is an
129 /// i32, and File is a pointer to FILE.
130 Value *emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B,
131 const TargetLibraryInfo *TLI);
133 /// Emit a call to the fputs function. Str is required to be a pointer and
134 /// File is a pointer to FILE.
135 Value *emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
136 const TargetLibraryInfo *TLI);
138 /// Emit a call to the fputs_unlocked function. Str is required to be a
139 /// pointer and File is a pointer to FILE.
140 Value *emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B,
141 const TargetLibraryInfo *TLI);
143 /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
144 /// Size is an 'intptr_t', and File is a pointer to FILE.
145 Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
146 const DataLayout &DL, const TargetLibraryInfo *TLI);
148 /// Emit a call to the malloc function.
149 Value *emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL,
150 const TargetLibraryInfo *TLI);
152 /// Emit a call to the calloc function.
153 Value *emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
154 IRBuilder<> &B, const TargetLibraryInfo &TLI);
156 /// Emit a call to the fwrite_unlocked function. This assumes that Ptr is a
157 /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
158 Value *emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
159 IRBuilder<> &B, const DataLayout &DL,
160 const TargetLibraryInfo *TLI);
162 /// Emit a call to the fgetc_unlocked function. File is a pointer to FILE.
163 Value *emitFGetCUnlocked(Value *File, IRBuilder<> &B,
164 const TargetLibraryInfo *TLI);
166 /// Emit a call to the fgets_unlocked function. Str is required to be a
167 /// pointer, Size is an i32 and File is a pointer to FILE.
168 Value *emitFGetSUnlocked(Value *Str, Value *Size, Value *File, IRBuilder<> &B,
169 const TargetLibraryInfo *TLI);
171 /// Emit a call to the fread_unlocked function. This assumes that Ptr is a
172 /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
173 Value *emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
174 IRBuilder<> &B, const DataLayout &DL,
175 const TargetLibraryInfo *TLI);