@@ -112,6 +112,14 @@ static bool setNonLazyBind(Function &F) {
112
112
return true ;
113
113
}
114
114
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
+
115
123
bool llvm::inferLibFuncAttributes (Function &F, const TargetLibraryInfo &TLI) {
116
124
LibFunc TheLibFunc;
117
125
if (!(TLI.getLibFunc (F, TheLibFunc) && TLI.has (TheLibFunc)))
@@ -755,11 +763,12 @@ Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
755
763
return nullptr ;
756
764
757
765
Module *M = B.GetInsertBlock ()->getModule ();
766
+ StringRef StrlenName = TLI->getName (LibFunc_strlen);
758
767
LLVMContext &Context = B.GetInsertBlock ()->getContext ();
759
- Constant *StrLen = M->getOrInsertFunction (" strlen " , DL.getIntPtrType (Context),
768
+ Constant *StrLen = M->getOrInsertFunction (StrlenName , DL.getIntPtrType (Context),
760
769
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 );
763
772
if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts ()))
764
773
CI->setCallingConv (F->getCallingConv ());
765
774
@@ -772,13 +781,14 @@ Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
772
781
return nullptr ;
773
782
774
783
Module *M = B.GetInsertBlock ()->getModule ();
784
+ StringRef StrChrName = TLI->getName (LibFunc_strchr);
775
785
Type *I8Ptr = B.getInt8PtrTy ();
776
786
Type *I32Ty = B.getInt32Ty ();
777
787
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);
780
790
CallInst *CI = B.CreateCall (
781
- StrChr, {castToCStr (Ptr , B), ConstantInt::get (I32Ty, C)}, " strchr " );
791
+ StrChr, {castToCStr (Ptr , B), ConstantInt::get (I32Ty, C)}, StrChrName );
782
792
if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts ()))
783
793
CI->setCallingConv (F->getCallingConv ());
784
794
return CI;
@@ -790,13 +800,14 @@ Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
790
800
return nullptr ;
791
801
792
802
Module *M = B.GetInsertBlock ()->getModule ();
803
+ StringRef StrNCmpName = TLI->getName (LibFunc_strncmp);
793
804
LLVMContext &Context = B.GetInsertBlock ()->getContext ();
794
- Value *StrNCmp = M->getOrInsertFunction (" strncmp " , B.getInt32Ty (),
805
+ Value *StrNCmp = M->getOrInsertFunction (StrNCmpName , B.getInt32Ty (),
795
806
B.getInt8PtrTy (), B.getInt8PtrTy (),
796
807
DL.getIntPtrType (Context));
797
- inferLibFuncAttributes (*M-> getFunction ( " strncmp " ) , *TLI);
808
+ inferLibFuncAttributes (M, StrNCmpName , *TLI);
798
809
CallInst *CI = B.CreateCall (
799
- StrNCmp, {castToCStr (Ptr1, B), castToCStr (Ptr2, B), Len}, " strncmp " );
810
+ StrNCmp, {castToCStr (Ptr1, B), castToCStr (Ptr2, B), Len}, StrNCmpName );
800
811
801
812
if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts ()))
802
813
CI->setCallingConv (F->getCallingConv ());
@@ -812,7 +823,7 @@ Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
812
823
Module *M = B.GetInsertBlock ()->getModule ();
813
824
Type *I8Ptr = B.getInt8PtrTy ();
814
825
Value *StrCpy = M->getOrInsertFunction (Name, I8Ptr, I8Ptr, I8Ptr);
815
- inferLibFuncAttributes (*M-> getFunction ( Name) , *TLI);
826
+ inferLibFuncAttributes (M, Name, *TLI);
816
827
CallInst *CI =
817
828
B.CreateCall (StrCpy, {castToCStr (Dst, B), castToCStr (Src, B)}, Name);
818
829
if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts ()))
@@ -829,9 +840,9 @@ Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
829
840
Type *I8Ptr = B.getInt8PtrTy ();
830
841
Value *StrNCpy = M->getOrInsertFunction (Name, I8Ptr, I8Ptr, I8Ptr,
831
842
Len->getType ());
832
- inferLibFuncAttributes (*M-> getFunction ( Name) , *TLI);
843
+ inferLibFuncAttributes (M, Name, *TLI);
833
844
CallInst *CI = B.CreateCall (
834
- StrNCpy, {castToCStr (Dst, B), castToCStr (Src, B), Len}, " strncpy " );
845
+ StrNCpy, {castToCStr (Dst, B), castToCStr (Src, B), Len}, Name );
835
846
if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts ()))
836
847
CI->setCallingConv (F->getCallingConv ());
837
848
return CI;
@@ -866,12 +877,13 @@ Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
866
877
return nullptr ;
867
878
868
879
Module *M = B.GetInsertBlock ()->getModule ();
880
+ StringRef MemChrName = TLI->getName (LibFunc_memchr);
869
881
LLVMContext &Context = B.GetInsertBlock ()->getContext ();
870
- Value *MemChr = M->getOrInsertFunction (" memchr " , B.getInt8PtrTy (),
882
+ Value *MemChr = M->getOrInsertFunction (MemChrName , B.getInt8PtrTy (),
871
883
B.getInt8PtrTy (), B.getInt32Ty (),
872
884
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 );
875
887
876
888
if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts ()))
877
889
CI->setCallingConv (F->getCallingConv ());
@@ -885,13 +897,14 @@ Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
885
897
return nullptr ;
886
898
887
899
Module *M = B.GetInsertBlock ()->getModule ();
900
+ StringRef MemCmpName = TLI->getName (LibFunc_memcmp);
888
901
LLVMContext &Context = B.GetInsertBlock ()->getContext ();
889
- Value *MemCmp = M->getOrInsertFunction (" memcmp " , B.getInt32Ty (),
902
+ Value *MemCmp = M->getOrInsertFunction (MemCmpName , B.getInt32Ty (),
890
903
B.getInt8PtrTy (), B.getInt8PtrTy (),
891
904
DL.getIntPtrType (Context));
892
- inferLibFuncAttributes (*M-> getFunction ( " memcmp " ) , *TLI);
905
+ inferLibFuncAttributes (M, MemCmpName , *TLI);
893
906
CallInst *CI = B.CreateCall (
894
- MemCmp, {castToCStr (Ptr1, B), castToCStr (Ptr2, B), Len}, " memcmp " );
907
+ MemCmp, {castToCStr (Ptr1, B), castToCStr (Ptr2, B), Len}, MemCmpName );
895
908
896
909
if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts ()))
897
910
CI->setCallingConv (F->getCallingConv ());
@@ -958,14 +971,15 @@ Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B,
958
971
return nullptr ;
959
972
960
973
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);
963
977
CallInst *CI = B.CreateCall (PutChar,
964
978
B.CreateIntCast (Char,
965
979
B.getInt32Ty (),
966
980
/* isSigned*/ true ,
967
981
" chari" ),
968
- " putchar " );
982
+ PutCharName );
969
983
970
984
if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts ()))
971
985
CI->setCallingConv (F->getCallingConv ());
@@ -978,10 +992,11 @@ Value *llvm::emitPutS(Value *Str, IRBuilder<> &B,
978
992
return nullptr ;
979
993
980
994
Module *M = B.GetInsertBlock ()->getModule ();
995
+ StringRef PutsName = TLI->getName (LibFunc_puts);
981
996
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 );
985
1000
if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts ()))
986
1001
CI->setCallingConv (F->getCallingConv ());
987
1002
return CI;
@@ -993,13 +1008,14 @@ Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
993
1008
return nullptr ;
994
1009
995
1010
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 (),
997
1013
File->getType ());
998
1014
if (File->getType ()->isPointerTy ())
999
- inferLibFuncAttributes (*M-> getFunction ( " fputc " ) , *TLI);
1015
+ inferLibFuncAttributes (M, FPutcName , *TLI);
1000
1016
Char = B.CreateIntCast (Char, B.getInt32Ty (), /* isSigned*/ true ,
1001
1017
" chari" );
1002
- CallInst *CI = B.CreateCall (F, {Char, File}, " fputc " );
1018
+ CallInst *CI = B.CreateCall (F, {Char, File}, FPutcName );
1003
1019
1004
1020
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
1005
1021
CI->setCallingConv (Fn->getCallingConv ());
@@ -1012,12 +1028,13 @@ Value *llvm::emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B,
1012
1028
return nullptr ;
1013
1029
1014
1030
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 (),
1016
1033
B.getInt32Ty (), File->getType ());
1017
1034
if (File->getType ()->isPointerTy ())
1018
- inferLibFuncAttributes (*M-> getFunction ( " fputc_unlocked " ) , *TLI);
1035
+ inferLibFuncAttributes (M, FPutcUnlockedName , *TLI);
1019
1036
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 );
1021
1038
1022
1039
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
1023
1040
CI->setCallingConv (Fn->getCallingConv ());
@@ -1034,8 +1051,8 @@ Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
1034
1051
Constant *F = M->getOrInsertFunction (
1035
1052
FPutsName, B.getInt32Ty (), B.getInt8PtrTy (), File->getType ());
1036
1053
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 );
1039
1056
1040
1057
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
1041
1058
CI->setCallingConv (Fn->getCallingConv ());
@@ -1052,8 +1069,8 @@ Value *llvm::emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B,
1052
1069
Constant *F = M->getOrInsertFunction (FPutsUnlockedName, B.getInt32Ty (),
1053
1070
B.getInt8PtrTy (), File->getType ());
1054
1071
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 );
1057
1074
1058
1075
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
1059
1076
CI->setCallingConv (Fn->getCallingConv ());
@@ -1073,7 +1090,7 @@ Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
1073
1090
DL.getIntPtrType (Context), DL.getIntPtrType (Context), File->getType ());
1074
1091
1075
1092
if (File->getType ()->isPointerTy ())
1076
- inferLibFuncAttributes (*M-> getFunction ( FWriteName) , *TLI);
1093
+ inferLibFuncAttributes (M, FWriteName, *TLI);
1077
1094
CallInst *CI =
1078
1095
B.CreateCall (F, {castToCStr (Ptr , B), Size ,
1079
1096
ConstantInt::get (DL.getIntPtrType (Context), 1 ), File});
@@ -1089,11 +1106,12 @@ Value *llvm::emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL,
1089
1106
return nullptr ;
1090
1107
1091
1108
Module *M = B.GetInsertBlock ()->getModule ();
1109
+ StringRef MallocName = TLI->getName (LibFunc_malloc);
1092
1110
LLVMContext &Context = B.GetInsertBlock ()->getContext ();
1093
- Value *Malloc = M->getOrInsertFunction (" malloc " , B.getInt8PtrTy (),
1111
+ Value *Malloc = M->getOrInsertFunction (MallocName , B.getInt8PtrTy (),
1094
1112
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 );
1097
1115
1098
1116
if (const Function *F = dyn_cast<Function>(Malloc->stripPointerCasts ()))
1099
1117
CI->setCallingConv (F->getCallingConv ());
@@ -1107,12 +1125,13 @@ Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
1107
1125
return nullptr ;
1108
1126
1109
1127
Module *M = B.GetInsertBlock ()->getModule ();
1128
+ StringRef CallocName = TLI.getName (LibFunc_calloc);
1110
1129
const DataLayout &DL = M->getDataLayout ();
1111
1130
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 (),
1113
1132
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 );
1116
1135
1117
1136
if (const auto *F = dyn_cast<Function>(Calloc->stripPointerCasts ()))
1118
1137
CI->setCallingConv (F->getCallingConv ());
@@ -1134,7 +1153,7 @@ Value *llvm::emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
1134
1153
DL.getIntPtrType (Context), DL.getIntPtrType (Context), File->getType ());
1135
1154
1136
1155
if (File->getType ()->isPointerTy ())
1137
- inferLibFuncAttributes (*M-> getFunction ( FWriteUnlockedName) , *TLI);
1156
+ inferLibFuncAttributes (M, FWriteUnlockedName, *TLI);
1138
1157
CallInst *CI = B.CreateCall (F, {castToCStr (Ptr , B), Size , N, File});
1139
1158
1140
1159
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
@@ -1148,11 +1167,12 @@ Value *llvm::emitFGetCUnlocked(Value *File, IRBuilder<> &B,
1148
1167
return nullptr ;
1149
1168
1150
1169
Module *M = B.GetInsertBlock ()->getModule ();
1170
+ StringRef FGetCUnlockedName = TLI->getName (LibFunc_fgetc_unlocked);
1151
1171
Constant *F =
1152
- M->getOrInsertFunction (" fgetc_unlocked " , B.getInt32Ty (), File->getType ());
1172
+ M->getOrInsertFunction (FGetCUnlockedName , B.getInt32Ty (), File->getType ());
1153
1173
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 );
1156
1176
1157
1177
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
1158
1178
CI->setCallingConv (Fn->getCallingConv ());
@@ -1165,12 +1185,13 @@ Value *llvm::emitFGetSUnlocked(Value *Str, Value *Size, Value *File,
1165
1185
return nullptr ;
1166
1186
1167
1187
Module *M = B.GetInsertBlock ()->getModule ();
1188
+ StringRef FGetSUnlockedName = TLI->getName (LibFunc_fgets_unlocked);
1168
1189
Constant *F =
1169
- M->getOrInsertFunction (" fgets_unlocked " , B.getInt8PtrTy (),
1190
+ M->getOrInsertFunction (FGetSUnlockedName , B.getInt8PtrTy (),
1170
1191
B.getInt8PtrTy (), B.getInt32Ty (), File->getType ());
1171
- inferLibFuncAttributes (*M-> getFunction ( " fgets_unlocked " ) , *TLI);
1192
+ inferLibFuncAttributes (M, FGetSUnlockedName , *TLI);
1172
1193
CallInst *CI =
1173
- B.CreateCall (F, {castToCStr (Str, B), Size , File}, " fgets_unlocked " );
1194
+ B.CreateCall (F, {castToCStr (Str, B), Size , File}, FGetSUnlockedName );
1174
1195
1175
1196
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
1176
1197
CI->setCallingConv (Fn->getCallingConv ());
@@ -1191,7 +1212,7 @@ Value *llvm::emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
1191
1212
DL.getIntPtrType (Context), DL.getIntPtrType (Context), File->getType ());
1192
1213
1193
1214
if (File->getType ()->isPointerTy ())
1194
- inferLibFuncAttributes (*M-> getFunction ( FReadUnlockedName) , *TLI);
1215
+ inferLibFuncAttributes (M, FReadUnlockedName, *TLI);
1195
1216
CallInst *CI = B.CreateCall (F, {castToCStr (Ptr , B), Size , N, File});
1196
1217
1197
1218
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts ()))
0 commit comments