Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:42

0001 //===- BuildLibCalls.h - Utility builder for libcalls -----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file exposes an interface to build some C language libcalls for
0010 // optimization passes that need to call the various functions.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
0015 #define LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
0016 
0017 #include "llvm/Analysis/TargetLibraryInfo.h"
0018 
0019 namespace llvm {
0020   class Value;
0021   class DataLayout;
0022   class IRBuilderBase;
0023 
0024   /// Analyze the name and prototype of the given function and set any
0025   /// applicable attributes. Note that this merely helps optimizations on an
0026   /// already existing function but does not consider mandatory attributes.
0027   ///
0028   /// If the library function is unavailable, this doesn't modify it.
0029   ///
0030   /// Returns true if any attributes were set and false otherwise.
0031   bool inferNonMandatoryLibFuncAttrs(Module *M, StringRef Name,
0032                                      const TargetLibraryInfo &TLI);
0033   bool inferNonMandatoryLibFuncAttrs(Function &F, const TargetLibraryInfo &TLI);
0034 
0035   /// Calls getOrInsertFunction() and then makes sure to add mandatory
0036   /// argument attributes.
0037   FunctionCallee getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI,
0038                                     LibFunc TheLibFunc, FunctionType *T,
0039                                     AttributeList AttributeList);
0040   FunctionCallee getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI,
0041                                     LibFunc TheLibFunc, FunctionType *T);
0042   template <typename... ArgsTy>
0043   FunctionCallee getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI,
0044                                LibFunc TheLibFunc, AttributeList AttributeList,
0045                                Type *RetTy, ArgsTy... Args) {
0046     SmallVector<Type*, sizeof...(ArgsTy)> ArgTys{Args...};
0047     return getOrInsertLibFunc(M, TLI, TheLibFunc,
0048                               FunctionType::get(RetTy, ArgTys, false),
0049                               AttributeList);
0050   }
0051   /// Same as above, but without the attributes.
0052   template <typename... ArgsTy>
0053   FunctionCallee getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI,
0054                              LibFunc TheLibFunc, Type *RetTy, ArgsTy... Args) {
0055     return getOrInsertLibFunc(M, TLI, TheLibFunc, AttributeList{}, RetTy,
0056                               Args...);
0057   }
0058   // Avoid an incorrect ordering that'd otherwise compile incorrectly.
0059   template <typename... ArgsTy>
0060   FunctionCallee
0061   getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI,
0062                      LibFunc TheLibFunc, AttributeList AttributeList,
0063                      FunctionType *Invalid, ArgsTy... Args) = delete;
0064 
0065   // Handle -mregparm for the given function.
0066   // Note that this function is a rough approximation that only works for simple
0067   // function signatures; it does not apply other relevant attributes for
0068   // function signatures, including sign/zero-extension for arguments and return
0069   // values.
0070   void markRegisterParameterAttributes(Function *F);
0071 
0072   /// Check whether the library function is available on target and also that
0073   /// it in the current Module is a Function with the right type.
0074   bool isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI,
0075                           LibFunc TheLibFunc);
0076   bool isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI,
0077                           StringRef Name);
0078 
0079   /// Check whether the overloaded floating point function
0080   /// corresponding to \a Ty is available.
0081   bool hasFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty,
0082                   LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn);
0083 
0084   /// Get the name of the overloaded floating point function
0085   /// corresponding to \a Ty. Return the LibFunc in \a TheLibFunc.
0086   StringRef getFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty,
0087                        LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn,
0088                        LibFunc &TheLibFunc);
0089 
0090   /// Emit a call to the strlen function to the builder, for the specified
0091   /// pointer. Ptr is required to be some pointer type, and the return value has
0092   /// 'size_t' type.
0093   Value *emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL,
0094                     const TargetLibraryInfo *TLI);
0095 
0096   /// Emit a call to the strdup function to the builder, for the specified
0097   /// pointer. Ptr is required to be some pointer type, and the return value has
0098   /// 'i8*' type.
0099   Value *emitStrDup(Value *Ptr, IRBuilderBase &B, const TargetLibraryInfo *TLI);
0100 
0101   /// Emit a call to the strchr function to the builder, for the specified
0102   /// pointer and character. Ptr is required to be some pointer type, and the
0103   /// return value has 'i8*' type.
0104   Value *emitStrChr(Value *Ptr, char C, IRBuilderBase &B,
0105                     const TargetLibraryInfo *TLI);
0106 
0107   /// Emit a call to the strncmp function to the builder.
0108   Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
0109                      const DataLayout &DL, const TargetLibraryInfo *TLI);
0110 
0111   /// Emit a call to the strcpy function to the builder, for the specified
0112   /// pointer arguments.
0113   Value *emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B,
0114                     const TargetLibraryInfo *TLI);
0115 
0116   /// Emit a call to the stpcpy function to the builder, for the specified
0117   /// pointer arguments.
0118   Value *emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B,
0119                     const TargetLibraryInfo *TLI);
0120 
0121   /// Emit a call to the strncpy function to the builder, for the specified
0122   /// pointer arguments and length.
0123   Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
0124                      const TargetLibraryInfo *TLI);
0125 
0126   /// Emit a call to the stpncpy function to the builder, for the specified
0127   /// pointer arguments and length.
0128   Value *emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
0129                      const TargetLibraryInfo *TLI);
0130 
0131   /// Emit a call to the __memcpy_chk function to the builder. This expects that
0132   /// the Len and ObjSize have type 'size_t' and Dst/Src are pointers.
0133   Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
0134                        IRBuilderBase &B, const DataLayout &DL,
0135                        const TargetLibraryInfo *TLI);
0136 
0137   /// Emit a call to the mempcpy function.
0138   Value *emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
0139                      const DataLayout &DL, const TargetLibraryInfo *TLI);
0140 
0141   /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
0142   /// Val is an 'int' value, and Len is an 'size_t' value.
0143   Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B,
0144                     const DataLayout &DL, const TargetLibraryInfo *TLI);
0145 
0146   /// Emit a call to the memrchr function, analogously to emitMemChr.
0147   Value *emitMemRChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B,
0148                     const DataLayout &DL, const TargetLibraryInfo *TLI);
0149 
0150   /// Emit a call to the memcmp function.
0151   Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
0152                     const DataLayout &DL, const TargetLibraryInfo *TLI);
0153 
0154   /// Emit a call to the bcmp function.
0155   Value *emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
0156                   const DataLayout &DL, const TargetLibraryInfo *TLI);
0157 
0158   /// Emit a call to the memccpy function.
0159   Value *emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
0160                      IRBuilderBase &B, const TargetLibraryInfo *TLI);
0161 
0162   /// Emit a call to the snprintf function.
0163   Value *emitSNPrintf(Value *Dest, Value *Size, Value *Fmt,
0164                       ArrayRef<Value *> Args, IRBuilderBase &B,
0165                       const TargetLibraryInfo *TLI);
0166 
0167   /// Emit a call to the sprintf function.
0168   Value *emitSPrintf(Value *Dest, Value *Fmt, ArrayRef<Value *> VariadicArgs,
0169                      IRBuilderBase &B, const TargetLibraryInfo *TLI);
0170 
0171   /// Emit a call to the strcat function.
0172   Value *emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B,
0173                     const TargetLibraryInfo *TLI);
0174 
0175   /// Emit a call to the strlcpy function.
0176   Value *emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
0177                      const TargetLibraryInfo *TLI);
0178 
0179   /// Emit a call to the strlcat function.
0180   Value *emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
0181                      const TargetLibraryInfo *TLI);
0182 
0183   /// Emit a call to the strncat function.
0184   Value *emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
0185                      const TargetLibraryInfo *TLI);
0186 
0187   /// Emit a call to the vsnprintf function.
0188   Value *emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList,
0189                        IRBuilderBase &B, const TargetLibraryInfo *TLI);
0190 
0191   /// Emit a call to the vsprintf function.
0192   Value *emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B,
0193                       const TargetLibraryInfo *TLI);
0194 
0195   /// Emit a call to the unary function named 'Name' (e.g.  'floor'). This
0196   /// function is known to take a single of type matching 'Op' and returns one
0197   /// value with the same type. If 'Op' is a long double, 'l' is added as the
0198   /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
0199   Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
0200                               StringRef Name, IRBuilderBase &B,
0201                               const AttributeList &Attrs);
0202 
0203   /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn,
0204   /// depending of the type of Op.
0205   Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
0206                               LibFunc DoubleFn, LibFunc FloatFn,
0207                               LibFunc LongDoubleFn, IRBuilderBase &B,
0208                               const AttributeList &Attrs);
0209 
0210   /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
0211   /// function is known to take type matching 'Op1' and 'Op2' and return one
0212   /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
0213   /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
0214   Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2,
0215                                const TargetLibraryInfo *TLI,
0216                                StringRef Name, IRBuilderBase &B,
0217                                const AttributeList &Attrs);
0218 
0219   /// Emit a call to the binary function DoubleFn, FloatFn or LongDoubleFn,
0220   /// depending of the type of Op1.
0221   Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2,
0222                                const TargetLibraryInfo *TLI, LibFunc DoubleFn,
0223                                LibFunc FloatFn, LibFunc LongDoubleFn,
0224                                IRBuilderBase &B, const AttributeList &Attrs);
0225 
0226   /// Emit a call to the putchar function. This assumes that Char is an 'int'.
0227   Value *emitPutChar(Value *Char, IRBuilderBase &B,
0228                      const TargetLibraryInfo *TLI);
0229 
0230   /// Emit a call to the puts function. This assumes that Str is some pointer.
0231   Value *emitPutS(Value *Str, IRBuilderBase &B, const TargetLibraryInfo *TLI);
0232 
0233   /// Emit a call to the fputc function. This assumes that Char is an 'int', and
0234   /// File is a pointer to FILE.
0235   Value *emitFPutC(Value *Char, Value *File, IRBuilderBase &B,
0236                    const TargetLibraryInfo *TLI);
0237 
0238   /// Emit a call to the fputs function. Str is required to be a pointer and
0239   /// File is a pointer to FILE.
0240   Value *emitFPutS(Value *Str, Value *File, IRBuilderBase &B,
0241                    const TargetLibraryInfo *TLI);
0242 
0243   /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
0244   /// Size is an 'size_t', and File is a pointer to FILE.
0245   Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B,
0246                     const DataLayout &DL, const TargetLibraryInfo *TLI);
0247 
0248   /// Emit a call to the malloc function.
0249   Value *emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL,
0250                     const TargetLibraryInfo *TLI);
0251 
0252   /// Emit a call to the calloc function.
0253   Value *emitCalloc(Value *Num, Value *Size, IRBuilderBase &B,
0254                     const TargetLibraryInfo &TLI, unsigned AddrSpace);
0255 
0256   /// Emit a call to the hot/cold operator new function.
0257   Value *emitHotColdNew(Value *Num, IRBuilderBase &B,
0258                         const TargetLibraryInfo *TLI, LibFunc NewFunc,
0259                         uint8_t HotCold);
0260   Value *emitHotColdNewNoThrow(Value *Num, Value *NoThrow, IRBuilderBase &B,
0261                                const TargetLibraryInfo *TLI, LibFunc NewFunc,
0262                                uint8_t HotCold);
0263   Value *emitHotColdNewAligned(Value *Num, Value *Align, IRBuilderBase &B,
0264                                const TargetLibraryInfo *TLI, LibFunc NewFunc,
0265                                uint8_t HotCold);
0266   Value *emitHotColdNewAlignedNoThrow(Value *Num, Value *Align, Value *NoThrow,
0267                                       IRBuilderBase &B,
0268                                       const TargetLibraryInfo *TLI,
0269                                       LibFunc NewFunc, uint8_t HotCold);
0270   Value *emitHotColdSizeReturningNew(Value *Num, IRBuilderBase &B,
0271                                      const TargetLibraryInfo *TLI,
0272                                      LibFunc NewFunc, uint8_t HotCold);
0273   Value *emitHotColdSizeReturningNewAligned(Value *Num, Value *Align,
0274                                             IRBuilderBase &B,
0275                                             const TargetLibraryInfo *TLI,
0276                                             LibFunc NewFunc, uint8_t HotCold);
0277 }
0278 
0279 #endif