29 #define DEBUG_TYPE "build-libcalls" 33 STATISTIC(NumReadNone,
"Number of functions inferred as readnone");
34 STATISTIC(NumReadOnly,
"Number of functions inferred as readonly");
35 STATISTIC(NumArgMemOnly,
"Number of functions inferred as argmemonly");
36 STATISTIC(NumNoUnwind,
"Number of functions inferred as nounwind");
37 STATISTIC(NumNoCapture,
"Number of arguments inferred as nocapture");
38 STATISTIC(NumReadOnlyArg,
"Number of arguments inferred as readonly");
39 STATISTIC(NumNoAlias,
"Number of function returns inferred as noalias");
40 STATISTIC(NumNonNull,
"Number of function returns inferred as nonnull returns");
41 STATISTIC(NumReturnedArg,
"Number of arguments inferred as returned");
109 "nonnull applies only to pointers");
152 bool Changed =
false;
160 switch (TheLibFunc) {
169 case LibFunc_strrchr:
176 case LibFunc_strtoul:
177 case LibFunc_strtoll:
178 case LibFunc_strtold:
179 case LibFunc_strtoull:
185 case LibFunc_strncpy:
190 case LibFunc_strncat:
194 case LibFunc_stpncpy:
199 case LibFunc_strxfrm:
207 case LibFunc_strncmp:
208 case LibFunc_strcspn:
209 case LibFunc_strcoll:
210 case LibFunc_strcasecmp:
211 case LibFunc_strncasecmp:
218 case LibFunc_strpbrk:
224 case LibFunc_strtok_r:
235 case LibFunc_setvbuf:
240 case LibFunc_strndup:
247 case LibFunc_statvfs:
260 case LibFunc_sprintf:
267 case LibFunc_snprintf:
274 case LibFunc_setitimer:
296 case LibFunc_memrchr:
314 case LibFunc_memmove:
320 case LibFunc_mempcpy:
321 case LibFunc_memccpy:
328 case LibFunc_memcpy_chk:
331 case LibFunc_memalign:
343 case LibFunc_realloc:
358 case LibFunc_realpath:
370 case LibFunc_readlink:
407 case LibFunc_ctermid:
408 case LibFunc_clearerr:
409 case LibFunc_closedir:
445 case LibFunc_fgetc_unlocked:
451 case LibFunc_fsetpos:
452 case LibFunc_flockfile:
453 case LibFunc_funlockfile:
454 case LibFunc_ftrylockfile:
464 case LibFunc_fputc_unlocked:
469 case LibFunc_fstatvfs:
474 case LibFunc_fgets_unlocked:
479 case LibFunc_fread_unlocked:
485 case LibFunc_fwrite_unlocked:
492 case LibFunc_fputs_unlocked:
499 case LibFunc_fprintf:
505 case LibFunc_fgetpos:
511 case LibFunc_getlogin_r:
512 case LibFunc_getc_unlocked:
522 case LibFunc_getchar:
523 case LibFunc_getchar_unlocked:
526 case LibFunc_getitimer:
530 case LibFunc_getpwnam:
548 case LibFunc_unsetenv:
562 case LibFunc_putc_unlocked:
582 case LibFunc_putchar:
583 case LibFunc_putchar_unlocked:
603 case LibFunc_vsscanf:
610 case LibFunc_vfscanf:
620 case LibFunc_vprintf:
625 case LibFunc_vfprintf:
626 case LibFunc_vsprintf:
632 case LibFunc_vsnprintf:
643 case LibFunc_opendir:
649 case LibFunc_tmpfile:
679 case LibFunc_dunder_strdup:
680 case LibFunc_dunder_strndup:
686 case LibFunc_dunder_strtok_r:
691 case LibFunc_under_IO_getc:
695 case LibFunc_under_IO_putc:
699 case LibFunc_dunder_isoc99_scanf:
705 case LibFunc_lstat64:
706 case LibFunc_statvfs64:
712 case LibFunc_dunder_isoc99_sscanf:
719 case LibFunc_fopen64:
727 case LibFunc_fseeko64:
728 case LibFunc_ftello64:
732 case LibFunc_tmpfile64:
736 case LibFunc_fstat64:
737 case LibFunc_fstatvfs64:
746 case LibFunc_gettimeofday:
758 case LibFunc_msvc_new_int:
759 case LibFunc_msvc_new_longlong:
760 case LibFunc_msvc_new_array_int:
761 case LibFunc_msvc_new_array_longlong:
769 case LibFunc_memset_pattern16:
776 case LibFunc_nvvm_reflect:
794 return TLI->
has(FloatFn);
796 return TLI->
has(DoubleFn);
798 return TLI->
has(LongDoubleFn);
806 "Cannot get name for unavailable function!");
816 return TLI->
getName(LongDoubleFn);
831 bool IsVaArgs =
false) {
832 if (!TLI->
has(TheLibFunc))
864 return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, I32Ty},
880 return emitLibCall(LibFunc_strcpy, I8Ptr, {I8Ptr, I8Ptr},
887 return emitLibCall(LibFunc_stpcpy, I8Ptr, {I8Ptr, I8Ptr},
908 if (!TLI->
has(LibFunc_memcpy_chk))
914 Attribute::NoUnwind);
961 {Ptr1, Ptr2, Val, Len},
B, TLI);
1045 assert((Name !=
"") &&
"Must specify Name to emitUnaryFloatFnCall");
1057 Attribute::Speculatable));
1079 DoubleFn, FloatFn, LongDoubleFn);
1087 assert((Name !=
"") &&
"Must specify Name to emitBinaryFloatFnCall");
1099 Attribute::Speculatable));
1109 assert((Name !=
"") &&
"Must specify Name to emitBinaryFloatFnCall");
1124 DoubleFn, FloatFn, LongDoubleFn);
1131 if (!TLI->
has(LibFunc_putchar))
1154 if (!TLI->
has(LibFunc_puts))
1171 if (!TLI->
has(LibFunc_fputc))
1192 if (!TLI->
has(LibFunc_fputc_unlocked))
1212 if (!TLI->
has(LibFunc_fputs))
1231 if (!TLI->
has(LibFunc_fputs_unlocked))
1250 if (!TLI->
has(LibFunc_fwrite))
1274 if (!TLI->
has(LibFunc_malloc))
1294 if (!TLI.
has(LibFunc_calloc))
1302 CallocName, Attrs, B.
getInt8PtrTy(), PtrType, PtrType);
1316 if (!TLI->
has(LibFunc_fwrite_unlocked))
1338 if (!TLI->
has(LibFunc_fgetc_unlocked))
1357 if (!TLI->
has(LibFunc_fgets_unlocked))
1378 if (!TLI->
has(LibFunc_fread_unlocked))
bool onlyReadsMemory() const
Determine if the function does not access or only reads memory.
Value * emitStpCpy(Value *Dst, Value *Src, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the stpcpy function to the builder, for the specified pointer arguments.
static bool setReturnedArg(Function &F, unsigned ArgNo)
A parsed version of the target data layout string in and methods for querying it. ...
static bool setDoesNotCapture(Function &F, unsigned ArgNo)
LLVMContext & getContext() const
This class represents lattice values for constants.
Value * emitStrDup(Value *Ptr, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strdup function to the builder, for the specified pointer.
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
adds the attribute to the list of attributes for the given arg.
A Module instance is used to store all the information related to an LLVM module. ...
2: 32-bit floating point type
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
This class represents a function call, abstracting a target machine's calling convention.
Value * emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, ArrayRef< Value *> Args, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the snprintf function.
The two locations do not alias at all.
StringRef getName(LibFunc F) const
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
STATISTIC(NumFunctions, "Total number of functions")
bool isLibFreeFunction(const Function *F, const LibFunc TLIFn)
isLibFreeFunction - Returns true if the function is a builtin free()
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
1: 16-bit floating point type
Value * emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the memccpy function.
bool isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that reallocates memory (e...
Value * emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fputs_unlocked function.
LLVMContext & getContext() const
Get the context in which this basic block lives.
Value * emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the malloc function.
static bool setRetDoesNotAlias(Function &F)
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
static bool setNonLazyBind(Function &F)
TypeID getTypeID() const
Return the type id for the type.
LLVMContext & getContext() const
Get the global data context.
Value * emitPutChar(Value *Char, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the putchar function. This assumes that Char is an integer.
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Value * emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strlcpy function.
Value * emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, const AttributeList &Attrs)
Emit a call to the unary function named 'Name' (e.g.
Value * emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fputc_unlocked function.
mir Rename Register Operands
static Value * emitUnaryFloatFnCallHelper(Value *Op, StringRef Name, IRBuilder<> &B, const AttributeList &Attrs)
Class to represent function types.
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Type * getType() const
All values are typed, get the type of this value.
static bool setDoesNotThrow(Function &F)
BasicBlock * GetInsertBlock() const
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.
bool hasAttribute(unsigned i, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
bool has(LibFunc F) const
Tests whether a library function is available.
Value * emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the vsprintf function.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
void setOnlyAccessesArgMemory()
bool doesNotAccessMemory() const
Determine if the function does not access memory.
Value * emitFPutS(Value *Str, Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fputs function.
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
bool inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI)
Analyze the name and prototype of the given function and set any applicable attributes.
Type * getReturnType() const
Returns the type of the ret val.
void setOnlyReadsMemory()
StringRef getFloatFnName(const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn)
Get the name of the overloaded floating point function corresponding to Ty.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static void appendTypeSuffix(Value *Op, StringRef &Name, SmallString< 20 > &NameBuffer)
Append a suffix to the function name according to the type of 'Op'.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
Instrumentation for Order File
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isPointerTy() const
True if this is an instance of PointerType.
static bool setOnlyReadsMemory(Function &F)
void addAttribute(unsigned i, Attribute::AttrKind Kind)
adds the attribute to the list of attributes.
void setCallingConv(CallingConv::ID CC)
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Class to represent integer types.
Value * castToCStr(Value *V, IRBuilder<> &B)
Return V if it is an i8*, otherwise cast it to i8*.
static bool setDoesNotAccessMemory(Function &F)
static bool setRetNonNull(Function &F)
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.
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool doesNotThrow() const
Determine if the function cannot unwind.
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 'Name' (e.g.
Value * emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strncpy function to the builder, for the specified pointer arguments and length...
Value * emitFGetCUnlocked(Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fgetc_unlocked function. File is a pointer to FILE.
Value * emitStrCat(Value *Dest, Value *Src, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strcat function.
Value * emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strcpy function to the builder, for the specified pointer arguments.
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Value * emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the vsnprintf function.
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
amdgpu Simplify well known AMD library false FunctionCallee Callee
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
bool getRtLibUseGOT() const
Returns true if PLT should be avoided for RTLib calls.
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.
void setDoesNotAccessMemory()
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.
static bool setDoesNotAlias(Function &F, unsigned ArgNo)
LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index, Attribute::AttrKind Kind) const
Remove the specified attribute at the specified index from this attribute list.
static bool setOnlyAccessesArgMemory(Function &F)
bool onlyAccessesArgMemory() const
Determine if the call can access memmory only using pointers based on its arguments.
Value * emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strlcat function.
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Value * emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the bcmp function.
Value * emitSPrintf(Value *Dest, Value *Fmt, ArrayRef< Value *> VariadicArgs, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the sprintf function.
Value * emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the fwrite function.
static Value * emitLibCall(LibFunc TheLibFunc, Type *ReturnType, ArrayRef< Type *> ParamTypes, ArrayRef< Value *> Operands, IRBuilder<> &B, const TargetLibraryInfo *TLI, bool IsVaArgs=false)
3: 64-bit floating point type
Value * emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memcmp function.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Value * emitBinaryFloatFnCallHelper(Value *Op1, Value *Op2, StringRef Name, IRBuilder<> &B, const AttributeList &Attrs)
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.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
Value * emitPutS(Value *Str, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the puts function. This assumes that Str is some pointer.
Value * emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the stpncpy function to the builder, for the specified pointer arguments and length...
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
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.
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
StringRef - Represent a constant reference to a string, i.e.
static bool setDoesNotFreeMemory(Function &F)
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Value * emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strncat function.
bool hasFloatFn(const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn)
Check whether the overloaded floating point function corresponding to Ty is available.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute >> Attrs)
Create an AttributeList with the specified parameters in it.