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 /// Return V if it is an i8*, otherwise cast it to i8*.
41 Value *castToCStr(Value *V, IRBuilder<> &B);
43 /// Emit a call to the strlen function to the builder, for the specified
44 /// pointer. Ptr is required to be some pointer type, and the return value has
46 Value *emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
47 const TargetLibraryInfo *TLI);
49 /// Emit a call to the strnlen function to the builder, for the specified
50 /// pointer. Ptr is required to be some pointer type, MaxLen must be of size_t
51 /// type, and the return value has 'intptr_t' type.
52 Value *emitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
53 const DataLayout &DL, const TargetLibraryInfo *TLI);
55 /// Emit a call to the strchr function to the builder, for the specified
56 /// pointer and character. Ptr is required to be some pointer type, and the
57 /// return value has 'i8*' type.
58 Value *emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
59 const TargetLibraryInfo *TLI);
61 /// Emit a call to the strncmp function to the builder.
62 Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
63 const DataLayout &DL, const TargetLibraryInfo *TLI);
65 /// Emit a call to the strcpy function to the builder, for the specified
66 /// pointer arguments.
67 Value *emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
68 const TargetLibraryInfo *TLI, StringRef Name = "strcpy");
70 /// Emit a call to the strncpy function to the builder, for the specified
71 /// pointer arguments and length.
72 Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
73 const TargetLibraryInfo *TLI, StringRef Name = "strncpy");
75 /// Emit a call to the __memcpy_chk function to the builder. This expects that
76 /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers.
77 Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
78 IRBuilder<> &B, const DataLayout &DL,
79 const TargetLibraryInfo *TLI);
81 /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
82 /// Val is an i32 value, and Len is an 'intptr_t' value.
83 Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
84 const DataLayout &DL, const TargetLibraryInfo *TLI);
86 /// Emit a call to the memcmp function.
87 Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
88 const DataLayout &DL, const TargetLibraryInfo *TLI);
90 /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This
91 /// function is known to take a single of type matching 'Op' and returns one
92 /// value with the same type. If 'Op' is a long double, 'l' is added as the
93 /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
94 Value *emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
95 const AttributeList &Attrs);
97 /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
98 /// function is known to take type matching 'Op1' and 'Op2' and return one
99 /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
100 /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
101 Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
102 IRBuilder<> &B, const AttributeList &Attrs);
104 /// Emit a call to the putchar function. This assumes that Char is an integer.
105 Value *emitPutChar(Value *Char, IRBuilder<> &B, const TargetLibraryInfo *TLI);
107 /// Emit a call to the puts function. This assumes that Str is some pointer.
108 Value *emitPutS(Value *Str, IRBuilder<> &B, const TargetLibraryInfo *TLI);
110 /// Emit a call to the fputc function. This assumes that Char is an i32, and
111 /// File is a pointer to FILE.
112 Value *emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
113 const TargetLibraryInfo *TLI);
115 /// Emit a call to the fputc_unlocked function. This assumes that Char is an
116 /// i32, and File is a pointer to FILE.
117 Value *emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B,
118 const TargetLibraryInfo *TLI);
120 /// Emit a call to the fputs function. Str is required to be a pointer and
121 /// File is a pointer to FILE.
122 Value *emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
123 const TargetLibraryInfo *TLI);
125 /// Emit a call to the fputs_unlocked function. Str is required to be a
126 /// pointer and File is a pointer to FILE.
127 Value *emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B,
128 const TargetLibraryInfo *TLI);
130 /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
131 /// Size is an 'intptr_t', and File is a pointer to FILE.
132 Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
133 const DataLayout &DL, const TargetLibraryInfo *TLI);
135 /// Emit a call to the malloc function.
136 Value *emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL,
137 const TargetLibraryInfo *TLI);
139 /// Emit a call to the calloc function.
140 Value *emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
141 IRBuilder<> &B, const TargetLibraryInfo &TLI);
143 /// Emit a call to the fwrite_unlocked function. This assumes that Ptr is a
144 /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
145 Value *emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
146 IRBuilder<> &B, const DataLayout &DL,
147 const TargetLibraryInfo *TLI);
149 /// Emit a call to the fgetc_unlocked function. File is a pointer to FILE.
150 Value *emitFGetCUnlocked(Value *File, IRBuilder<> &B,
151 const TargetLibraryInfo *TLI);
153 /// Emit a call to the fgets_unlocked function. Str is required to be a
154 /// pointer, Size is an i32 and File is a pointer to FILE.
155 Value *emitFGetSUnlocked(Value *Str, Value *Size, Value *File, IRBuilder<> &B,
156 const TargetLibraryInfo *TLI);
158 /// Emit a call to the fread_unlocked function. This assumes that Ptr is a
159 /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
160 Value *emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
161 IRBuilder<> &B, const DataLayout &DL,
162 const TargetLibraryInfo *TLI);