llvm.org GIT mirror llvm / 741ce0d
NFC: Pull out a function to reduce some duplication Part of https://reviews.llvm.org/D62358 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@362271 91177308-0d34-0410-b5e6-96231b3b80d8 Erik Pilkington 4 months ago
3 changed file(s) with 88 addition(s) and 127 deletion(s). Raw diff Collapse all Expand all
6969 /// Emit a call to the strcpy function to the builder, for the specified
7070 /// pointer arguments.
7171 Value *emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
72 const TargetLibraryInfo *TLI, StringRef Name = "strcpy");
72 const TargetLibraryInfo *TLI);
73
74 /// Emit a call to the stpcpy function to the builder, for the specified
75 /// pointer arguments.
76 Value *emitStpCpy(Value *Dst, Value *Src, IRBuilder<> &B,
77 const TargetLibraryInfo *TLI);
7378
7479 /// Emit a call to the strncpy function to the builder, for the specified
7580 /// pointer arguments and length.
7681 Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
77 const TargetLibraryInfo *TLI, StringRef Name = "strncpy");
82 const TargetLibraryInfo *TLI);
83
84 /// Emit a call to the stpncpy function to the builder, for the specified
85 /// pointer arguments and length.
86 Value *emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
87 const TargetLibraryInfo *TLI);
7888
7989 /// Emit a call to the __memcpy_chk function to the builder. This expects that
8090 /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers.
788788 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
789789 }
790790
791 static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType,
792 ArrayRef ParamTypes,
793 ArrayRef Operands, IRBuilder<> &B,
794 const TargetLibraryInfo *TLI,
795 bool IsVaArgs = false) {
796 if (!TLI->has(TheLibFunc))
797 return nullptr;
798
799 Module *M = B.GetInsertBlock()->getModule();
800 StringRef FuncName = TLI->getName(TheLibFunc);
801 FunctionType *FuncType = FunctionType::get(ReturnType, ParamTypes, IsVaArgs);
802 FunctionCallee Callee = M->getOrInsertFunction(FuncName, FuncType);
803 inferLibFuncAttributes(M, FuncName, *TLI);
804 CallInst *CI = B.CreateCall(Callee, Operands, FuncName);
805 if (const Function *F =
806 dyn_cast(Callee.getCallee()->stripPointerCasts()))
807 CI->setCallingConv(F->getCallingConv());
808 return CI;
809 }
810
791811 Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
792812 const TargetLibraryInfo *TLI) {
793 if (!TLI->has(LibFunc_strlen))
794 return nullptr;
795
796 Module *M = B.GetInsertBlock()->getModule();
797 StringRef StrlenName = TLI->getName(LibFunc_strlen);
798813 LLVMContext &Context = B.GetInsertBlock()->getContext();
799 FunctionCallee StrLen = M->getOrInsertFunction(
800 StrlenName, DL.getIntPtrType(Context), B.getInt8PtrTy());
801 inferLibFuncAttributes(M, StrlenName, *TLI);
802 CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), StrlenName);
803 if (const Function *F =
804 dyn_cast(StrLen.getCallee()->stripPointerCasts()))
805 CI->setCallingConv(F->getCallingConv());
806
807 return CI;
814 return emitLibCall(LibFunc_strlen, DL.getIntPtrType(Context),
815 B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI);
808816 }
809817
810818 Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
811819 const TargetLibraryInfo *TLI) {
812 if (!TLI->has(LibFunc_strchr))
813 return nullptr;
814
815 Module *M = B.GetInsertBlock()->getModule();
816 StringRef StrChrName = TLI->getName(LibFunc_strchr);
817820 Type *I8Ptr = B.getInt8PtrTy();
818821 Type *I32Ty = B.getInt32Ty();
819 FunctionCallee StrChr =
820 M->getOrInsertFunction(StrChrName, I8Ptr, I8Ptr, I32Ty);
821 inferLibFuncAttributes(M, StrChrName, *TLI);
822 CallInst *CI = B.CreateCall(
823 StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, StrChrName);
824 if (const Function *F =
825 dyn_cast(StrChr.getCallee()->stripPointerCasts()))
826 CI->setCallingConv(F->getCallingConv());
827 return CI;
822 return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, I32Ty},
823 {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, B, TLI);
828824 }
829825
830826 Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
831827 const DataLayout &DL, const TargetLibraryInfo *TLI) {
832 if (!TLI->has(LibFunc_strncmp))
833 return nullptr;
834
835 Module *M = B.GetInsertBlock()->getModule();
836 StringRef StrNCmpName = TLI->getName(LibFunc_strncmp);
837828 LLVMContext &Context = B.GetInsertBlock()->getContext();
838 FunctionCallee StrNCmp =
839 M->getOrInsertFunction(StrNCmpName, B.getInt32Ty(), B.getInt8PtrTy(),
840 B.getInt8PtrTy(), DL.getIntPtrType(Context));
841 inferLibFuncAttributes(M, StrNCmpName, *TLI);
842 CallInst *CI = B.CreateCall(
843 StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, StrNCmpName);
844
845 if (const Function *F =
846 dyn_cast(StrNCmp.getCallee()->stripPointerCasts()))
847 CI->setCallingConv(F->getCallingConv());
848
849 return CI;
829 return emitLibCall(
830 LibFunc_strncmp, B.getInt32Ty(),
831 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
832 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
850833 }
851834
852835 Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
853 const TargetLibraryInfo *TLI, StringRef Name) {
854 if (!TLI->has(LibFunc_strcpy))
855 return nullptr;
856
857 Module *M = B.GetInsertBlock()->getModule();
836 const TargetLibraryInfo *TLI) {
858837 Type *I8Ptr = B.getInt8PtrTy();
859 FunctionCallee StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
860 inferLibFuncAttributes(M, Name, *TLI);
861 CallInst *CI =
862 B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
863 if (const Function *F =
864 dyn_cast(StrCpy.getCallee()->stripPointerCasts()))
865 CI->setCallingConv(F->getCallingConv());
866 return CI;
838 return emitLibCall(LibFunc_strcpy, I8Ptr, {I8Ptr, I8Ptr},
839 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI);
840 }
841
842 Value *llvm::emitStpCpy(Value *Dst, Value *Src, IRBuilder<> &B,
843 const TargetLibraryInfo *TLI) {
844 Type *I8Ptr = B.getInt8PtrTy();
845 return emitLibCall(LibFunc_stpcpy, I8Ptr, {I8Ptr, I8Ptr},
846 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI);
867847 }
868848
869849 Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
870 const TargetLibraryInfo *TLI, StringRef Name) {
871 if (!TLI->has(LibFunc_strncpy))
872 return nullptr;
873
874 Module *M = B.GetInsertBlock()->getModule();
850 const TargetLibraryInfo *TLI) {
875851 Type *I8Ptr = B.getInt8PtrTy();
876 FunctionCallee StrNCpy =
877 M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, Len->getType());
878 inferLibFuncAttributes(M, Name, *TLI);
879 CallInst *CI = B.CreateCall(
880 StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, Name);
881 if (const Function *F =
882 dyn_cast(StrNCpy.getCallee()->stripPointerCasts()))
883 CI->setCallingConv(F->getCallingConv());
884 return CI;
852 return emitLibCall(LibFunc_strncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()},
853 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI);
854 }
855
856 Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
857 const TargetLibraryInfo *TLI) {
858 Type *I8Ptr = B.getInt8PtrTy();
859 return emitLibCall(LibFunc_stpncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()},
860 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI);
885861 }
886862
887863 Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
910886
911887 Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
912888 const DataLayout &DL, const TargetLibraryInfo *TLI) {
913 if (!TLI->has(LibFunc_memchr))
914 return nullptr;
915
916 Module *M = B.GetInsertBlock()->getModule();
917 StringRef MemChrName = TLI->getName(LibFunc_memchr);
918889 LLVMContext &Context = B.GetInsertBlock()->getContext();
919 FunctionCallee MemChr =
920 M->getOrInsertFunction(MemChrName, B.getInt8PtrTy(), B.getInt8PtrTy(),
921 B.getInt32Ty(), DL.getIntPtrType(Context));
922 inferLibFuncAttributes(M, MemChrName, *TLI);
923 CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, MemChrName);
924
925 if (const Function *F =
926 dyn_cast(MemChr.getCallee()->stripPointerCasts()))
927 CI->setCallingConv(F->getCallingConv());
928
929 return CI;
930 }
931
932 // Common code for memcmp() and bcmp(), which have the exact same properties,
933 // just a slight difference in semantics.
934 static Value *emitMemCmpOrBcmp(llvm::LibFunc libfunc, Value *Ptr1, Value *Ptr2,
935 Value *Len, IRBuilder<> &B, const DataLayout &DL,
936 const TargetLibraryInfo *TLI) {
937 if (!TLI->has(libfunc))
938 return nullptr;
939
940 Module *M = B.GetInsertBlock()->getModule();
941 StringRef CmpFnName = TLI->getName(libfunc);
942 LLVMContext &Context = B.GetInsertBlock()->getContext();
943 FunctionCallee CmpFn =
944 M->getOrInsertFunction(CmpFnName, B.getInt32Ty(), B.getInt8PtrTy(),
945 B.getInt8PtrTy(), DL.getIntPtrType(Context));
946 inferLibFuncAttributes(M, CmpFnName, *TLI);
947 CallInst *CI = B.CreateCall(
948 CmpFn, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, CmpFnName);
949
950 if (const Function *F =
951 dyn_cast(CmpFn.getCallee()->stripPointerCasts()))
952 CI->setCallingConv(F->getCallingConv());
953
954 return CI;
890 return emitLibCall(
891 LibFunc_memchr, B.getInt8PtrTy(),
892 {B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context)},
893 {castToCStr(Ptr, B), Val, Len}, B, TLI);
955894 }
956895
957896 Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
958897 const DataLayout &DL, const TargetLibraryInfo *TLI) {
959 return emitMemCmpOrBcmp(LibFunc_memcmp, Ptr1, Ptr2, Len, B, DL, TLI);
898 LLVMContext &Context = B.GetInsertBlock()->getContext();
899 return emitLibCall(
900 LibFunc_memcmp, B.getInt32Ty(),
901 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
902 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
960903 }
961904
962905 Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
963906 const DataLayout &DL, const TargetLibraryInfo *TLI) {
964 return emitMemCmpOrBcmp(LibFunc_bcmp, Ptr1, Ptr2, Len, B, DL, TLI);
907 LLVMContext &Context = B.GetInsertBlock()->getContext();
908 return emitLibCall(
909 LibFunc_bcmp, B.getInt32Ty(),
910 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
911 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
965912 }
966913
967914 /// Append a suffix to the function name according to the type of 'Op'.
28862886 Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
28872887 IRBuilder<> &B,
28882888 LibFunc Func) {
2889 Function *Callee = CI->getCalledFunction();
2890 StringRef Name = Callee->getName();
28912889 const DataLayout &DL = CI->getModule()->getDataLayout();
28922890 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1),
28932891 *ObjSize = CI->getArgOperand(2);
29032901 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
29042902 // TODO: It might be nice to get a maximum length out of the possible
29052903 // string lengths for varying.
2906 if (isFortifiedCallFoldable(CI, 2, 1, true))
2907 return emitStrCpy(Dst, Src, B, TLI, Name.substr(2, 6));
2904 if (isFortifiedCallFoldable(CI, 2, 1, true)) {
2905 if (Func == LibFunc_strcpy_chk)
2906 return emitStrCpy(Dst, Src, B, TLI);
2907 else
2908 return emitStpCpy(Dst, Src, B, TLI);
2909 }
29082910
29092911 if (OnlyLowerUnknownSize)
29102912 return nullptr;
29272929 Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
29282930 IRBuilder<> &B,
29292931 LibFunc Func) {
2930 Function *Callee = CI->getCalledFunction();
2931 StringRef Name = Callee->getName();
29322932 if (isFortifiedCallFoldable(CI, 3, 2, false)) {
2933 Value *Ret = emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
2934 CI->getArgOperand(2), B, TLI, Name.substr(2, 7));
2935 return Ret;
2936 }
2933 if (Func == LibFunc_strncpy_chk)
2934 return emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
2935 CI->getArgOperand(2), B, TLI);
2936 else
2937 return emitStpNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
2938 CI->getArgOperand(2), B, TLI);
2939 }
2940
29372941 return nullptr;
29382942 }
29392943