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