@@ -112,6 +112,14 @@ static bool setNonLazyBind(Function &F) {
112112 return true ;
113113}
114114
115+ bool llvm::inferLibFuncAttributes (Module *M, StringRef Name,
116+ const TargetLibraryInfo &TLI) {
117+ Function *F = M->getFunction (Name);
118+ if (!F)
119+ return false ;
120+ return inferLibFuncAttributes (*F, TLI);
121+ }
122+
115123bool llvm::inferLibFuncAttributes (Function &F, const TargetLibraryInfo &TLI) {
116124 LibFunc TheLibFunc;
117125 if (!(TLI.getLibFunc (F, TheLibFunc) && TLI.has (TheLibFunc)))
@@ -755,11 +763,12 @@ Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
755763 return nullptr ;
756764
757765 Module *M = B.GetInsertBlock ()->getModule ();
766+ StringRef StrlenName = TLI->getName (LibFunc_strlen);
758767 LLVMContext &Context = B.GetInsertBlock ()->getContext ();
759- Constant *StrLen = M->getOrInsertFunction (" strlen " , DL.getIntPtrType (Context),
768+ Constant *StrLen = M->getOrInsertFunction (StrlenName , DL.getIntPtrType (Context),
760769 B.getInt8PtrTy ());
761- inferLibFuncAttributes (*M-> getFunction ( " strlen " ) , *TLI);
762- CallInst *CI = B.CreateCall (StrLen, castToCStr (Ptr, B), " strlen " );
770+ inferLibFuncAttributes (M, StrlenName , *TLI);
771+ CallInst *CI = B.CreateCall (StrLen, castToCStr (Ptr, B), StrlenName );
763772 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts ()))
764773 CI->setCallingConv (F->getCallingConv ());
765774
@@ -772,13 +781,14 @@ Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
772781 return nullptr ;
773782
774783 Module *M = B.GetInsertBlock ()->getModule ();
784+ StringRef StrChrName = TLI->getName (LibFunc_strchr);
775785 Type *I8Ptr = B.getInt8PtrTy ();
776786 Type *I32Ty = B.getInt32Ty ();
777787 Constant *StrChr =
778- M->getOrInsertFunction (" strchr " , I8Ptr, I8Ptr, I32Ty);
779- inferLibFuncAttributes (*M-> getFunction ( " strchr " ) , *TLI);
788+ M->getOrInsertFunction (StrChrName , I8Ptr, I8Ptr, I32Ty);
789+ inferLibFuncAttributes (M, StrChrName , *TLI);
780790 CallInst *CI = B.CreateCall (
781- StrChr, {castToCStr (Ptr, B), ConstantInt::get (I32Ty, C)}, " strchr " );
791+ StrChr, {castToCStr (Ptr, B), ConstantInt::get (I32Ty, C)}, StrChrName );
782792 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts ()))
783793 CI->setCallingConv (F->getCallingConv ());
784794 return CI;
@@ -790,13 +800,14 @@ Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
790800 return nullptr ;
791801
792802 Module *M = B.GetInsertBlock ()->getModule ();
803+ StringRef StrNCmpName = TLI->getName (LibFunc_strncmp);
793804 LLVMContext &Context = B.GetInsertBlock ()->getContext ();
794- Value *StrNCmp = M->getOrInsertFunction (" strncmp " , B.getInt32Ty (),
805+ Value *StrNCmp = M->getOrInsertFunction (StrNCmpName , B.getInt32Ty (),
795806 B.getInt8PtrTy (), B.getInt8PtrTy (),
796807 DL.getIntPtrType (Context));
797- inferLibFuncAttributes (*M-> getFunction ( " strncmp " ) , *TLI);
808+ inferLibFuncAttributes (M, StrNCmpName , *TLI);
798809 CallInst *CI = B.CreateCall (
799- StrNCmp, {castToCStr (Ptr1, B), castToCStr (Ptr2, B), Len}, " strncmp " );
810+ StrNCmp, {castToCStr (Ptr1, B), castToCStr (Ptr2, B), Len}, StrNCmpName );
800811
801812 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts ()))
802813 CI->setCallingConv (F->getCallingConv ());
@@ -812,7 +823,7 @@ Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
812823 Module *M = B.GetInsertBlock ()->getModule ();
813824 Type *I8Ptr = B.getInt8PtrTy ();
814825 Value *StrCpy = M->getOrInsertFunction (Name, I8Ptr, I8Ptr, I8Ptr);
815- inferLibFuncAttributes (*M-> getFunction ( Name) , *TLI);
826+ inferLibFuncAttributes (M, Name, *TLI);
816827 CallInst *CI =
817828 B.CreateCall (StrCpy, {castToCStr (Dst, B), castToCStr (Src, B)}, Name);
818829 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts ()))
@@ -829,9 +840,9 @@ Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
829840 Type *I8Ptr = B.getInt8PtrTy ();
830841 Value *StrNCpy = M->getOrInsertFunction (Name, I8Ptr, I8Ptr, I8Ptr,
831842 Len->getType ());
832- inferLibFuncAttributes (*M-> getFunction ( Name) , *TLI);
843+ inferLibFuncAttributes (M, Name, *TLI);
833844 CallInst *CI = B.CreateCall (
834- StrNCpy, {castToCStr (Dst, B), castToCStr (Src, B), Len}, " strncpy " );
845+ StrNCpy, {castToCStr (Dst, B), castToCStr (Src, B), Len}, Name );
835846 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts ()))
836847 CI->setCallingConv (F->getCallingConv ());
837848 return CI;
@@ -866,12 +877,13 @@ Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
866877 return nullptr ;
867878
868879 Module *M = B.GetInsertBlock ()->getModule ();
880+ StringRef MemChrName = TLI->getName (LibFunc_memchr);
869881 LLVMContext &Context = B.GetInsertBlock ()->getContext ();
870- Value *MemChr = M->getOrInsertFunction (" memchr " , B.getInt8PtrTy (),
882+ Value *MemChr = M->getOrInsertFunction (MemChrName , B.getInt8PtrTy (),
871883 B.getInt8PtrTy (), B.getInt32Ty (),
872884 DL.getIntPtrType (Context));
873- inferLibFuncAttributes (*M-> getFunction ( " memchr " ) , *TLI);
874- CallInst *CI = B.CreateCall (MemChr, {castToCStr (Ptr, B), Val, Len}, " memchr " );
885+ inferLibFuncAttributes (M, MemChrName , *TLI);
886+ CallInst *CI = B.CreateCall (MemChr, {castToCStr (Ptr, B), Val, Len}, MemChrName );
875887
876888 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts ()))
877889 CI->setCallingConv (F->getCallingConv ());
@@ -885,13 +897,14 @@ Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
885897 return nullptr ;
886898
887899 Module *M = B.GetInsertBlock ()->getModule ();
900+ StringRef MemCmpName = TLI->getName (LibFunc_memcmp);
888901 LLVMContext &Context = B.GetInsertBlock ()->getContext ();
889- Value *MemCmp = M->getOrInsertFunction (" memcmp " , B.getInt32Ty (),
902+ Value *MemCmp = M->getOrInsertFunction (MemCmpName , B.getInt32Ty (),
890903 B.getInt8PtrTy (), B.getInt8PtrTy (),
891904 DL.getIntPtrType (Context));
892- inferLibFuncAttributes (*M-> getFunction ( " memcmp " ) , *TLI);
905+ inferLibFuncAttributes (M, MemCmpName , *TLI);
893906 CallInst *CI = B.CreateCall (
894- MemCmp, {castToCStr (Ptr1, B), castToCStr (Ptr2, B), Len}, " memcmp " );
907+ MemCmp, {castToCStr (Ptr1, B), castToCStr (Ptr2, B), Len}, MemCmpName );
895908
896909 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts ()))
897910 CI->setCallingConv (F->getCallingConv ());
@@ -958,14 +971,15 @@ Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B,
958971 return nullptr ;
959972
960973 Module *M = B.GetInsertBlock ()->getModule ();
961- Value *PutChar = M->getOrInsertFunction (" putchar" , B.getInt32Ty (), B.getInt32Ty ());
962- inferLibFuncAttributes (*M->getFunction (" putchar" ), *TLI);
974+ StringRef PutCharName = TLI->getName (LibFunc_putchar);
975+ Value *PutChar = M->getOrInsertFunction (PutCharName, B.getInt32Ty (), B.getInt32Ty ());
976+ inferLibFuncAttributes (M, PutCharName, *TLI);
963977 CallInst *CI = B.CreateCall (PutChar,
964978 B.CreateIntCast (Char,
965979 B.getInt32Ty (),
966980 /* isSigned*/ true ,
967981 " chari" ),
968- " putchar " );
982+ PutCharName );
969983
970984 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts ()))
971985 CI->setCallingConv (F->getCallingConv ());
@@ -978,10 +992,11 @@ Value *llvm::emitPutS(Value *Str, IRBuilder<> &B,
978992 return nullptr ;
979993
980994 Module *M = B.GetInsertBlock ()->getModule ();
995+ StringRef PutsName = TLI->getName (LibFunc_puts);
981996 Value *PutS =
982- M->getOrInsertFunction (" puts " , B.getInt32Ty (), B.getInt8PtrTy ());
983- inferLibFuncAttributes (*M-> getFunction ( " puts " ) , *TLI);
984- CallInst *CI = B.CreateCall (PutS, castToCStr (Str, B), " puts " );
997+ M->getOrInsertFunction (PutsName , B.getInt32Ty (), B.getInt8PtrTy ());
998+ inferLibFuncAttributes (M, PutsName , *TLI);
999+ CallInst *CI = B.CreateCall (PutS, castToCStr (Str, B), PutsName );
9851000 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts ()))
9861001 CI->setCallingConv (F->getCallingConv ());
9871002 return CI;
@@ -993,13 +1008,14 @@ Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
9931008 return nullptr ;
9941009
9951010 Module *M = B.GetInsertBlock ()->getModule ();
996- Constant *F = M->getOrInsertFunction (" fputc" , B.getInt32Ty (), B.getInt32Ty (),
1011+ StringRef FPutcName = TLI->getName (LibFunc_fputc);
1012+ Constant *F = M->getOrInsertFunction (FPutcName, B.getInt32Ty (), B.getInt32Ty (),
9971013 File->getType ());
9981014 if (File->getType ()->isPointerTy ())
999- inferLibFuncAttributes (*M-> getFunction ( " fputc " ) , *TLI);
1015+ inferLibFuncAttributes (M, FPutcName , *TLI);
10001016 Char = B.CreateIntCast (Char, B.getInt32Ty (), /* isSigned*/ true ,
10011017 " chari" );
1002- CallInst *CI = B.CreateCall (F, {Char, File}, " fputc " );
1018+ CallInst *CI = B.CreateCall (F, {Char, File}, FPutcName );
10031019
10041020 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
10051021 CI->setCallingConv (Fn->getCallingConv ());
@@ -1012,12 +1028,13 @@ Value *llvm::emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B,
10121028 return nullptr ;
10131029
10141030 Module *M = B.GetInsertBlock ()->getModule ();
1015- Constant *F = M->getOrInsertFunction (" fputc_unlocked" , B.getInt32Ty (),
1031+ StringRef FPutcUnlockedName = TLI->getName (LibFunc_fputc_unlocked);
1032+ Constant *F = M->getOrInsertFunction (FPutcUnlockedName, B.getInt32Ty (),
10161033 B.getInt32Ty (), File->getType ());
10171034 if (File->getType ()->isPointerTy ())
1018- inferLibFuncAttributes (*M-> getFunction ( " fputc_unlocked " ) , *TLI);
1035+ inferLibFuncAttributes (M, FPutcUnlockedName , *TLI);
10191036 Char = B.CreateIntCast (Char, B.getInt32Ty (), /* isSigned*/ true , " chari" );
1020- CallInst *CI = B.CreateCall (F, {Char, File}, " fputc_unlocked " );
1037+ CallInst *CI = B.CreateCall (F, {Char, File}, FPutcUnlockedName );
10211038
10221039 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
10231040 CI->setCallingConv (Fn->getCallingConv ());
@@ -1034,8 +1051,8 @@ Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
10341051 Constant *F = M->getOrInsertFunction (
10351052 FPutsName, B.getInt32Ty (), B.getInt8PtrTy (), File->getType ());
10361053 if (File->getType ()->isPointerTy ())
1037- inferLibFuncAttributes (*M-> getFunction ( FPutsName) , *TLI);
1038- CallInst *CI = B.CreateCall (F, {castToCStr (Str, B), File}, " fputs " );
1054+ inferLibFuncAttributes (M, FPutsName, *TLI);
1055+ CallInst *CI = B.CreateCall (F, {castToCStr (Str, B), File}, FPutsName );
10391056
10401057 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
10411058 CI->setCallingConv (Fn->getCallingConv ());
@@ -1052,8 +1069,8 @@ Value *llvm::emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B,
10521069 Constant *F = M->getOrInsertFunction (FPutsUnlockedName, B.getInt32Ty (),
10531070 B.getInt8PtrTy (), File->getType ());
10541071 if (File->getType ()->isPointerTy ())
1055- inferLibFuncAttributes (*M-> getFunction ( FPutsUnlockedName) , *TLI);
1056- CallInst *CI = B.CreateCall (F, {castToCStr (Str, B), File}, " fputs_unlocked " );
1072+ inferLibFuncAttributes (M, FPutsUnlockedName, *TLI);
1073+ CallInst *CI = B.CreateCall (F, {castToCStr (Str, B), File}, FPutsUnlockedName );
10571074
10581075 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
10591076 CI->setCallingConv (Fn->getCallingConv ());
@@ -1073,7 +1090,7 @@ Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
10731090 DL.getIntPtrType (Context), DL.getIntPtrType (Context), File->getType ());
10741091
10751092 if (File->getType ()->isPointerTy ())
1076- inferLibFuncAttributes (*M-> getFunction ( FWriteName) , *TLI);
1093+ inferLibFuncAttributes (M, FWriteName, *TLI);
10771094 CallInst *CI =
10781095 B.CreateCall (F, {castToCStr (Ptr, B), Size,
10791096 ConstantInt::get (DL.getIntPtrType (Context), 1 ), File});
@@ -1089,11 +1106,12 @@ Value *llvm::emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL,
10891106 return nullptr ;
10901107
10911108 Module *M = B.GetInsertBlock ()->getModule ();
1109+ StringRef MallocName = TLI->getName (LibFunc_malloc);
10921110 LLVMContext &Context = B.GetInsertBlock ()->getContext ();
1093- Value *Malloc = M->getOrInsertFunction (" malloc " , B.getInt8PtrTy (),
1111+ Value *Malloc = M->getOrInsertFunction (MallocName , B.getInt8PtrTy (),
10941112 DL.getIntPtrType (Context));
1095- inferLibFuncAttributes (*M-> getFunction ( " malloc " ) , *TLI);
1096- CallInst *CI = B.CreateCall (Malloc, Num, " malloc " );
1113+ inferLibFuncAttributes (M, MallocName , *TLI);
1114+ CallInst *CI = B.CreateCall (Malloc, Num, MallocName );
10971115
10981116 if (const Function *F = dyn_cast<Function>(Malloc->stripPointerCasts ()))
10991117 CI->setCallingConv (F->getCallingConv ());
@@ -1107,12 +1125,13 @@ Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
11071125 return nullptr ;
11081126
11091127 Module *M = B.GetInsertBlock ()->getModule ();
1128+ StringRef CallocName = TLI.getName (LibFunc_calloc);
11101129 const DataLayout &DL = M->getDataLayout ();
11111130 IntegerType *PtrType = DL.getIntPtrType ((B.GetInsertBlock ()->getContext ()));
1112- Value *Calloc = M->getOrInsertFunction (" calloc " , Attrs, B.getInt8PtrTy (),
1131+ Value *Calloc = M->getOrInsertFunction (CallocName , Attrs, B.getInt8PtrTy (),
11131132 PtrType, PtrType);
1114- inferLibFuncAttributes (*M-> getFunction ( " calloc " ) , TLI);
1115- CallInst *CI = B.CreateCall (Calloc, {Num, Size}, " calloc " );
1133+ inferLibFuncAttributes (M, CallocName , TLI);
1134+ CallInst *CI = B.CreateCall (Calloc, {Num, Size}, CallocName );
11161135
11171136 if (const auto *F = dyn_cast<Function>(Calloc->stripPointerCasts ()))
11181137 CI->setCallingConv (F->getCallingConv ());
@@ -1134,7 +1153,7 @@ Value *llvm::emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
11341153 DL.getIntPtrType (Context), DL.getIntPtrType (Context), File->getType ());
11351154
11361155 if (File->getType ()->isPointerTy ())
1137- inferLibFuncAttributes (*M-> getFunction ( FWriteUnlockedName) , *TLI);
1156+ inferLibFuncAttributes (M, FWriteUnlockedName, *TLI);
11381157 CallInst *CI = B.CreateCall (F, {castToCStr (Ptr, B), Size, N, File});
11391158
11401159 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
@@ -1148,11 +1167,12 @@ Value *llvm::emitFGetCUnlocked(Value *File, IRBuilder<> &B,
11481167 return nullptr ;
11491168
11501169 Module *M = B.GetInsertBlock ()->getModule ();
1170+ StringRef FGetCUnlockedName = TLI->getName (LibFunc_fgetc_unlocked);
11511171 Constant *F =
1152- M->getOrInsertFunction (" fgetc_unlocked " , B.getInt32Ty (), File->getType ());
1172+ M->getOrInsertFunction (FGetCUnlockedName , B.getInt32Ty (), File->getType ());
11531173 if (File->getType ()->isPointerTy ())
1154- inferLibFuncAttributes (*M-> getFunction ( " fgetc_unlocked " ) , *TLI);
1155- CallInst *CI = B.CreateCall (F, File, " fgetc_unlocked " );
1174+ inferLibFuncAttributes (M, FGetCUnlockedName , *TLI);
1175+ CallInst *CI = B.CreateCall (F, File, FGetCUnlockedName );
11561176
11571177 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
11581178 CI->setCallingConv (Fn->getCallingConv ());
@@ -1165,12 +1185,13 @@ Value *llvm::emitFGetSUnlocked(Value *Str, Value *Size, Value *File,
11651185 return nullptr ;
11661186
11671187 Module *M = B.GetInsertBlock ()->getModule ();
1188+ StringRef FGetSUnlockedName = TLI->getName (LibFunc_fgets_unlocked);
11681189 Constant *F =
1169- M->getOrInsertFunction (" fgets_unlocked " , B.getInt8PtrTy (),
1190+ M->getOrInsertFunction (FGetSUnlockedName , B.getInt8PtrTy (),
11701191 B.getInt8PtrTy (), B.getInt32Ty (), File->getType ());
1171- inferLibFuncAttributes (*M-> getFunction ( " fgets_unlocked " ) , *TLI);
1192+ inferLibFuncAttributes (M, FGetSUnlockedName , *TLI);
11721193 CallInst *CI =
1173- B.CreateCall (F, {castToCStr (Str, B), Size, File}, " fgets_unlocked " );
1194+ B.CreateCall (F, {castToCStr (Str, B), Size, File}, FGetSUnlockedName );
11741195
11751196 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
11761197 CI->setCallingConv (Fn->getCallingConv ());
@@ -1191,7 +1212,7 @@ Value *llvm::emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
11911212 DL.getIntPtrType (Context), DL.getIntPtrType (Context), File->getType ());
11921213
11931214 if (File->getType ()->isPointerTy ())
1194- inferLibFuncAttributes (*M-> getFunction ( FReadUnlockedName) , *TLI);
1215+ inferLibFuncAttributes (M, FReadUnlockedName, *TLI);
11951216 CallInst *CI = B.CreateCall (F, {castToCStr (Ptr, B), Size, N, File});
11961217
11971218 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
0 commit comments