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");
101 "nonnull applies only to pointers");
137 bool Changed =
false;
142 switch (TheLibFunc) {
151 case LibFunc_strrchr:
158 case LibFunc_strtoul:
159 case LibFunc_strtoll:
160 case LibFunc_strtold:
161 case LibFunc_strtoull:
167 case LibFunc_strncpy:
169 case LibFunc_strncat:
173 case LibFunc_stpncpy:
178 case LibFunc_strxfrm:
186 case LibFunc_strncmp:
187 case LibFunc_strcspn:
188 case LibFunc_strcoll:
189 case LibFunc_strcasecmp:
190 case LibFunc_strncasecmp:
197 case LibFunc_strpbrk:
203 case LibFunc_strtok_r:
214 case LibFunc_setvbuf:
219 case LibFunc_strndup:
226 case LibFunc_statvfs:
239 case LibFunc_sprintf:
245 case LibFunc_snprintf:
251 case LibFunc_setitimer:
273 case LibFunc_memrchr:
284 case LibFunc_memmove:
287 case LibFunc_mempcpy:
288 case LibFunc_memccpy:
293 case LibFunc_memcpy_chk:
296 case LibFunc_memalign:
308 case LibFunc_realloc:
323 case LibFunc_realpath:
335 case LibFunc_readlink:
372 case LibFunc_ctermid:
373 case LibFunc_clearerr:
374 case LibFunc_closedir:
410 case LibFunc_fgetc_unlocked:
416 case LibFunc_fsetpos:
417 case LibFunc_flockfile:
418 case LibFunc_funlockfile:
419 case LibFunc_ftrylockfile:
429 case LibFunc_fputc_unlocked:
434 case LibFunc_fstatvfs:
439 case LibFunc_fgets_unlocked:
444 case LibFunc_fread_unlocked:
450 case LibFunc_fwrite_unlocked:
457 case LibFunc_fputs_unlocked:
464 case LibFunc_fprintf:
470 case LibFunc_fgetpos:
476 case LibFunc_getlogin_r:
477 case LibFunc_getc_unlocked:
487 case LibFunc_getchar:
488 case LibFunc_getchar_unlocked:
491 case LibFunc_getitimer:
495 case LibFunc_getpwnam:
513 case LibFunc_unsetenv:
527 case LibFunc_putc_unlocked:
547 case LibFunc_putchar:
548 case LibFunc_putchar_unlocked:
568 case LibFunc_vsscanf:
575 case LibFunc_vfscanf:
585 case LibFunc_vprintf:
590 case LibFunc_vfprintf:
591 case LibFunc_vsprintf:
597 case LibFunc_vsnprintf:
608 case LibFunc_opendir:
614 case LibFunc_tmpfile:
644 case LibFunc_dunder_strdup:
645 case LibFunc_dunder_strndup:
651 case LibFunc_dunder_strtok_r:
656 case LibFunc_under_IO_getc:
660 case LibFunc_under_IO_putc:
664 case LibFunc_dunder_isoc99_scanf:
670 case LibFunc_lstat64:
671 case LibFunc_statvfs64:
677 case LibFunc_dunder_isoc99_sscanf:
684 case LibFunc_fopen64:
692 case LibFunc_fseeko64:
693 case LibFunc_ftello64:
697 case LibFunc_tmpfile64:
701 case LibFunc_fstat64:
702 case LibFunc_fstatvfs64:
711 case LibFunc_gettimeofday:
723 case LibFunc_msvc_new_int:
724 case LibFunc_msvc_new_longlong:
725 case LibFunc_msvc_new_array_int:
726 case LibFunc_msvc_new_array_longlong:
734 case LibFunc_memset_pattern16:
741 case LibFunc_nvvm_reflect:
760 return TLI->
has(FloatFn);
762 return TLI->
has(DoubleFn);
764 return TLI->
has(LongDoubleFn);
772 "Cannot get name for unavailable function!");
782 return TLI->
getName(LongDoubleFn);
795 if (!TLI->
has(LibFunc_strlen))
813 if (!TLI->
has(LibFunc_strchr))
832 if (!TLI->
has(LibFunc_strncmp))
853 if (!TLI->
has(LibFunc_strcpy))
869 if (!TLI->
has(LibFunc_strncpy))
887 if (!TLI->
has(LibFunc_memcpy_chk))
909 if (!TLI->
has(LibFunc_memchr))
929 if (!TLI->
has(LibFunc_memcmp))
966 assert((Name !=
"") &&
"Must specify Name to emitUnaryFloatFnCall");
999 DoubleFn, FloatFn, LongDoubleFn);
1006 assert((Name !=
"") &&
"Must specify Name to emitBinaryFloatFnCall");
1024 if (!TLI->
has(LibFunc_putchar))
1045 if (!TLI->
has(LibFunc_puts))
1061 if (!TLI->
has(LibFunc_fputc))
1081 if (!TLI->
has(LibFunc_fputc_unlocked))
1100 if (!TLI->
has(LibFunc_fputs))
1118 if (!TLI->
has(LibFunc_fputs_unlocked))
1136 if (!TLI->
has(LibFunc_fwrite))
1159 if (!TLI->
has(LibFunc_malloc))
1178 if (!TLI.
has(LibFunc_calloc))
1199 if (!TLI->
has(LibFunc_fwrite_unlocked))
1220 if (!TLI->
has(LibFunc_fgetc_unlocked))
1238 if (!TLI->
has(LibFunc_fgets_unlocked))
1258 if (!TLI->
has(LibFunc_fread_unlocked))
bool onlyReadsMemory() const
Determine if the function does not access or only reads memory.
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.
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
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
This class represents a function call, abstracting a target machine's calling convention.
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")
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
1: 16-bit floating point type
Value * emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, const TargetLibraryInfo *TLI, StringRef Name="strncpy")
Emit a call to the strncpy function to the builder, for the specified pointer arguments and length...
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.
StringRef getUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn)
Get the name of the overloaded unary floating point function corresponding to Ty. ...
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...
amdgpu Simplify well known AMD library false Value Value const Twine & Name
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.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
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.
static Value * emitUnaryFloatFnCallHelper(Value *Op, StringRef Name, IRBuilder<> &B, const AttributeList &Attrs)
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.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Value * emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, const TargetLibraryInfo *TLI, StringRef Name="strcpy")
Emit a call to the strcpy function to the builder, for the specified pointer arguments.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
amdgpu Simplify well known AMD library false Value * Callee
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()
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.
This is an important base class in LLVM.
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)
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)
const Constant * stripPointerCasts() const
static bool setRetNonNull(Function &F)
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs, and aliases.
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.
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 * emitFGetCUnlocked(Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fgetc_unlocked function. File is a pointer to FILE.
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
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.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
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.
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.
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 * emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the fwrite function.
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())
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 * 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.
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
bool hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn)
Check whether the overloaded unary floating point function corresponding to Ty is available...
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute >> Attrs)
Create an AttributeList with the specified parameters in it.