LLVM  9.0.0svn
BuildLibCalls.h
Go to the documentation of this file.
1 //===- BuildLibCalls.h - Utility builder for libcalls -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file exposes an interface to build some C language libcalls for
10 // optimization passes that need to call the various functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
15 #define LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
16 
18 #include "llvm/IR/IRBuilder.h"
19 
20 namespace llvm {
21  class Value;
22  class DataLayout;
23  class TargetLibraryInfo;
24 
25  /// Analyze the name and prototype of the given function and set any
26  /// applicable attributes.
27  /// If the library function is unavailable, this doesn't modify it.
28  ///
29  /// Returns true if any attributes were set and false otherwise.
30  bool inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI);
31  bool inferLibFuncAttributes(Module *M, StringRef Name, const TargetLibraryInfo &TLI);
32 
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);
38 
39  /// Get the name of the overloaded unary floating point function
40  /// corresponding to \a Ty.
41  StringRef getUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
42  LibFunc DoubleFn, LibFunc FloatFn,
43  LibFunc LongDoubleFn);
44 
45  /// Return V if it is an i8*, otherwise cast it to i8*.
46  Value *castToCStr(Value *V, IRBuilder<> &B);
47 
48  /// Emit a call to the strlen function to the builder, for the specified
49  /// pointer. Ptr is required to be some pointer type, and the return value has
50  /// 'intptr_t' type.
51  Value *emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
52  const TargetLibraryInfo *TLI);
53 
54  /// Emit a call to the strnlen function to the builder, for the specified
55  /// pointer. Ptr is required to be some pointer type, MaxLen must be of size_t
56  /// type, and the return value has 'intptr_t' type.
57  Value *emitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
58  const DataLayout &DL, const TargetLibraryInfo *TLI);
59 
60  /// Emit a call to the strchr function to the builder, for the specified
61  /// pointer and character. Ptr is required to be some pointer type, and the
62  /// return value has 'i8*' type.
63  Value *emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
64  const TargetLibraryInfo *TLI);
65 
66  /// Emit a call to the strncmp function to the builder.
67  Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
68  const DataLayout &DL, const TargetLibraryInfo *TLI);
69 
70  /// Emit a call to the strcpy function to the builder, for the specified
71  /// pointer arguments.
72  Value *emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
73  const TargetLibraryInfo *TLI, StringRef Name = "strcpy");
74 
75  /// Emit a call to the strncpy function to the builder, for the specified
76  /// pointer arguments and length.
77  Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
78  const TargetLibraryInfo *TLI, StringRef Name = "strncpy");
79 
80  /// Emit a call to the __memcpy_chk function to the builder. This expects that
81  /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers.
82  Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
83  IRBuilder<> &B, const DataLayout &DL,
84  const TargetLibraryInfo *TLI);
85 
86  /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
87  /// Val is an i32 value, and Len is an 'intptr_t' value.
88  Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
89  const DataLayout &DL, const TargetLibraryInfo *TLI);
90 
91  /// Emit a call to the memcmp function.
92  Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
93  const DataLayout &DL, const TargetLibraryInfo *TLI);
94 
95  /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This
96  /// function is known to take a single of type matching 'Op' and returns one
97  /// value with the same type. If 'Op' is a long double, 'l' is added as the
98  /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
99  Value *emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
100  const AttributeList &Attrs);
101 
102  /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn,
103  /// depending of the type of Op.
104  Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
105  LibFunc DoubleFn, LibFunc FloatFn,
106  LibFunc LongDoubleFn, IRBuilder<> &B,
107  const AttributeList &Attrs);
108 
109  /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
110  /// function is known to take type matching 'Op1' and 'Op2' and return one
111  /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
112  /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
113  Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
114  IRBuilder<> &B, const AttributeList &Attrs);
115 
116  /// Emit a call to the putchar function. This assumes that Char is an integer.
117  Value *emitPutChar(Value *Char, IRBuilder<> &B, const TargetLibraryInfo *TLI);
118 
119  /// Emit a call to the puts function. This assumes that Str is some pointer.
120  Value *emitPutS(Value *Str, IRBuilder<> &B, const TargetLibraryInfo *TLI);
121 
122  /// Emit a call to the fputc function. This assumes that Char is an i32, and
123  /// File is a pointer to FILE.
124  Value *emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
125  const TargetLibraryInfo *TLI);
126 
127  /// Emit a call to the fputc_unlocked function. This assumes that Char is an
128  /// i32, and File is a pointer to FILE.
129  Value *emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B,
130  const TargetLibraryInfo *TLI);
131 
132  /// Emit a call to the fputs function. Str is required to be a pointer and
133  /// File is a pointer to FILE.
134  Value *emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
135  const TargetLibraryInfo *TLI);
136 
137  /// Emit a call to the fputs_unlocked function. Str is required to be a
138  /// pointer and File is a pointer to FILE.
139  Value *emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B,
140  const TargetLibraryInfo *TLI);
141 
142  /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
143  /// Size is an 'intptr_t', and File is a pointer to FILE.
144  Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
145  const DataLayout &DL, const TargetLibraryInfo *TLI);
146 
147  /// Emit a call to the malloc function.
148  Value *emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL,
149  const TargetLibraryInfo *TLI);
150 
151  /// Emit a call to the calloc function.
152  Value *emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
153  IRBuilder<> &B, const TargetLibraryInfo &TLI);
154 
155  /// Emit a call to the fwrite_unlocked function. This assumes that Ptr is a
156  /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
157  Value *emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
158  IRBuilder<> &B, const DataLayout &DL,
159  const TargetLibraryInfo *TLI);
160 
161  /// Emit a call to the fgetc_unlocked function. File is a pointer to FILE.
162  Value *emitFGetCUnlocked(Value *File, IRBuilder<> &B,
163  const TargetLibraryInfo *TLI);
164 
165  /// Emit a call to the fgets_unlocked function. Str is required to be a
166  /// pointer, Size is an i32 and File is a pointer to FILE.
167  Value *emitFGetSUnlocked(Value *Str, Value *Size, Value *File, IRBuilder<> &B,
168  const TargetLibraryInfo *TLI);
169 
170  /// Emit a call to the fread_unlocked function. This assumes that Ptr is a
171  /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
172  Value *emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
173  IRBuilder<> &B, const DataLayout &DL,
174  const TargetLibraryInfo *TLI);
175 }
176 
177 #endif
uint64_t CallInst * C
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:48
This class represents lattice values for constants.
Definition: AllocatorList.h:23
F(f)
Value * emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, const TargetLibraryInfo *TLI, StringRef Name="strncpy")
Emit a call to the strncpy function to the builder, for the specified pointer arguments and length...
Value * emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fputs_unlocked function.
Value * emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the malloc function.
StringRef getUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn)
Get the name of the overloaded unary floating point function corresponding to Ty. ...
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Value * emitPutChar(Value *Char, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the putchar function. This assumes that Char is an integer.
Value * emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, const AttributeList &Attrs)
Emit a call to the unary function named &#39;Name&#39; (e.g.
Value * emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fputc_unlocked function.
Value * emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the __memcpy_chk function to the builder.
Value * emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, const TargetLibraryInfo *TLI, StringRef Name="strcpy")
Emit a call to the strcpy function to the builder, for the specified pointer arguments.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
Value * emitFPutS(Value *Str, Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fputs function.
bool inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI)
Analyze the name and prototype of the given function and set any applicable attributes.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Value * castToCStr(Value *V, IRBuilder<> &B)
Return V if it is an i8*, otherwise cast it to i8*.
Value * emitStrChr(Value *Ptr, char C, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strchr function to the builder, for the specified pointer and character.
Value * emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the fwrite_unlocked function.
Value * emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the strlen function to the builder, for the specified pointer.
Value * emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, IRBuilder<> &B, const AttributeList &Attrs)
Emit a call to the binary function named &#39;Name&#39; (e.g.
Value * emitFGetCUnlocked(Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fgetc_unlocked function. File is a pointer to FILE.
Value * emitFPutC(Value *Char, Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fputc function.
Value * emitFGetSUnlocked(Value *Str, Value *Size, Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fgets_unlocked function.
Value * emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the strncmp function to the builder.
#define N
Value * emitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the strnlen function to the builder, for the specified pointer.
uint32_t Size
Definition: Profile.cpp:46
Value * emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the fwrite function.
Value * emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memcmp function.
Value * emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the fread_unlocked function.
Value * emitPutS(Value *Str, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the puts function. This assumes that Str is some pointer.
Value * emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memchr function.
Value * emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs, IRBuilder<> &B, const TargetLibraryInfo &TLI)
Emit a call to the calloc function.
bool hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn)
Check whether the overloaded unary floating point function corresponding to Ty is available...