1//===- BuildLibCalls.h - Utility builder for libcalls -----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file exposes an interface to build some C language libcalls for
11// optimization passes that need to call the various functions.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
16#define LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
17
18#include "llvm/IR/IRBuilder.h"
19
20namespace llvm {
21  class Value;
22  class DataLayout;
23  class TargetLibraryInfo;
24
25  /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
26  Value *CastToCStr(Value *V, IRBuilder<> &B);
27
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 DataLayout *TD,
32                    const TargetLibraryInfo *TLI);
33
34  /// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
35  /// specified pointer.  Ptr is required to be some pointer type, MaxLen must
36  /// be of size_t type, and the return value has 'intptr_t' type.
37  Value *EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
38                     const DataLayout *TD, const TargetLibraryInfo *TLI);
39
40  /// EmitStrChr - Emit a call to the strchr function to the builder, for the
41  /// specified pointer and character.  Ptr is required to be some pointer type,
42  /// and the return value has 'i8*' type.
43  Value *EmitStrChr(Value *Ptr, char C, IRBuilder<> &B, const DataLayout *TD,
44                    const TargetLibraryInfo *TLI);
45
46  /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
47  Value *EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
48                     const DataLayout *TD, const TargetLibraryInfo *TLI);
49
50  /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
51  /// specified pointer arguments.
52  Value *EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
53                    const DataLayout *TD, const TargetLibraryInfo *TLI,
54                    StringRef Name = "strcpy");
55
56  /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
57  /// specified pointer arguments and length.
58  Value *EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
59                     const DataLayout *TD, const TargetLibraryInfo *TLI,
60                     StringRef Name = "strncpy");
61
62  /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
63  /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
64  /// are pointers.
65  Value *EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
66                       IRBuilder<> &B, const DataLayout *TD,
67                       const TargetLibraryInfo *TLI);
68
69  /// EmitMemChr - Emit a call to the memchr function.  This assumes that Ptr is
70  /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
71  Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
72                    const DataLayout *TD, const TargetLibraryInfo *TLI);
73
74  /// EmitMemCmp - Emit a call to the memcmp function.
75  Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
76                    const DataLayout *TD, const TargetLibraryInfo *TLI);
77
78  /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name'
79  /// (e.g.  'floor').  This function is known to take a single of type matching
80  /// 'Op' and returns one value with the same type.  If 'Op' is a long double,
81  /// 'l' is added as the suffix of name, if 'Op' is a float, we add a 'f'
82  /// suffix.
83  Value *EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
84                              const AttributeSet &Attrs);
85
86  /// EmitUnaryFloatFnCall - Emit a call to the binary function named 'Name'
87  /// (e.g. 'fmin').  This function is known to take type matching 'Op1' and
88  /// 'Op2' and return one value with the same type.  If 'Op1/Op2' are long
89  /// double, 'l' is added as the suffix of name, if 'Op1/Op2' are float, we
90  /// add a 'f' suffix.
91  Value *EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
92                                  IRBuilder<> &B, const AttributeSet &Attrs);
93
94  /// EmitPutChar - Emit a call to the putchar function.  This assumes that Char
95  /// is an integer.
96  Value *EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
97                     const TargetLibraryInfo *TLI);
98
99  /// EmitPutS - Emit a call to the puts function.  This assumes that Str is
100  /// some pointer.
101  Value *EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
102                  const TargetLibraryInfo *TLI);
103
104  /// EmitFPutC - Emit a call to the fputc function.  This assumes that Char is
105  /// an i32, and File is a pointer to FILE.
106  Value *EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
107                   const DataLayout *TD, const TargetLibraryInfo *TLI);
108
109  /// EmitFPutS - Emit a call to the puts function.  Str is required to be a
110  /// pointer and File is a pointer to FILE.
111  Value *EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, const DataLayout *TD,
112                   const TargetLibraryInfo *TLI);
113
114  /// EmitFWrite - Emit a call to the fwrite function.  This assumes that Ptr is
115  /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
116  Value *EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
117                    const DataLayout *TD, const TargetLibraryInfo *TLI);
118
119  /// SimplifyFortifiedLibCalls - Helper class for folding checked library
120  /// calls (e.g. __strcpy_chk) into their unchecked counterparts.
121  class SimplifyFortifiedLibCalls {
122  protected:
123    CallInst *CI;
124    virtual void replaceCall(Value *With) = 0;
125    virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
126                            bool isString) const = 0;
127
128  public:
129    virtual ~SimplifyFortifiedLibCalls();
130    bool fold(CallInst *CI, const DataLayout *TD, const TargetLibraryInfo *TLI);
131  };
132}
133
134#endif
135