LLVM API Documentation

BuildLibCalls.h
Go to the documentation of this file.
00001 //===- BuildLibCalls.h - Utility builder for libcalls -----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file exposes an interface to build some C language libcalls for
00011 // optimization passes that need to call the various functions.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
00016 #define LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
00017 
00018 #include "llvm/IR/IRBuilder.h"
00019 
00020 namespace llvm {
00021   class Value;
00022   class DataLayout;
00023   class TargetLibraryInfo;
00024 
00025   /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
00026   Value *CastToCStr(Value *V, IRBuilder<> &B);
00027 
00028   /// EmitStrLen - Emit a call to the strlen function to the builder, for the
00029   /// specified pointer.  Ptr is required to be some pointer type, and the
00030   /// return value has 'intptr_t' type.
00031   Value *EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
00032                     const TargetLibraryInfo *TLI);
00033 
00034   /// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
00035   /// specified pointer.  Ptr is required to be some pointer type, MaxLen must
00036   /// be of size_t type, and the return value has 'intptr_t' type.
00037   Value *EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
00038                      const DataLayout *TD, const TargetLibraryInfo *TLI);
00039 
00040   /// EmitStrChr - Emit a call to the strchr function to the builder, for the
00041   /// specified pointer and character.  Ptr is required to be some pointer type,
00042   /// and the return value has 'i8*' type.
00043   Value *EmitStrChr(Value *Ptr, char C, IRBuilder<> &B, const DataLayout *TD,
00044                     const TargetLibraryInfo *TLI);
00045 
00046   /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
00047   Value *EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
00048                      const DataLayout *TD, const TargetLibraryInfo *TLI);
00049 
00050   /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
00051   /// specified pointer arguments.
00052   Value *EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
00053                     const DataLayout *TD, const TargetLibraryInfo *TLI,
00054                     StringRef Name = "strcpy");
00055 
00056   /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
00057   /// specified pointer arguments and length.
00058   Value *EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
00059                      const DataLayout *TD, const TargetLibraryInfo *TLI,
00060                      StringRef Name = "strncpy");
00061 
00062   /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
00063   /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
00064   /// are pointers.
00065   Value *EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
00066                        IRBuilder<> &B, const DataLayout *TD,
00067                        const TargetLibraryInfo *TLI);
00068 
00069   /// EmitMemChr - Emit a call to the memchr function.  This assumes that Ptr is
00070   /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
00071   Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
00072                     const DataLayout *TD, const TargetLibraryInfo *TLI);
00073 
00074   /// EmitMemCmp - Emit a call to the memcmp function.
00075   Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
00076                     const DataLayout *TD, const TargetLibraryInfo *TLI);
00077 
00078   /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name'
00079   /// (e.g.  'floor').  This function is known to take a single of type matching
00080   /// 'Op' and returns one value with the same type.  If 'Op' is a long double,
00081   /// 'l' is added as the suffix of name, if 'Op' is a float, we add a 'f'
00082   /// suffix.
00083   Value *EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
00084                               const AttributeSet &Attrs);
00085 
00086   /// EmitUnaryFloatFnCall - Emit a call to the binary function named 'Name'
00087   /// (e.g. 'fmin').  This function is known to take type matching 'Op1' and
00088   /// 'Op2' and return one value with the same type.  If 'Op1/Op2' are long
00089   /// double, 'l' is added as the suffix of name, if 'Op1/Op2' are float, we
00090   /// add a 'f' suffix.
00091   Value *EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
00092                                   IRBuilder<> &B, const AttributeSet &Attrs);
00093 
00094   /// EmitPutChar - Emit a call to the putchar function.  This assumes that Char
00095   /// is an integer.
00096   Value *EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
00097                      const TargetLibraryInfo *TLI);
00098 
00099   /// EmitPutS - Emit a call to the puts function.  This assumes that Str is
00100   /// some pointer.
00101   Value *EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
00102                   const TargetLibraryInfo *TLI);
00103 
00104   /// EmitFPutC - Emit a call to the fputc function.  This assumes that Char is
00105   /// an i32, and File is a pointer to FILE.
00106   Value *EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
00107                    const DataLayout *TD, const TargetLibraryInfo *TLI);
00108 
00109   /// EmitFPutS - Emit a call to the puts function.  Str is required to be a
00110   /// pointer and File is a pointer to FILE.
00111   Value *EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, const DataLayout *TD,
00112                    const TargetLibraryInfo *TLI);
00113 
00114   /// EmitFWrite - Emit a call to the fwrite function.  This assumes that Ptr is
00115   /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
00116   Value *EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
00117                     const DataLayout *TD, const TargetLibraryInfo *TLI);
00118 
00119   /// SimplifyFortifiedLibCalls - Helper class for folding checked library
00120   /// calls (e.g. __strcpy_chk) into their unchecked counterparts.
00121   class SimplifyFortifiedLibCalls {
00122   protected:
00123     CallInst *CI;
00124     virtual void replaceCall(Value *With) = 0;
00125     virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
00126                             bool isString) const = 0;
00127 
00128   public:
00129     virtual ~SimplifyFortifiedLibCalls();
00130     bool fold(CallInst *CI, const DataLayout *TD, const TargetLibraryInfo *TLI);
00131   };
00132 }
00133 
00134 #endif