Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 1 | //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements some functions that will create standard C libcalls. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Transforms/Utils/BuildLibCalls.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Constants.h" |
| 17 | #include "llvm/IR/DataLayout.h" |
| 18 | #include "llvm/IR/Function.h" |
| 19 | #include "llvm/IR/IRBuilder.h" |
| 20 | #include "llvm/IR/Intrinsics.h" |
| 21 | #include "llvm/IR/LLVMContext.h" |
| 22 | #include "llvm/IR/Module.h" |
| 23 | #include "llvm/IR/Type.h" |
Eli Friedman | 489c0ff | 2011-11-17 01:27:36 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetLibraryInfo.h" |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*. |
| 29 | Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) { |
Matt Arsenault | be55888 | 2014-04-23 20:58:57 +0000 | [diff] [blame] | 30 | unsigned AS = V->getType()->getPointerAddressSpace(); |
| 31 | return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr"); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /// EmitStrLen - Emit a call to the strlen function to the builder, for the |
| 35 | /// specified pointer. This always returns an integer value of size intptr_t. |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 36 | Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 37 | const TargetLibraryInfo *TLI) { |
| 38 | if (!TLI->has(LibFunc::strlen)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 39 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 40 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 41 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 42 | AttributeSet AS[2]; |
| 43 | AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); |
Bill Wendling | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 44 | Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind }; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame^] | 45 | AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 46 | |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 47 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 48 | Constant *StrLen = M->getOrInsertFunction("strlen", |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 49 | AttributeSet::get(M->getContext(), |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 50 | AS), |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 51 | TD->getIntPtrType(Context), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 52 | B.getInt8PtrTy(), |
| 53 | NULL); |
| 54 | CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 55 | if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts())) |
| 56 | CI->setCallingConv(F->getCallingConv()); |
| 57 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 58 | return CI; |
| 59 | } |
| 60 | |
Nuno Lopes | 7ba5b98 | 2012-07-25 17:18:59 +0000 | [diff] [blame] | 61 | /// EmitStrNLen - Emit a call to the strnlen function to the builder, for the |
| 62 | /// specified pointer. Ptr is required to be some pointer type, MaxLen must |
| 63 | /// be of size_t type, and the return value has 'intptr_t' type. |
| 64 | Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 65 | const DataLayout *TD, const TargetLibraryInfo *TLI) { |
Nuno Lopes | 7ba5b98 | 2012-07-25 17:18:59 +0000 | [diff] [blame] | 66 | if (!TLI->has(LibFunc::strnlen)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 67 | return nullptr; |
Nuno Lopes | 7ba5b98 | 2012-07-25 17:18:59 +0000 | [diff] [blame] | 68 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 69 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 70 | AttributeSet AS[2]; |
| 71 | AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); |
Bill Wendling | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 72 | Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind }; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame^] | 73 | AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs); |
Nuno Lopes | 7ba5b98 | 2012-07-25 17:18:59 +0000 | [diff] [blame] | 74 | |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 75 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 76 | Constant *StrNLen = M->getOrInsertFunction("strnlen", |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 77 | AttributeSet::get(M->getContext(), |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 78 | AS), |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 79 | TD->getIntPtrType(Context), |
Nuno Lopes | 7ba5b98 | 2012-07-25 17:18:59 +0000 | [diff] [blame] | 80 | B.getInt8PtrTy(), |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 81 | TD->getIntPtrType(Context), |
Nuno Lopes | 7ba5b98 | 2012-07-25 17:18:59 +0000 | [diff] [blame] | 82 | NULL); |
| 83 | CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 84 | if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts())) |
| 85 | CI->setCallingConv(F->getCallingConv()); |
| 86 | |
Nuno Lopes | 7ba5b98 | 2012-07-25 17:18:59 +0000 | [diff] [blame] | 87 | return CI; |
| 88 | } |
| 89 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 90 | /// EmitStrChr - Emit a call to the strchr function to the builder, for the |
| 91 | /// specified pointer and character. Ptr is required to be some pointer type, |
| 92 | /// and the return value has 'i8*' type. |
| 93 | Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 94 | const DataLayout *TD, const TargetLibraryInfo *TLI) { |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 95 | if (!TLI->has(LibFunc::strchr)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 96 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 97 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 98 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 99 | Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind }; |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 100 | AttributeSet AS = |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame^] | 101 | AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 103 | Type *I8Ptr = B.getInt8PtrTy(); |
| 104 | Type *I32Ty = B.getInt32Ty(); |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 105 | Constant *StrChr = M->getOrInsertFunction("strchr", |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 106 | AttributeSet::get(M->getContext(), |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 107 | AS), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 108 | I8Ptr, I8Ptr, I32Ty, NULL); |
| 109 | CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B), |
| 110 | ConstantInt::get(I32Ty, C), "strchr"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 111 | if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts())) |
| 112 | CI->setCallingConv(F->getCallingConv()); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 113 | return CI; |
| 114 | } |
| 115 | |
Benjamin Kramer | 1118860 | 2010-06-15 21:34:25 +0000 | [diff] [blame] | 116 | /// EmitStrNCmp - Emit a call to the strncmp function to the builder. |
| 117 | Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 118 | IRBuilder<> &B, const DataLayout *TD, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 119 | const TargetLibraryInfo *TLI) { |
| 120 | if (!TLI->has(LibFunc::strncmp)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 121 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 122 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 123 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 124 | AttributeSet AS[3]; |
| 125 | AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); |
| 126 | AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); |
Bill Wendling | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 127 | Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind }; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame^] | 128 | AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs); |
Benjamin Kramer | 1118860 | 2010-06-15 21:34:25 +0000 | [diff] [blame] | 129 | |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 130 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 131 | Value *StrNCmp = M->getOrInsertFunction("strncmp", |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 132 | AttributeSet::get(M->getContext(), |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 133 | AS), |
Benjamin Kramer | 1118860 | 2010-06-15 21:34:25 +0000 | [diff] [blame] | 134 | B.getInt32Ty(), |
| 135 | B.getInt8PtrTy(), |
| 136 | B.getInt8PtrTy(), |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 137 | TD->getIntPtrType(Context), NULL); |
Benjamin Kramer | 1118860 | 2010-06-15 21:34:25 +0000 | [diff] [blame] | 138 | CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B), |
| 139 | CastToCStr(Ptr2, B), Len, "strncmp"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 140 | |
| 141 | if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts())) |
| 142 | CI->setCallingConv(F->getCallingConv()); |
| 143 | |
Benjamin Kramer | 1118860 | 2010-06-15 21:34:25 +0000 | [diff] [blame] | 144 | return CI; |
| 145 | } |
| 146 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 147 | /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the |
| 148 | /// specified pointer arguments. |
| 149 | Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 150 | const DataLayout *TD, const TargetLibraryInfo *TLI, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 151 | StringRef Name) { |
| 152 | if (!TLI->has(LibFunc::strcpy)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 153 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 154 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 155 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 156 | AttributeSet AS[2]; |
| 157 | AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); |
| 158 | AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 159 | Attribute::NoUnwind); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 160 | Type *I8Ptr = B.getInt8PtrTy(); |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 161 | Value *StrCpy = M->getOrInsertFunction(Name, |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 162 | AttributeSet::get(M->getContext(), AS), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 163 | I8Ptr, I8Ptr, I8Ptr, NULL); |
| 164 | CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B), |
Benjamin Kramer | 2fc3956 | 2010-03-11 20:45:13 +0000 | [diff] [blame] | 165 | Name); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 166 | if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts())) |
| 167 | CI->setCallingConv(F->getCallingConv()); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 168 | return CI; |
| 169 | } |
| 170 | |
Eric Christopher | 103e3ef | 2010-03-11 17:45:38 +0000 | [diff] [blame] | 171 | /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the |
Eric Christopher | 43dc11c | 2010-03-11 01:25:07 +0000 | [diff] [blame] | 172 | /// specified pointer arguments. |
| 173 | Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 174 | IRBuilder<> &B, const DataLayout *TD, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 175 | const TargetLibraryInfo *TLI, StringRef Name) { |
| 176 | if (!TLI->has(LibFunc::strncpy)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 177 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 178 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 179 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 180 | AttributeSet AS[2]; |
| 181 | AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); |
| 182 | AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 183 | Attribute::NoUnwind); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 184 | Type *I8Ptr = B.getInt8PtrTy(); |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 185 | Value *StrNCpy = M->getOrInsertFunction(Name, |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 186 | AttributeSet::get(M->getContext(), |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 187 | AS), |
Eric Christopher | e8b281c | 2010-04-07 23:00:07 +0000 | [diff] [blame] | 188 | I8Ptr, I8Ptr, I8Ptr, |
| 189 | Len->getType(), NULL); |
Eric Christopher | 43dc11c | 2010-03-11 01:25:07 +0000 | [diff] [blame] | 190 | CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B), |
| 191 | Len, "strncpy"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 192 | if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts())) |
| 193 | CI->setCallingConv(F->getCallingConv()); |
Eric Christopher | 43dc11c | 2010-03-11 01:25:07 +0000 | [diff] [blame] | 194 | return CI; |
| 195 | } |
| 196 | |
Evan Cheng | d9e8223 | 2010-03-23 15:48:04 +0000 | [diff] [blame] | 197 | /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder. |
| 198 | /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src |
| 199 | /// are pointers. |
| 200 | Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 201 | IRBuilder<> &B, const DataLayout *TD, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 202 | const TargetLibraryInfo *TLI) { |
| 203 | if (!TLI->has(LibFunc::memcpy_chk)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 204 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 205 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 206 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 207 | AttributeSet AS; |
| 208 | AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 209 | Attribute::NoUnwind); |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 210 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Evan Cheng | d9e8223 | 2010-03-23 15:48:04 +0000 | [diff] [blame] | 211 | Value *MemCpy = M->getOrInsertFunction("__memcpy_chk", |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 212 | AttributeSet::get(M->getContext(), AS), |
Evan Cheng | d9e8223 | 2010-03-23 15:48:04 +0000 | [diff] [blame] | 213 | B.getInt8PtrTy(), |
| 214 | B.getInt8PtrTy(), |
| 215 | B.getInt8PtrTy(), |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 216 | TD->getIntPtrType(Context), |
| 217 | TD->getIntPtrType(Context), NULL); |
Evan Cheng | d9e8223 | 2010-03-23 15:48:04 +0000 | [diff] [blame] | 218 | Dst = CastToCStr(Dst, B); |
| 219 | Src = CastToCStr(Src, B); |
| 220 | CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 221 | if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts())) |
| 222 | CI->setCallingConv(F->getCallingConv()); |
Evan Cheng | d9e8223 | 2010-03-23 15:48:04 +0000 | [diff] [blame] | 223 | return CI; |
| 224 | } |
| 225 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 226 | /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is |
| 227 | /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value. |
| 228 | Value *llvm::EmitMemChr(Value *Ptr, Value *Val, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 229 | Value *Len, IRBuilder<> &B, const DataLayout *TD, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 230 | const TargetLibraryInfo *TLI) { |
| 231 | if (!TLI->has(LibFunc::memchr)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 232 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 233 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 234 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 235 | AttributeSet AS; |
Bill Wendling | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 236 | Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind }; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame^] | 237 | AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs); |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 238 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 239 | Value *MemChr = M->getOrInsertFunction("memchr", |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 240 | AttributeSet::get(M->getContext(), AS), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 241 | B.getInt8PtrTy(), |
| 242 | B.getInt8PtrTy(), |
| 243 | B.getInt32Ty(), |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 244 | TD->getIntPtrType(Context), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 245 | NULL); |
| 246 | CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 247 | |
| 248 | if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts())) |
| 249 | CI->setCallingConv(F->getCallingConv()); |
| 250 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 251 | return CI; |
| 252 | } |
| 253 | |
| 254 | /// EmitMemCmp - Emit a call to the memcmp function. |
| 255 | Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 256 | Value *Len, IRBuilder<> &B, const DataLayout *TD, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 257 | const TargetLibraryInfo *TLI) { |
| 258 | if (!TLI->has(LibFunc::memcmp)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 259 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 260 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 261 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 262 | AttributeSet AS[3]; |
| 263 | AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); |
| 264 | AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); |
Bill Wendling | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 265 | Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind }; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame^] | 266 | AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 267 | |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 268 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 269 | Value *MemCmp = M->getOrInsertFunction("memcmp", |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 270 | AttributeSet::get(M->getContext(), AS), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 271 | B.getInt32Ty(), |
| 272 | B.getInt8PtrTy(), |
| 273 | B.getInt8PtrTy(), |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 274 | TD->getIntPtrType(Context), NULL); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 275 | CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), |
| 276 | Len, "memcmp"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 277 | |
| 278 | if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts())) |
| 279 | CI->setCallingConv(F->getCallingConv()); |
| 280 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 281 | return CI; |
| 282 | } |
| 283 | |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 284 | /// Append a suffix to the function name according to the type of 'Op'. |
| 285 | static void AppendTypeSuffix(Value *Op, StringRef &Name, SmallString<20> &NameBuffer) { |
| 286 | if (!Op->getType()->isDoubleTy()) { |
| 287 | NameBuffer += Name; |
| 288 | |
| 289 | if (Op->getType()->isFloatTy()) |
| 290 | NameBuffer += 'f'; |
| 291 | else |
| 292 | NameBuffer += 'l'; |
| 293 | |
| 294 | Name = NameBuffer; |
| 295 | } |
| 296 | return; |
| 297 | } |
| 298 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 299 | /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g. |
| 300 | /// 'floor'). This function is known to take a single of type matching 'Op' and |
| 301 | /// returns one value with the same type. If 'Op' is a long double, 'l' is |
| 302 | /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix. |
Benjamin Kramer | b106bcc | 2011-11-15 19:12:09 +0000 | [diff] [blame] | 303 | Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 304 | const AttributeSet &Attrs) { |
Benjamin Kramer | b106bcc | 2011-11-15 19:12:09 +0000 | [diff] [blame] | 305 | SmallString<20> NameBuffer; |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 306 | AppendTypeSuffix(Op, Name, NameBuffer); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 307 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 308 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 309 | Value *Callee = M->getOrInsertFunction(Name, Op->getType(), |
| 310 | Op->getType(), NULL); |
| 311 | CallInst *CI = B.CreateCall(Callee, Op, Name); |
| 312 | CI->setAttributes(Attrs); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 313 | if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) |
| 314 | CI->setCallingConv(F->getCallingConv()); |
| 315 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 316 | return CI; |
| 317 | } |
| 318 | |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 319 | /// EmitBinaryFloatFnCall - Emit a call to the binary function named 'Name' |
| 320 | /// (e.g. 'fmin'). This function is known to take type matching 'Op1' and 'Op2' |
| 321 | /// and return one value with the same type. If 'Op1/Op2' are long double, 'l' |
| 322 | /// is added as the suffix of name, if 'Op1/Op2' is a float, we add a 'f' |
| 323 | /// suffix. |
| 324 | Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, |
| 325 | IRBuilder<> &B, const AttributeSet &Attrs) { |
| 326 | SmallString<20> NameBuffer; |
| 327 | AppendTypeSuffix(Op1, Name, NameBuffer); |
| 328 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 329 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 330 | Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), |
| 331 | Op1->getType(), Op2->getType(), NULL); |
| 332 | CallInst *CI = B.CreateCall2(Callee, Op1, Op2, Name); |
| 333 | CI->setAttributes(Attrs); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 334 | if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) |
| 335 | CI->setCallingConv(F->getCallingConv()); |
| 336 | |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 337 | return CI; |
| 338 | } |
| 339 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 340 | /// EmitPutChar - Emit a call to the putchar function. This assumes that Char |
| 341 | /// is an integer. |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 342 | Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 343 | const TargetLibraryInfo *TLI) { |
| 344 | if (!TLI->has(LibFunc::putchar)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 345 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 346 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 347 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 348 | Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(), |
| 349 | B.getInt32Ty(), NULL); |
| 350 | CallInst *CI = B.CreateCall(PutChar, |
| 351 | B.CreateIntCast(Char, |
| 352 | B.getInt32Ty(), |
| 353 | /*isSigned*/true, |
| 354 | "chari"), |
| 355 | "putchar"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 356 | |
| 357 | if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts())) |
| 358 | CI->setCallingConv(F->getCallingConv()); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 359 | return CI; |
| 360 | } |
| 361 | |
| 362 | /// EmitPutS - Emit a call to the puts function. This assumes that Str is |
| 363 | /// some pointer. |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 364 | Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 365 | const TargetLibraryInfo *TLI) { |
| 366 | if (!TLI->has(LibFunc::puts)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 367 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 368 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 369 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 370 | AttributeSet AS[2]; |
| 371 | AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); |
| 372 | AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 373 | Attribute::NoUnwind); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 374 | |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 375 | Value *PutS = M->getOrInsertFunction("puts", |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 376 | AttributeSet::get(M->getContext(), AS), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 377 | B.getInt32Ty(), |
| 378 | B.getInt8PtrTy(), |
| 379 | NULL); |
| 380 | CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 381 | if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts())) |
| 382 | CI->setCallingConv(F->getCallingConv()); |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 383 | return CI; |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is |
| 387 | /// an integer and File is a pointer to FILE. |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 388 | Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 389 | const DataLayout *TD, const TargetLibraryInfo *TLI) { |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 390 | if (!TLI->has(LibFunc::fputc)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 391 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 392 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 393 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 394 | AttributeSet AS[2]; |
| 395 | AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); |
| 396 | AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 397 | Attribute::NoUnwind); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 398 | Constant *F; |
| 399 | if (File->getType()->isPointerTy()) |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 400 | F = M->getOrInsertFunction("fputc", |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 401 | AttributeSet::get(M->getContext(), AS), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 402 | B.getInt32Ty(), |
| 403 | B.getInt32Ty(), File->getType(), |
| 404 | NULL); |
| 405 | else |
| 406 | F = M->getOrInsertFunction("fputc", |
| 407 | B.getInt32Ty(), |
| 408 | B.getInt32Ty(), |
| 409 | File->getType(), NULL); |
| 410 | Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true, |
| 411 | "chari"); |
| 412 | CallInst *CI = B.CreateCall2(F, Char, File, "fputc"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 413 | |
| 414 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 415 | CI->setCallingConv(Fn->getCallingConv()); |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 416 | return CI; |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /// EmitFPutS - Emit a call to the puts function. Str is required to be a |
| 420 | /// pointer and File is a pointer to FILE. |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 421 | Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 422 | const DataLayout *TD, const TargetLibraryInfo *TLI) { |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 423 | if (!TLI->has(LibFunc::fputs)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 424 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 425 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 426 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 427 | AttributeSet AS[3]; |
| 428 | AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); |
| 429 | AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); |
| 430 | AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 431 | Attribute::NoUnwind); |
Eli Friedman | 489c0ff | 2011-11-17 01:27:36 +0000 | [diff] [blame] | 432 | StringRef FPutsName = TLI->getName(LibFunc::fputs); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 433 | Constant *F; |
| 434 | if (File->getType()->isPointerTy()) |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 435 | F = M->getOrInsertFunction(FPutsName, |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 436 | AttributeSet::get(M->getContext(), AS), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 437 | B.getInt32Ty(), |
| 438 | B.getInt8PtrTy(), |
| 439 | File->getType(), NULL); |
| 440 | else |
Eli Friedman | 489c0ff | 2011-11-17 01:27:36 +0000 | [diff] [blame] | 441 | F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 442 | B.getInt8PtrTy(), |
| 443 | File->getType(), NULL); |
| 444 | CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 445 | |
| 446 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 447 | CI->setCallingConv(Fn->getCallingConv()); |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 448 | return CI; |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is |
| 452 | /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE. |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 453 | Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 454 | IRBuilder<> &B, const DataLayout *TD, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 455 | const TargetLibraryInfo *TLI) { |
| 456 | if (!TLI->has(LibFunc::fwrite)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 457 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 458 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 459 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 460 | AttributeSet AS[3]; |
| 461 | AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); |
| 462 | AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture); |
| 463 | AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 464 | Attribute::NoUnwind); |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 465 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Eli Friedman | 489c0ff | 2011-11-17 01:27:36 +0000 | [diff] [blame] | 466 | StringRef FWriteName = TLI->getName(LibFunc::fwrite); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 467 | Constant *F; |
| 468 | if (File->getType()->isPointerTy()) |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 469 | F = M->getOrInsertFunction(FWriteName, |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 470 | AttributeSet::get(M->getContext(), AS), |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 471 | TD->getIntPtrType(Context), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 472 | B.getInt8PtrTy(), |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 473 | TD->getIntPtrType(Context), |
| 474 | TD->getIntPtrType(Context), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 475 | File->getType(), NULL); |
| 476 | else |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 477 | F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 478 | B.getInt8PtrTy(), |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 479 | TD->getIntPtrType(Context), |
| 480 | TD->getIntPtrType(Context), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 481 | File->getType(), NULL); |
| 482 | CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size, |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 483 | ConstantInt::get(TD->getIntPtrType(Context), 1), File); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 484 | |
| 485 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 486 | CI->setCallingConv(Fn->getCallingConv()); |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 487 | return CI; |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 488 | } |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 489 | |
Benjamin Kramer | a956527 | 2010-03-12 20:41:29 +0000 | [diff] [blame] | 490 | SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { } |
| 491 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 492 | bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 493 | const TargetLibraryInfo *TLI) { |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 494 | // We really need DataLayout for later. |
Eric Christopher | 1f272f7 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 495 | if (!TD) return false; |
| 496 | |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 497 | this->CI = CI; |
Eric Christopher | 1f272f7 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 498 | Function *Callee = CI->getCalledFunction(); |
| 499 | StringRef Name = Callee->getName(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 500 | FunctionType *FT = Callee->getFunctionType(); |
Eric Christopher | 1f272f7 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 501 | LLVMContext &Context = CI->getParent()->getContext(); |
Eli Friedman | e217f89 | 2011-05-27 01:00:36 +0000 | [diff] [blame] | 502 | IRBuilder<> B(CI); |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 503 | |
| 504 | if (Name == "__memcpy_chk") { |
Eric Christopher | 1f272f7 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 505 | // Check if this has the right signature. |
| 506 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
| 507 | !FT->getParamType(0)->isPointerTy() || |
| 508 | !FT->getParamType(1)->isPointerTy() || |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 509 | FT->getParamType(2) != TD->getIntPtrType(Context) || |
| 510 | FT->getParamType(3) != TD->getIntPtrType(Context)) |
Eric Christopher | 1f272f7 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 511 | return false; |
Gabor Greif | 2de43a7 | 2010-06-28 12:29:20 +0000 | [diff] [blame] | 512 | |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 513 | if (isFoldable(3, 2, false)) { |
Benjamin Kramer | 84bd73c | 2010-12-27 00:25:32 +0000 | [diff] [blame] | 514 | B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), |
| 515 | CI->getArgOperand(2), 1); |
Gabor Greif | 2de43a7 | 2010-06-28 12:29:20 +0000 | [diff] [blame] | 516 | replaceCall(CI->getArgOperand(0)); |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 517 | return true; |
| 518 | } |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | // Should be similar to memcpy. |
| 523 | if (Name == "__mempcpy_chk") { |
| 524 | return false; |
| 525 | } |
| 526 | |
| 527 | if (Name == "__memmove_chk") { |
Eric Christopher | 1f272f7 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 528 | // Check if this has the right signature. |
| 529 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
| 530 | !FT->getParamType(0)->isPointerTy() || |
| 531 | !FT->getParamType(1)->isPointerTy() || |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 532 | FT->getParamType(2) != TD->getIntPtrType(Context) || |
| 533 | FT->getParamType(3) != TD->getIntPtrType(Context)) |
Eric Christopher | 1f272f7 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 534 | return false; |
Gabor Greif | 2de43a7 | 2010-06-28 12:29:20 +0000 | [diff] [blame] | 535 | |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 536 | if (isFoldable(3, 2, false)) { |
Benjamin Kramer | 84bd73c | 2010-12-27 00:25:32 +0000 | [diff] [blame] | 537 | B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1), |
| 538 | CI->getArgOperand(2), 1); |
Gabor Greif | 2de43a7 | 2010-06-28 12:29:20 +0000 | [diff] [blame] | 539 | replaceCall(CI->getArgOperand(0)); |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 540 | return true; |
| 541 | } |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | if (Name == "__memset_chk") { |
Eric Christopher | 1f272f7 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 546 | // Check if this has the right signature. |
| 547 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
| 548 | !FT->getParamType(0)->isPointerTy() || |
| 549 | !FT->getParamType(1)->isIntegerTy() || |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 550 | FT->getParamType(2) != TD->getIntPtrType(Context) || |
| 551 | FT->getParamType(3) != TD->getIntPtrType(Context)) |
Eric Christopher | 1f272f7 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 552 | return false; |
Gabor Greif | 2de43a7 | 2010-06-28 12:29:20 +0000 | [diff] [blame] | 553 | |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 554 | if (isFoldable(3, 2, false)) { |
Gabor Greif | 2de43a7 | 2010-06-28 12:29:20 +0000 | [diff] [blame] | 555 | Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 556 | false); |
Benjamin Kramer | 84bd73c | 2010-12-27 00:25:32 +0000 | [diff] [blame] | 557 | B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1); |
Gabor Greif | e3ba486 | 2010-06-25 07:58:41 +0000 | [diff] [blame] | 558 | replaceCall(CI->getArgOperand(0)); |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 559 | return true; |
| 560 | } |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") { |
Eric Christopher | 1f272f7 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 565 | // Check if this has the right signature. |
| 566 | if (FT->getNumParams() != 3 || |
| 567 | FT->getReturnType() != FT->getParamType(0) || |
| 568 | FT->getParamType(0) != FT->getParamType(1) || |
| 569 | FT->getParamType(0) != Type::getInt8PtrTy(Context) || |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 570 | FT->getParamType(2) != TD->getIntPtrType(Context)) |
Eric Christopher | 1f272f7 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 571 | return 0; |
| 572 | |
| 573 | |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 574 | // If a) we don't have any length information, or b) we know this will |
| 575 | // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our |
| 576 | // st[rp]cpy_chk call which may fail at runtime if the size is too long. |
| 577 | // TODO: It might be nice to get a maximum length out of the possible |
| 578 | // string lengths for varying. |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 579 | if (isFoldable(2, 1, true)) { |
Gabor Greif | 2de43a7 | 2010-06-28 12:29:20 +0000 | [diff] [blame] | 580 | Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 581 | TLI, Name.substr(2, 6)); |
Nuno Lopes | e722031 | 2012-08-01 16:58:51 +0000 | [diff] [blame] | 582 | if (!Ret) |
Nuno Lopes | a9a8c62 | 2012-08-01 17:13:28 +0000 | [diff] [blame] | 583 | return false; |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 584 | replaceCall(Ret); |
| 585 | return true; |
| 586 | } |
| 587 | return false; |
| 588 | } |
| 589 | |
Eric Christopher | e8b281c | 2010-04-07 23:00:07 +0000 | [diff] [blame] | 590 | if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") { |
Eric Christopher | 1f272f7 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 591 | // Check if this has the right signature. |
| 592 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
| 593 | FT->getParamType(0) != FT->getParamType(1) || |
| 594 | FT->getParamType(0) != Type::getInt8PtrTy(Context) || |
| 595 | !FT->getParamType(2)->isIntegerTy() || |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 596 | FT->getParamType(3) != TD->getIntPtrType(Context)) |
Eric Christopher | 4016dcd | 2010-04-13 16:41:29 +0000 | [diff] [blame] | 597 | return false; |
Gabor Greif | 2de43a7 | 2010-06-28 12:29:20 +0000 | [diff] [blame] | 598 | |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 599 | if (isFoldable(3, 2, false)) { |
Gabor Greif | 2de43a7 | 2010-06-28 12:29:20 +0000 | [diff] [blame] | 600 | Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1), |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 601 | CI->getArgOperand(2), B, TD, TLI, |
| 602 | Name.substr(2, 7)); |
Nuno Lopes | e722031 | 2012-08-01 16:58:51 +0000 | [diff] [blame] | 603 | if (!Ret) |
Nuno Lopes | a9a8c62 | 2012-08-01 17:13:28 +0000 | [diff] [blame] | 604 | return false; |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 605 | replaceCall(Ret); |
| 606 | return true; |
| 607 | } |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | if (Name == "__strcat_chk") { |
| 612 | return false; |
| 613 | } |
| 614 | |
| 615 | if (Name == "__strncat_chk") { |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | return false; |
| 620 | } |