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 TRANSFORMS_UTILS_BUILDLIBCALLS_H
16 #define TRANSFORMS_UTILS_BUILDLIBCALLS_H
18 #include "llvm/Support/IRBuilder.h"
24 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
25 Value *CastToCStr(Value *V, IRBuilder<> &B);
27 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
28 /// specified pointer. Ptr is required to be some pointer type, and the
29 /// return value has 'intptr_t' type.
30 Value *EmitStrLen(Value *Ptr, IRBuilder<> &B, const TargetData *TD);
32 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
33 /// specified pointer and character. Ptr is required to be some pointer type,
34 /// and the return value has 'i8*' type.
35 Value *EmitStrChr(Value *Ptr, char C, IRBuilder<> &B, const TargetData *TD);
37 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
38 /// specified pointer arguments.
39 Value *EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
40 const TargetData *TD, StringRef Name = "strcpy");
42 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
43 /// specified pointer arguments and length.
44 Value *EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
45 const TargetData *TD, StringRef Name = "strncpy");
47 /// EmitMemCpy - Emit a call to the memcpy function to the builder. This
48 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
49 Value *EmitMemCpy(Value *Dst, Value *Src, Value *Len, unsigned Align,
50 bool isVolatile, IRBuilder<> &B, const TargetData *TD);
52 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
53 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
55 Value *EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
56 IRBuilder<> &B, const TargetData *TD);
58 /// EmitMemMove - Emit a call to the memmove function to the builder. This
59 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
60 Value *EmitMemMove(Value *Dst, Value *Src, Value *Len, unsigned Align,
61 bool isVolatile, IRBuilder<> &B, const TargetData *TD);
63 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
64 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
65 Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
66 const TargetData *TD);
68 /// EmitMemCmp - Emit a call to the memcmp function.
69 Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
70 const TargetData *TD);
72 /// EmitMemSet - Emit a call to the memset function
73 Value *EmitMemSet(Value *Dst, Value *Val, Value *Len, bool isVolatile,
74 IRBuilder<> &B, const TargetData *TD);
76 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name'
77 /// (e.g. 'floor'). This function is known to take a single of type matching
78 /// 'Op' and returns one value with the same type. If 'Op' is a long double,
79 /// 'l' is added as the suffix of name, if 'Op' is a float, we add a 'f'
81 Value *EmitUnaryFloatFnCall(Value *Op, const char *Name, IRBuilder<> &B,
82 const AttrListPtr &Attrs);
84 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
86 Value *EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD);
88 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
90 void EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD);
92 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
93 /// an i32, and File is a pointer to FILE.
94 void EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
95 const TargetData *TD);
97 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
98 /// pointer and File is a pointer to FILE.
99 void EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, const TargetData *TD);
101 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
102 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
103 void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
104 const TargetData *TD);
106 /// SimplifyFortifiedLibCalls - Helper class for folding checked library
107 /// calls (e.g. __strcpy_chk) into their unchecked counterparts.
108 class SimplifyFortifiedLibCalls {
111 virtual void replaceCall(Value *With) = 0;
112 virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
113 bool isString) const = 0;
115 virtual ~SimplifyFortifiedLibCalls();
116 bool fold(CallInst *CI, const TargetData *TD);