llvm.org GIT mirror llvm / 5ab8235
Merging r344454, r344455, r344645: ------------------------------------------------------------------------ r344454 | xbolva00 | 2018-10-13 08:21:55 -0700 (Sat, 13 Oct 2018) | 11 lines [InstCombine] Fixed crash with aliased functions Summary: Fixes PR39177 Reviewers: spatel, jbuening Reviewed By: jbuening Subscribers: jbuening, llvm-commits Differential Revision: https://reviews.llvm.org/D53129 ------------------------------------------------------------------------ ------------------------------------------------------------------------ r344455 | xbolva00 | 2018-10-13 08:26:13 -0700 (Sat, 13 Oct 2018) | 2 lines [NFC] Fixed duplicated test file ------------------------------------------------------------------------ ------------------------------------------------------------------------ r344645 | xbolva00 | 2018-10-16 14:18:31 -0700 (Tue, 16 Oct 2018) | 9 lines [InstCombine] Cleanup libfunc attribute inferring Reviewers: efriedma Reviewed By: efriedma Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D53338 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_70@345921 91177308-0d34-0410-b5e6-96231b3b80d8 Tom Stellard 1 year, 11 months ago
4 changed file(s) with 118 addition(s) and 51 deletion(s). Raw diff Collapse all Expand all
2828 ///
2929 /// Returns true if any attributes were set and false otherwise.
3030 bool inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI);
31 bool inferLibFuncAttributes(Module *M, StringRef Name, const TargetLibraryInfo &TLI);
3132
3233 /// Check whether the overloaded unary floating point function
3334 /// corresponding to \a Ty is available.
920920 Type *Int8PtrTy = DestInt8PtrTy;
921921
922922 Module *M = TheStore->getModule();
923 StringRef FuncName = "memset_pattern16";
923924 Value *MSP =
924 M->getOrInsertFunction("memset_pattern16", Builder.getVoidTy(),
925 M->getOrInsertFunction(FuncName, Builder.getVoidTy(),
925926 Int8PtrTy, Int8PtrTy, IntPtr);
926 inferLibFuncAttributes(*M->getFunction("memset_pattern16"), *TLI);
927 inferLibFuncAttributes(M, FuncName, *TLI);
927928
928929 // Otherwise we should form a memset_pattern16. PatternValue is known to be
929930 // an constant array of 16-bytes. Plop the value into a mergable global.
109109 return false;
110110 F.addFnAttr(Attribute::NonLazyBind);
111111 return true;
112 }
113
114 bool llvm::inferLibFuncAttributes(Module *M, StringRef Name,
115 const TargetLibraryInfo &TLI) {
116 Function *F = M->getFunction(Name);
117 if (!F)
118 return false;
119 return inferLibFuncAttributes(*F, TLI);
112120 }
113121
114122 bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
754762 return nullptr;
755763
756764 Module *M = B.GetInsertBlock()->getModule();
765 StringRef StrlenName = TLI->getName(LibFunc_strlen);
757766 LLVMContext &Context = B.GetInsertBlock()->getContext();
758 Constant *StrLen = M->getOrInsertFunction("strlen", DL.getIntPtrType(Context),
767 Constant *StrLen = M->getOrInsertFunction(StrlenName, DL.getIntPtrType(Context),
759768 B.getInt8PtrTy());
760 inferLibFuncAttributes(*M->getFunction("strlen"), *TLI);
761 CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), "strlen");
769 inferLibFuncAttributes(M, StrlenName, *TLI);
770 CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), StrlenName);
762771 if (const Function *F = dyn_cast(StrLen->stripPointerCasts()))
763772 CI->setCallingConv(F->getCallingConv());
764773
771780 return nullptr;
772781
773782 Module *M = B.GetInsertBlock()->getModule();
783 StringRef StrChrName = TLI->getName(LibFunc_strchr);
774784 Type *I8Ptr = B.getInt8PtrTy();
775785 Type *I32Ty = B.getInt32Ty();
776786 Constant *StrChr =
777 M->getOrInsertFunction("strchr", I8Ptr, I8Ptr, I32Ty);
778 inferLibFuncAttributes(*M->getFunction("strchr"), *TLI);
787 M->getOrInsertFunction(StrChrName, I8Ptr, I8Ptr, I32Ty);
788 inferLibFuncAttributes(M, StrChrName, *TLI);
779789 CallInst *CI = B.CreateCall(
780 StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr");
790 StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, StrChrName);
781791 if (const Function *F = dyn_cast(StrChr->stripPointerCasts()))
782792 CI->setCallingConv(F->getCallingConv());
783793 return CI;
789799 return nullptr;
790800
791801 Module *M = B.GetInsertBlock()->getModule();
802 StringRef StrNCmpName = TLI->getName(LibFunc_strncmp);
792803 LLVMContext &Context = B.GetInsertBlock()->getContext();
793 Value *StrNCmp = M->getOrInsertFunction("strncmp", B.getInt32Ty(),
804 Value *StrNCmp = M->getOrInsertFunction(StrNCmpName, B.getInt32Ty(),
794805 B.getInt8PtrTy(), B.getInt8PtrTy(),
795806 DL.getIntPtrType(Context));
796 inferLibFuncAttributes(*M->getFunction("strncmp"), *TLI);
807 inferLibFuncAttributes(M, StrNCmpName, *TLI);
797808 CallInst *CI = B.CreateCall(
798 StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "strncmp");
809 StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, StrNCmpName);
799810
800811 if (const Function *F = dyn_cast(StrNCmp->stripPointerCasts()))
801812 CI->setCallingConv(F->getCallingConv());
811822 Module *M = B.GetInsertBlock()->getModule();
812823 Type *I8Ptr = B.getInt8PtrTy();
813824 Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
814 inferLibFuncAttributes(*M->getFunction(Name), *TLI);
825 inferLibFuncAttributes(M, Name, *TLI);
815826 CallInst *CI =
816827 B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
817828 if (const Function *F = dyn_cast(StrCpy->stripPointerCasts()))
828839 Type *I8Ptr = B.getInt8PtrTy();
829840 Value *StrNCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr,
830841 Len->getType());
831 inferLibFuncAttributes(*M->getFunction(Name), *TLI);
842 inferLibFuncAttributes(M, Name, *TLI);
832843 CallInst *CI = B.CreateCall(
833 StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, "strncpy");
844 StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, Name);
834845 if (const Function *F = dyn_cast(StrNCpy->stripPointerCasts()))
835846 CI->setCallingConv(F->getCallingConv());
836847 return CI;
865876 return nullptr;
866877
867878 Module *M = B.GetInsertBlock()->getModule();
879 StringRef MemChrName = TLI->getName(LibFunc_memchr);
868880 LLVMContext &Context = B.GetInsertBlock()->getContext();
869 Value *MemChr = M->getOrInsertFunction("memchr", B.getInt8PtrTy(),
881 Value *MemChr = M->getOrInsertFunction(MemChrName, B.getInt8PtrTy(),
870882 B.getInt8PtrTy(), B.getInt32Ty(),
871883 DL.getIntPtrType(Context));
872 inferLibFuncAttributes(*M->getFunction("memchr"), *TLI);
873 CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, "memchr");
884 inferLibFuncAttributes(M, MemChrName, *TLI);
885 CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, MemChrName);
874886
875887 if (const Function *F = dyn_cast(MemChr->stripPointerCasts()))
876888 CI->setCallingConv(F->getCallingConv());
884896 return nullptr;
885897
886898 Module *M = B.GetInsertBlock()->getModule();
899 StringRef MemCmpName = TLI->getName(LibFunc_memcmp);
887900 LLVMContext &Context = B.GetInsertBlock()->getContext();
888 Value *MemCmp = M->getOrInsertFunction("memcmp", B.getInt32Ty(),
901 Value *MemCmp = M->getOrInsertFunction(MemCmpName, B.getInt32Ty(),
889902 B.getInt8PtrTy(), B.getInt8PtrTy(),
890903 DL.getIntPtrType(Context));
891 inferLibFuncAttributes(*M->getFunction("memcmp"), *TLI);
904 inferLibFuncAttributes(M, MemCmpName, *TLI);
892905 CallInst *CI = B.CreateCall(
893 MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "memcmp");
906 MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, MemCmpName);
894907
895908 if (const Function *F = dyn_cast(MemCmp->stripPointerCasts()))
896909 CI->setCallingConv(F->getCallingConv());
957970 return nullptr;
958971
959972 Module *M = B.GetInsertBlock()->getModule();
960 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(), B.getInt32Ty());
961 inferLibFuncAttributes(*M->getFunction("putchar"), *TLI);
973 StringRef PutCharName = TLI->getName(LibFunc_putchar);
974 Value *PutChar = M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty());
975 inferLibFuncAttributes(M, PutCharName, *TLI);
962976 CallInst *CI = B.CreateCall(PutChar,
963977 B.CreateIntCast(Char,
964978 B.getInt32Ty(),
965979 /*isSigned*/true,
966980 "chari"),
967 "putchar");
981 PutCharName);
968982
969983 if (const Function *F = dyn_cast(PutChar->stripPointerCasts()))
970984 CI->setCallingConv(F->getCallingConv());
977991 return nullptr;
978992
979993 Module *M = B.GetInsertBlock()->getModule();
994 StringRef PutsName = TLI->getName(LibFunc_puts);
980995 Value *PutS =
981 M->getOrInsertFunction("puts", B.getInt32Ty(), B.getInt8PtrTy());
982 inferLibFuncAttributes(*M->getFunction("puts"), *TLI);
983 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), "puts");
996 M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy());
997 inferLibFuncAttributes(M, PutsName, *TLI);
998 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName);
984999 if (const Function *F = dyn_cast(PutS->stripPointerCasts()))
9851000 CI->setCallingConv(F->getCallingConv());
9861001 return CI;
9921007 return nullptr;
9931008
9941009 Module *M = B.GetInsertBlock()->getModule();
995 Constant *F = M->getOrInsertFunction("fputc", B.getInt32Ty(), B.getInt32Ty(),
1010 StringRef FPutcName = TLI->getName(LibFunc_fputc);
1011 Constant *F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(), B.getInt32Ty(),
9961012 File->getType());
9971013 if (File->getType()->isPointerTy())
998 inferLibFuncAttributes(*M->getFunction("fputc"), *TLI);
1014 inferLibFuncAttributes(M, FPutcName, *TLI);
9991015 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
10001016 "chari");
1001 CallInst *CI = B.CreateCall(F, {Char, File}, "fputc");
1017 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName);
10021018
10031019 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
10041020 CI->setCallingConv(Fn->getCallingConv());
10111027 return nullptr;
10121028
10131029 Module *M = B.GetInsertBlock()->getModule();
1014 Constant *F = M->getOrInsertFunction("fputc_unlocked", B.getInt32Ty(),
1030 StringRef FPutcUnlockedName = TLI->getName(LibFunc_fputc_unlocked);
1031 Constant *F = M->getOrInsertFunction(FPutcUnlockedName, B.getInt32Ty(),
10151032 B.getInt32Ty(), File->getType());
10161033 if (File->getType()->isPointerTy())
1017 inferLibFuncAttributes(*M->getFunction("fputc_unlocked"), *TLI);
1034 inferLibFuncAttributes(M, FPutcUnlockedName, *TLI);
10181035 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari");
1019 CallInst *CI = B.CreateCall(F, {Char, File}, "fputc_unlocked");
1036 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcUnlockedName);
10201037
10211038 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
10221039 CI->setCallingConv(Fn->getCallingConv());
10331050 Constant *F = M->getOrInsertFunction(
10341051 FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType());
10351052 if (File->getType()->isPointerTy())
1036 inferLibFuncAttributes(*M->getFunction(FPutsName), *TLI);
1037 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs");
1053 inferLibFuncAttributes(M, FPutsName, *TLI);
1054 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName);
10381055
10391056 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
10401057 CI->setCallingConv(Fn->getCallingConv());
10511068 Constant *F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(),
10521069 B.getInt8PtrTy(), File->getType());
10531070 if (File->getType()->isPointerTy())
1054 inferLibFuncAttributes(*M->getFunction(FPutsUnlockedName), *TLI);
1055 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs_unlocked");
1071 inferLibFuncAttributes(M, FPutsUnlockedName, *TLI);
1072 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsUnlockedName);
10561073
10571074 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
10581075 CI->setCallingConv(Fn->getCallingConv());
10721089 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
10731090
10741091 if (File->getType()->isPointerTy())
1075 inferLibFuncAttributes(*M->getFunction(FWriteName), *TLI);
1092 inferLibFuncAttributes(M, FWriteName, *TLI);
10761093 CallInst *CI =
10771094 B.CreateCall(F, {castToCStr(Ptr, B), Size,
10781095 ConstantInt::get(DL.getIntPtrType(Context), 1), File});
10881105 return nullptr;
10891106
10901107 Module *M = B.GetInsertBlock()->getModule();
1108 StringRef MallocName = TLI->getName(LibFunc_malloc);
10911109 LLVMContext &Context = B.GetInsertBlock()->getContext();
1092 Value *Malloc = M->getOrInsertFunction("malloc", B.getInt8PtrTy(),
1110 Value *Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(),
10931111 DL.getIntPtrType(Context));
1094 inferLibFuncAttributes(*M->getFunction("malloc"), *TLI);
1095 CallInst *CI = B.CreateCall(Malloc, Num, "malloc");
1112 inferLibFuncAttributes(M, MallocName, *TLI);
1113 CallInst *CI = B.CreateCall(Malloc, Num, MallocName);
10961114
10971115 if (const Function *F = dyn_cast(Malloc->stripPointerCasts()))
10981116 CI->setCallingConv(F->getCallingConv());
11061124 return nullptr;
11071125
11081126 Module *M = B.GetInsertBlock()->getModule();
1127 StringRef CallocName = TLI.getName(LibFunc_calloc);
11091128 const DataLayout &DL = M->getDataLayout();
11101129 IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext()));
1111 Value *Calloc = M->getOrInsertFunction("calloc", Attrs, B.getInt8PtrTy(),
1130 Value *Calloc = M->getOrInsertFunction(CallocName, Attrs, B.getInt8PtrTy(),
11121131 PtrType, PtrType);
1113 inferLibFuncAttributes(*M->getFunction("calloc"), TLI);
1114 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, "calloc");
1132 inferLibFuncAttributes(M, CallocName, TLI);
1133 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
11151134
11161135 if (const auto *F = dyn_cast(Calloc->stripPointerCasts()))
11171136 CI->setCallingConv(F->getCallingConv());
11331152 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
11341153
11351154 if (File->getType()->isPointerTy())
1136 inferLibFuncAttributes(*M->getFunction(FWriteUnlockedName), *TLI);
1155 inferLibFuncAttributes(M, FWriteUnlockedName, *TLI);
11371156 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
11381157
11391158 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
11471166 return nullptr;
11481167
11491168 Module *M = B.GetInsertBlock()->getModule();
1169 StringRef FGetCUnlockedName = TLI->getName(LibFunc_fgetc_unlocked);
11501170 Constant *F =
1151 M->getOrInsertFunction("fgetc_unlocked", B.getInt32Ty(), File->getType());
1171 M->getOrInsertFunction(FGetCUnlockedName, B.getInt32Ty(), File->getType());
11521172 if (File->getType()->isPointerTy())
1153 inferLibFuncAttributes(*M->getFunction("fgetc_unlocked"), *TLI);
1154 CallInst *CI = B.CreateCall(F, File, "fgetc_unlocked");
1173 inferLibFuncAttributes(M, FGetCUnlockedName, *TLI);
1174 CallInst *CI = B.CreateCall(F, File, FGetCUnlockedName);
11551175
11561176 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
11571177 CI->setCallingConv(Fn->getCallingConv());
11641184 return nullptr;
11651185
11661186 Module *M = B.GetInsertBlock()->getModule();
1187 StringRef FGetSUnlockedName = TLI->getName(LibFunc_fgets_unlocked);
11671188 Constant *F =
1168 M->getOrInsertFunction("fgets_unlocked", B.getInt8PtrTy(),
1189 M->getOrInsertFunction(FGetSUnlockedName, B.getInt8PtrTy(),
11691190 B.getInt8PtrTy(), B.getInt32Ty(), File->getType());
1170 inferLibFuncAttributes(*M->getFunction("fgets_unlocked"), *TLI);
1191 inferLibFuncAttributes(M, FGetSUnlockedName, *TLI);
11711192 CallInst *CI =
1172 B.CreateCall(F, {castToCStr(Str, B), Size, File}, "fgets_unlocked");
1193 B.CreateCall(F, {castToCStr(Str, B), Size, File}, FGetSUnlockedName);
11731194
11741195 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
11751196 CI->setCallingConv(Fn->getCallingConv());
11901211 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
11911212
11921213 if (File->getType()->isPointerTy())
1193 inferLibFuncAttributes(*M->getFunction(FReadUnlockedName), *TLI);
1214 inferLibFuncAttributes(M, FReadUnlockedName, *TLI);
11941215 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
11951216
11961217 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ; RUN: opt < %s -instcombine -S | FileCheck %s
2
3 %struct._IO_FILE = type { i32, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, %struct._IO_marker*, %struct._IO_FILE*, i32, i32, i64, i16, i8, [1 x i8], i8*, i64, i8*, i8*, i8*, i8*, i64, i32, [20 x i8] }
4 %struct._IO_marker = type { %struct._IO_marker*, %struct._IO_FILE*, i32 }
5
6 @stderr = external global %struct._IO_FILE*, align 8
7 @.str = private constant [8 x i8] c"crash!\0A\00", align 1
8
9 @fwrite = alias i64 (i8*, i64, i64, %struct._IO_FILE*), i64 (i8*, i64, i64, %struct._IO_FILE*)* @__fwrite_alias
10
11 define i64 @__fwrite_alias(i8* %ptr, i64 %size, i64 %n, %struct._IO_FILE* %s) {
12 ; CHECK-LABEL: @__fwrite_alias(
13 ; CHECK-NEXT: entry:
14 ; CHECK-NEXT: ret i64 0
15 ;
16 entry:
17 %ptr.addr = alloca i8*, align 8
18 %size.addr = alloca i64, align 8
19 %n.addr = alloca i64, align 8
20 %s.addr = alloca %struct._IO_FILE*, align 8
21 store i8* %ptr, i8** %ptr.addr, align 8
22 store i64 %size, i64* %size.addr, align 8
23 store i64 %n, i64* %n.addr, align 8
24 store %struct._IO_FILE* %s, %struct._IO_FILE** %s.addr, align 8
25 ret i64 0
26 }
27
28 define void @foo() {
29 ; CHECK-LABEL: @foo(
30 ; CHECK-NEXT: entry:
31 ; CHECK-NEXT: [[TMP0:%.*]] = load %struct._IO_FILE*, %struct._IO_FILE** @stderr, align 8
32 ; CHECK-NEXT: [[TMP1:%.*]] = call i64 @__fwrite_alias(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 7, i64 1, %struct._IO_FILE* [[TMP0]])
33 ; CHECK-NEXT: ret void
34 ;
35 entry:
36 %retval = alloca i32, align 4
37 store i32 0, i32* %retval, align 4
38 %0 = load %struct._IO_FILE*, %struct._IO_FILE** @stderr, align 8
39 %call = call i32 (%struct._IO_FILE*, i8*, ...) @fprintf(%struct._IO_FILE* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0))
40 ret void
41 }
42
43 declare i32 @fprintf(%struct._IO_FILE*, i8*, ...)