Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1 | //===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===// |
| 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 a simple pass that applies a variety of small |
| 11 | // optimizations for calls to specific well-known function calls (e.g. runtime |
Chris Lattner | e9f9a7e | 2009-09-03 05:19:59 +0000 | [diff] [blame] | 12 | // library functions). Any optimization that takes the very simple form |
| 13 | // "replace call to library function with simpler code that provides the same |
| 14 | // result" belongs in this file. |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #define DEBUG_TYPE "simplify-libcalls" |
| 19 | #include "llvm/Transforms/Scalar.h" |
| 20 | #include "llvm/Intrinsics.h" |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 21 | #include "llvm/LLVMContext.h" |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/Pass.h" |
| 24 | #include "llvm/Support/IRBuilder.h" |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
| 27 | #include "llvm/ADT/SmallPtrSet.h" |
| 28 | #include "llvm/ADT/StringMap.h" |
| 29 | #include "llvm/ADT/Statistic.h" |
Daniel Dunbar | 473955f | 2009-07-29 22:00:43 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 56b4f2b | 2008-05-01 06:39:12 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 33 | #include "llvm/Config/config.h" |
| 34 | using namespace llvm; |
| 35 | |
| 36 | STATISTIC(NumSimplified, "Number of library calls simplified"); |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 37 | STATISTIC(NumAnnotated, "Number of attributes added to library functions"); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 38 | |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | // Optimizer Base Class |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
| 43 | /// This class is the abstract base class for the set of optimizations that |
| 44 | /// corresponds to one library call. |
| 45 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 46 | class LibCallOptimization { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 47 | protected: |
| 48 | Function *Caller; |
| 49 | const TargetData *TD; |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 50 | LLVMContext* Context; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 51 | public: |
| 52 | LibCallOptimization() { } |
| 53 | virtual ~LibCallOptimization() {} |
| 54 | |
| 55 | /// CallOptimizer - This pure virtual method is implemented by base classes to |
| 56 | /// do various optimizations. If this returns null then no transformation was |
| 57 | /// performed. If it returns CI, then it transformed the call and CI is to be |
| 58 | /// deleted. If it returns something else, replace CI with the new value and |
| 59 | /// delete CI. |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 60 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 61 | =0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 62 | |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 63 | Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 64 | Caller = CI->getParent()->getParent(); |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 65 | this->TD = TD; |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 66 | if (CI->getCalledFunction()) |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 67 | Context = &CI->getCalledFunction()->getContext(); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 68 | return CallOptimizer(CI->getCalledFunction(), CI, B); |
| 69 | } |
| 70 | |
| 71 | /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*. |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 72 | Value *CastToCStr(Value *V, IRBuilder<> &B); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 73 | |
| 74 | /// EmitStrLen - Emit a call to the strlen function to the builder, for the |
| 75 | /// specified pointer. Ptr is required to be some pointer type, and the |
| 76 | /// return value has 'intptr_t' type. |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 77 | Value *EmitStrLen(Value *Ptr, IRBuilder<> &B); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 79 | /// EmitStrChr - Emit a call to the strchr function to the builder, for the |
| 80 | /// specified pointer and character. Ptr is required to be some pointer type, |
| 81 | /// and the return value has 'i8*' type. |
| 82 | Value *EmitStrChr(Value *Ptr, char C, IRBuilder<> &B); |
Mikhail Glushenkov | ed5cb59 | 2010-01-04 07:55:25 +0000 | [diff] [blame] | 83 | |
Eric Christopher | 7672d04 | 2010-01-23 05:29:06 +0000 | [diff] [blame] | 84 | /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the |
| 85 | /// specified pointer arguments. |
| 86 | Value *EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B); |
| 87 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 88 | /// EmitMemCpy - Emit a call to the memcpy function to the builder. This |
| 89 | /// always expects that the size has type 'intptr_t' and Dst/Src are pointers. |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 90 | Value *EmitMemCpy(Value *Dst, Value *Src, Value *Len, |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 91 | unsigned Align, IRBuilder<> &B); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 92 | |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 93 | /// EmitMemMove - Emit a call to the memmove function to the builder. This |
| 94 | /// always expects that the size has type 'intptr_t' and Dst/Src are pointers. |
| 95 | Value *EmitMemMove(Value *Dst, Value *Src, Value *Len, |
| 96 | unsigned Align, IRBuilder<> &B); |
| 97 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 98 | /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is |
| 99 | /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value. |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 100 | Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B); |
Nick Lewycky | 13a09e2 | 2008-12-21 00:19:21 +0000 | [diff] [blame] | 101 | |
| 102 | /// EmitMemCmp - Emit a call to the memcmp function. |
| 103 | Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B); |
| 104 | |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 105 | /// EmitMemSet - Emit a call to the memset function |
| 106 | Value *EmitMemSet(Value *Dst, Value *Val, Value *Len, IRBuilder<> &B); |
| 107 | |
Mikhail Glushenkov | ed5cb59 | 2010-01-04 07:55:25 +0000 | [diff] [blame] | 108 | /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' |
| 109 | /// (e.g. 'floor'). This function is known to take a single of type matching |
| 110 | /// 'Op' and returns one value with the same type. If 'Op' is a long double, |
| 111 | /// 'l' is added as the suffix of name, if 'Op' is a float, we add a 'f' |
| 112 | /// suffix. |
Dan Gohman | 79cb840 | 2009-09-25 23:10:17 +0000 | [diff] [blame] | 113 | Value *EmitUnaryFloatFnCall(Value *Op, const char *Name, IRBuilder<> &B, |
| 114 | const AttrListPtr &Attrs); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 115 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 116 | /// EmitPutChar - Emit a call to the putchar function. This assumes that Char |
| 117 | /// is an integer. |
Chris Lattner | 74965f2 | 2009-11-09 04:57:04 +0000 | [diff] [blame] | 118 | Value *EmitPutChar(Value *Char, IRBuilder<> &B); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 119 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 120 | /// EmitPutS - Emit a call to the puts function. This assumes that Str is |
| 121 | /// some pointer. |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 122 | void EmitPutS(Value *Str, IRBuilder<> &B); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 123 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 124 | /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is |
| 125 | /// an i32, and File is a pointer to FILE. |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 126 | void EmitFPutC(Value *Char, Value *File, IRBuilder<> &B); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 127 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 128 | /// EmitFPutS - Emit a call to the puts function. Str is required to be a |
| 129 | /// pointer and File is a pointer to FILE. |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 130 | void EmitFPutS(Value *Str, Value *File, IRBuilder<> &B); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 131 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 132 | /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is |
| 133 | /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE. |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 134 | void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 135 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 136 | }; |
| 137 | } // End anonymous namespace. |
| 138 | |
| 139 | /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*. |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 140 | Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) { |
Chris Lattner | bf79632 | 2009-12-02 06:05:42 +0000 | [diff] [blame] | 141 | return B.CreateBitCast(V, Type::getInt8PtrTy(*Context), "cstr"); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /// EmitStrLen - Emit a call to the strlen function to the builder, for the |
| 145 | /// specified pointer. This always returns an integer value of size intptr_t. |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 146 | Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 147 | Module *M = Caller->getParent(); |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 148 | AttributeWithIndex AWI[2]; |
| 149 | AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture); |
| 150 | AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly | |
| 151 | Attribute::NoUnwind); |
| 152 | |
| 153 | Constant *StrLen =M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 154 | TD->getIntPtrType(*Context), |
Eric Christopher | 3a8bb73 | 2010-02-02 00:13:06 +0000 | [diff] [blame] | 155 | Type::getInt8PtrTy(*Context), |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 156 | NULL); |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 157 | CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen"); |
| 158 | if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts())) |
| 159 | CI->setCallingConv(F->getCallingConv()); |
| 160 | |
| 161 | return CI; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 164 | /// EmitStrChr - Emit a call to the strchr function to the builder, for the |
| 165 | /// specified pointer and character. Ptr is required to be some pointer type, |
| 166 | /// and the return value has 'i8*' type. |
| 167 | Value *LibCallOptimization::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B) { |
| 168 | Module *M = Caller->getParent(); |
| 169 | AttributeWithIndex AWI = |
| 170 | AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind); |
Mikhail Glushenkov | ed5cb59 | 2010-01-04 07:55:25 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 172 | const Type *I8Ptr = Type::getInt8PtrTy(*Context); |
| 173 | const Type *I32Ty = Type::getInt32Ty(*Context); |
| 174 | Constant *StrChr = M->getOrInsertFunction("strchr", AttrListPtr::get(&AWI, 1), |
| 175 | I8Ptr, I8Ptr, I32Ty, NULL); |
| 176 | CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B), |
| 177 | ConstantInt::get(I32Ty, C), "strchr"); |
| 178 | if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts())) |
| 179 | CI->setCallingConv(F->getCallingConv()); |
| 180 | return CI; |
| 181 | } |
| 182 | |
Eric Christopher | 7672d04 | 2010-01-23 05:29:06 +0000 | [diff] [blame] | 183 | /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the |
| 184 | /// specified pointer arguments. |
| 185 | Value *LibCallOptimization::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B) { |
| 186 | Module *M = Caller->getParent(); |
| 187 | AttributeWithIndex AWI[2]; |
| 188 | AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture); |
| 189 | AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind); |
| 190 | const Type *I8Ptr = Type::getInt8PtrTy(*Context); |
| 191 | Value *StrCpy = M->getOrInsertFunction("strcpy", AttrListPtr::get(AWI, 2), |
| 192 | I8Ptr, I8Ptr, I8Ptr, NULL); |
| 193 | CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B), |
| 194 | "strcpy"); |
| 195 | if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts())) |
| 196 | CI->setCallingConv(F->getCallingConv()); |
| 197 | return CI; |
| 198 | } |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 199 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 200 | /// EmitMemCpy - Emit a call to the memcpy function to the builder. This always |
| 201 | /// expects that the size has type 'intptr_t' and Dst/Src are pointers. |
| 202 | Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len, |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 203 | unsigned Align, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 204 | Module *M = Caller->getParent(); |
Chris Lattner | bf79632 | 2009-12-02 06:05:42 +0000 | [diff] [blame] | 205 | const Type *Ty = Len->getType(); |
| 206 | Value *MemCpy = Intrinsic::getDeclaration(M, Intrinsic::memcpy, &Ty, 1); |
| 207 | Dst = CastToCStr(Dst, B); |
| 208 | Src = CastToCStr(Src, B); |
| 209 | return B.CreateCall4(MemCpy, Dst, Src, Len, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 210 | ConstantInt::get(Type::getInt32Ty(*Context), Align)); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Chris Lattner | bf79632 | 2009-12-02 06:05:42 +0000 | [diff] [blame] | 213 | /// EmitMemMove - Emit a call to the memmove function to the builder. This |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 214 | /// always expects that the size has type 'intptr_t' and Dst/Src are pointers. |
| 215 | Value *LibCallOptimization::EmitMemMove(Value *Dst, Value *Src, Value *Len, |
| 216 | unsigned Align, IRBuilder<> &B) { |
| 217 | Module *M = Caller->getParent(); |
Chris Lattner | bf79632 | 2009-12-02 06:05:42 +0000 | [diff] [blame] | 218 | const Type *Ty = TD->getIntPtrType(*Context); |
| 219 | Value *MemMove = Intrinsic::getDeclaration(M, Intrinsic::memmove, &Ty, 1); |
| 220 | Dst = CastToCStr(Dst, B); |
| 221 | Src = CastToCStr(Src, B); |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 222 | Value *A = ConstantInt::get(Type::getInt32Ty(*Context), Align); |
Chris Lattner | bf79632 | 2009-12-02 06:05:42 +0000 | [diff] [blame] | 223 | return B.CreateCall4(MemMove, Dst, Src, Len, A); |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +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 *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val, |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 229 | Value *Len, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 230 | Module *M = Caller->getParent(); |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 231 | AttributeWithIndex AWI; |
| 232 | AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind); |
| 233 | |
| 234 | Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1), |
Eric Christopher | eae6ed1 | 2010-02-02 00:06:55 +0000 | [diff] [blame] | 235 | Type::getInt8PtrTy(*Context), |
| 236 | Type::getInt8PtrTy(*Context), |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 237 | Type::getInt32Ty(*Context), |
Eric Christopher | eae6ed1 | 2010-02-02 00:06:55 +0000 | [diff] [blame] | 238 | TD->getIntPtrType(*Context), |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 239 | NULL); |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 240 | CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr"); |
| 241 | |
| 242 | if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts())) |
| 243 | CI->setCallingConv(F->getCallingConv()); |
| 244 | |
| 245 | return CI; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Nick Lewycky | 13a09e2 | 2008-12-21 00:19:21 +0000 | [diff] [blame] | 248 | /// EmitMemCmp - Emit a call to the memcmp function. |
| 249 | Value *LibCallOptimization::EmitMemCmp(Value *Ptr1, Value *Ptr2, |
| 250 | Value *Len, IRBuilder<> &B) { |
| 251 | Module *M = Caller->getParent(); |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 252 | AttributeWithIndex AWI[3]; |
| 253 | AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture); |
| 254 | AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture); |
| 255 | AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly | |
| 256 | Attribute::NoUnwind); |
| 257 | |
| 258 | Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 259 | Type::getInt32Ty(*Context), |
Mikhail Glushenkov | 0ecbdeb | 2010-01-06 09:20:39 +0000 | [diff] [blame] | 260 | Type::getInt8PtrTy(*Context), |
| 261 | Type::getInt8PtrTy(*Context), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 262 | TD->getIntPtrType(*Context), NULL); |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 263 | CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), |
| 264 | Len, "memcmp"); |
| 265 | |
| 266 | if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts())) |
| 267 | CI->setCallingConv(F->getCallingConv()); |
| 268 | |
| 269 | return CI; |
Nick Lewycky | 13a09e2 | 2008-12-21 00:19:21 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 272 | /// EmitMemSet - Emit a call to the memset function |
| 273 | Value *LibCallOptimization::EmitMemSet(Value *Dst, Value *Val, |
| 274 | Value *Len, IRBuilder<> &B) { |
| 275 | Module *M = Caller->getParent(); |
| 276 | Intrinsic::ID IID = Intrinsic::memset; |
| 277 | const Type *Tys[1]; |
| 278 | Tys[0] = Len->getType(); |
| 279 | Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 280 | Value *Align = ConstantInt::get(Type::getInt32Ty(*Context), 1); |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 281 | return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align); |
| 282 | } |
| 283 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 284 | /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g. |
| 285 | /// 'floor'). This function is known to take a single of type matching 'Op' and |
| 286 | /// returns one value with the same type. If 'Op' is a long double, 'l' is |
| 287 | /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix. |
| 288 | Value *LibCallOptimization::EmitUnaryFloatFnCall(Value *Op, const char *Name, |
Dan Gohman | 79cb840 | 2009-09-25 23:10:17 +0000 | [diff] [blame] | 289 | IRBuilder<> &B, |
| 290 | const AttrListPtr &Attrs) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 291 | char NameBuffer[20]; |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 292 | if (!Op->getType()->isDoubleTy()) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 293 | // If we need to add a suffix, copy into NameBuffer. |
| 294 | unsigned NameLen = strlen(Name); |
| 295 | assert(NameLen < sizeof(NameBuffer)-2); |
| 296 | memcpy(NameBuffer, Name, NameLen); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 297 | if (Op->getType()->isFloatTy()) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 298 | NameBuffer[NameLen] = 'f'; // floorf |
| 299 | else |
| 300 | NameBuffer[NameLen] = 'l'; // floorl |
| 301 | NameBuffer[NameLen+1] = 0; |
| 302 | Name = NameBuffer; |
| 303 | } |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 304 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 305 | Module *M = Caller->getParent(); |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 306 | Value *Callee = M->getOrInsertFunction(Name, Op->getType(), |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 307 | Op->getType(), NULL); |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 308 | CallInst *CI = B.CreateCall(Callee, Op, Name); |
Dan Gohman | 79cb840 | 2009-09-25 23:10:17 +0000 | [diff] [blame] | 309 | CI->setAttributes(Attrs); |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 310 | if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) |
| 311 | CI->setCallingConv(F->getCallingConv()); |
| 312 | |
| 313 | return CI; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /// EmitPutChar - Emit a call to the putchar function. This assumes that Char |
| 317 | /// is an integer. |
Chris Lattner | 74965f2 | 2009-11-09 04:57:04 +0000 | [diff] [blame] | 318 | Value *LibCallOptimization::EmitPutChar(Value *Char, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 319 | Module *M = Caller->getParent(); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 320 | Value *PutChar = M->getOrInsertFunction("putchar", Type::getInt32Ty(*Context), |
| 321 | Type::getInt32Ty(*Context), NULL); |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 322 | CallInst *CI = B.CreateCall(PutChar, |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 323 | B.CreateIntCast(Char, |
Eric Christopher | 3a8bb73 | 2010-02-02 00:13:06 +0000 | [diff] [blame] | 324 | Type::getInt32Ty(*Context), |
| 325 | /*isSigned*/true, |
| 326 | "chari"), |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 327 | "putchar"); |
| 328 | |
| 329 | if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts())) |
| 330 | CI->setCallingConv(F->getCallingConv()); |
Chris Lattner | 74965f2 | 2009-11-09 04:57:04 +0000 | [diff] [blame] | 331 | return CI; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /// EmitPutS - Emit a call to the puts function. This assumes that Str is |
| 335 | /// some pointer. |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 336 | void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 337 | Module *M = Caller->getParent(); |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 338 | AttributeWithIndex AWI[2]; |
| 339 | AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture); |
| 340 | AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind); |
| 341 | |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 342 | Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 343 | Type::getInt32Ty(*Context), |
Eric Christopher | 3a8bb73 | 2010-02-02 00:13:06 +0000 | [diff] [blame] | 344 | Type::getInt8PtrTy(*Context), |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 345 | NULL); |
| 346 | CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts"); |
| 347 | if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts())) |
| 348 | CI->setCallingConv(F->getCallingConv()); |
| 349 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is |
| 353 | /// an integer and File is a pointer to FILE. |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 354 | void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 355 | Module *M = Caller->getParent(); |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 356 | AttributeWithIndex AWI[2]; |
| 357 | AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture); |
| 358 | AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind); |
| 359 | Constant *F; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 360 | if (File->getType()->isPointerTy()) |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 361 | F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2), |
Eric Christopher | 3a8bb73 | 2010-02-02 00:13:06 +0000 | [diff] [blame] | 362 | Type::getInt32Ty(*Context), |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 363 | Type::getInt32Ty(*Context), File->getType(), |
Eric Christopher | 3a8bb73 | 2010-02-02 00:13:06 +0000 | [diff] [blame] | 364 | NULL); |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 365 | else |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 366 | F = M->getOrInsertFunction("fputc", |
Eric Christopher | 3a8bb73 | 2010-02-02 00:13:06 +0000 | [diff] [blame] | 367 | Type::getInt32Ty(*Context), |
| 368 | Type::getInt32Ty(*Context), |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 369 | File->getType(), NULL); |
Duncan Sands | f63c410 | 2009-11-16 12:32:28 +0000 | [diff] [blame] | 370 | Char = B.CreateIntCast(Char, Type::getInt32Ty(*Context), /*isSigned*/true, |
| 371 | "chari"); |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 372 | CallInst *CI = B.CreateCall2(F, Char, File, "fputc"); |
| 373 | |
| 374 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 375 | CI->setCallingConv(Fn->getCallingConv()); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /// EmitFPutS - Emit a call to the puts function. Str is required to be a |
| 379 | /// pointer and File is a pointer to FILE. |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 380 | void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 381 | Module *M = Caller->getParent(); |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 382 | AttributeWithIndex AWI[3]; |
| 383 | AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture); |
| 384 | AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture); |
| 385 | AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind); |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 386 | Constant *F; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 387 | if (File->getType()->isPointerTy()) |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 388 | F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3), |
Eric Christopher | 3a8bb73 | 2010-02-02 00:13:06 +0000 | [diff] [blame] | 389 | Type::getInt32Ty(*Context), |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 390 | Type::getInt8PtrTy(*Context), |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 391 | File->getType(), NULL); |
| 392 | else |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 393 | F = M->getOrInsertFunction("fputs", Type::getInt32Ty(*Context), |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 394 | Type::getInt8PtrTy(*Context), |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 395 | File->getType(), NULL); |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 396 | CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs"); |
| 397 | |
| 398 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 399 | CI->setCallingConv(Fn->getCallingConv()); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is |
| 403 | /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE. |
| 404 | void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File, |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 405 | IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 406 | Module *M = Caller->getParent(); |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 407 | AttributeWithIndex AWI[3]; |
| 408 | AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture); |
| 409 | AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture); |
| 410 | AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind); |
| 411 | Constant *F; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 412 | if (File->getType()->isPointerTy()) |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 413 | F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 414 | TD->getIntPtrType(*Context), |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 415 | Type::getInt8PtrTy(*Context), |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 416 | TD->getIntPtrType(*Context), |
Eric Christopher | 3a8bb73 | 2010-02-02 00:13:06 +0000 | [diff] [blame] | 417 | TD->getIntPtrType(*Context), |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 418 | File->getType(), NULL); |
| 419 | else |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 420 | F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(*Context), |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 421 | Type::getInt8PtrTy(*Context), |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 422 | TD->getIntPtrType(*Context), |
Eric Christopher | 3a8bb73 | 2010-02-02 00:13:06 +0000 | [diff] [blame] | 423 | TD->getIntPtrType(*Context), |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 424 | File->getType(), NULL); |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 425 | CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 426 | ConstantInt::get(TD->getIntPtrType(*Context), 1), File); |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 427 | |
| 428 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 429 | CI->setCallingConv(Fn->getCallingConv()); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | //===----------------------------------------------------------------------===// |
| 433 | // Helper Functions |
| 434 | //===----------------------------------------------------------------------===// |
| 435 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 436 | /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 437 | /// value is equal or not-equal to zero. |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 438 | static bool IsOnlyUsedInZeroEqualityComparison(Value *V) { |
| 439 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); |
| 440 | UI != E; ++UI) { |
| 441 | if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI)) |
| 442 | if (IC->isEquality()) |
| 443 | if (Constant *C = dyn_cast<Constant>(IC->getOperand(1))) |
| 444 | if (C->isNullValue()) |
| 445 | continue; |
| 446 | // Unknown instruction. |
| 447 | return false; |
| 448 | } |
| 449 | return true; |
| 450 | } |
| 451 | |
| 452 | //===----------------------------------------------------------------------===// |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 453 | // String and Memory LibCall Optimizations |
| 454 | //===----------------------------------------------------------------------===// |
| 455 | |
| 456 | //===---------------------------------------===// |
| 457 | // 'strcat' Optimizations |
Chris Lattner | e9f9a7e | 2009-09-03 05:19:59 +0000 | [diff] [blame] | 458 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 459 | struct StrCatOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 460 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 461 | // Verify the "strcat" function prototype. |
| 462 | const FunctionType *FT = Callee->getFunctionType(); |
| 463 | if (FT->getNumParams() != 2 || |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 464 | FT->getReturnType() != Type::getInt8PtrTy(*Context) || |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 465 | FT->getParamType(0) != FT->getReturnType() || |
| 466 | FT->getParamType(1) != FT->getReturnType()) |
| 467 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 468 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 469 | // Extract some information from the instruction |
| 470 | Value *Dst = CI->getOperand(1); |
| 471 | Value *Src = CI->getOperand(2); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 472 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 473 | // See if we can get the length of the input string. |
| 474 | uint64_t Len = GetStringLength(Src); |
Chris Lattner | 56b4f2b | 2008-05-01 06:39:12 +0000 | [diff] [blame] | 475 | if (Len == 0) return 0; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 476 | --Len; // Unbias length. |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 477 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 478 | // Handle the simple, do-nothing case: strcat(x, "") -> x |
| 479 | if (Len == 0) |
| 480 | return Dst; |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 481 | |
| 482 | // These optimizations require TargetData. |
| 483 | if (!TD) return 0; |
| 484 | |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 485 | EmitStrLenMemCpy(Src, Dst, Len, B); |
| 486 | return Dst; |
| 487 | } |
| 488 | |
| 489 | void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 490 | // We need to find the end of the destination string. That's where the |
| 491 | // memory is to be moved to. We just generate a call to strlen. |
| 492 | Value *DstLen = EmitStrLen(Dst, B); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 493 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 494 | // Now that we have the destination's length, we must index into the |
| 495 | // destination's pointer to get the actual memcpy destination (end of |
| 496 | // the string .. we're concatenating). |
Ed Schouten | b5e0a96 | 2009-04-06 13:06:48 +0000 | [diff] [blame] | 497 | Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr"); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 498 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 499 | // We have enough information to now generate the memcpy call to do the |
| 500 | // concatenation for us. Make a memcpy to copy the nul byte with align = 1. |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 501 | EmitMemCpy(CpyDst, Src, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 502 | ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B); |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 503 | } |
| 504 | }; |
| 505 | |
| 506 | //===---------------------------------------===// |
| 507 | // 'strncat' Optimizations |
| 508 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 509 | struct StrNCatOpt : public StrCatOpt { |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 510 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
| 511 | // Verify the "strncat" function prototype. |
| 512 | const FunctionType *FT = Callee->getFunctionType(); |
| 513 | if (FT->getNumParams() != 3 || |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 514 | FT->getReturnType() != Type::getInt8PtrTy(*Context) || |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 515 | FT->getParamType(0) != FT->getReturnType() || |
| 516 | FT->getParamType(1) != FT->getReturnType() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 517 | !FT->getParamType(2)->isIntegerTy()) |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 518 | return 0; |
| 519 | |
| 520 | // Extract some information from the instruction |
| 521 | Value *Dst = CI->getOperand(1); |
| 522 | Value *Src = CI->getOperand(2); |
| 523 | uint64_t Len; |
| 524 | |
| 525 | // We don't do anything if length is not constant |
| 526 | if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3))) |
| 527 | Len = LengthArg->getZExtValue(); |
| 528 | else |
| 529 | return 0; |
| 530 | |
| 531 | // See if we can get the length of the input string. |
| 532 | uint64_t SrcLen = GetStringLength(Src); |
| 533 | if (SrcLen == 0) return 0; |
| 534 | --SrcLen; // Unbias length. |
| 535 | |
| 536 | // Handle the simple, do-nothing cases: |
| 537 | // strncat(x, "", c) -> x |
| 538 | // strncat(x, c, 0) -> x |
| 539 | if (SrcLen == 0 || Len == 0) return Dst; |
| 540 | |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 541 | // These optimizations require TargetData. |
| 542 | if (!TD) return 0; |
| 543 | |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 544 | // We don't optimize this case |
| 545 | if (Len < SrcLen) return 0; |
| 546 | |
| 547 | // strncat(x, s, c) -> strcat(x, s) |
| 548 | // s is constant so the strcat can be optimized further |
Chris Lattner | 5db4cdf | 2009-04-12 18:22:33 +0000 | [diff] [blame] | 549 | EmitStrLenMemCpy(Src, Dst, SrcLen, B); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 550 | return Dst; |
| 551 | } |
| 552 | }; |
| 553 | |
| 554 | //===---------------------------------------===// |
| 555 | // 'strchr' Optimizations |
| 556 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 557 | struct StrChrOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 558 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 559 | // Verify the "strchr" function prototype. |
| 560 | const FunctionType *FT = Callee->getFunctionType(); |
| 561 | if (FT->getNumParams() != 2 || |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 562 | FT->getReturnType() != Type::getInt8PtrTy(*Context) || |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 563 | FT->getParamType(0) != FT->getReturnType()) |
| 564 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 565 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 566 | Value *SrcStr = CI->getOperand(1); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 567 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 568 | // If the second operand is non-constant, see if we can compute the length |
| 569 | // of the input string and turn this into memchr. |
| 570 | ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2)); |
| 571 | if (CharC == 0) { |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 572 | // These optimizations require TargetData. |
| 573 | if (!TD) return 0; |
| 574 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 575 | uint64_t Len = GetStringLength(SrcStr); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 576 | if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32. |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 577 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 578 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 579 | return EmitMemChr(SrcStr, CI->getOperand(2), // include nul. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 580 | ConstantInt::get(TD->getIntPtrType(*Context), Len), B); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | // Otherwise, the character is a constant, see if the first argument is |
| 584 | // a string literal. If so, we can constant fold. |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 585 | std::string Str; |
| 586 | if (!GetConstantStringInfo(SrcStr, Str)) |
Chris Lattner | 56b4f2b | 2008-05-01 06:39:12 +0000 | [diff] [blame] | 587 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 588 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 589 | // strchr can find the nul character. |
| 590 | Str += '\0'; |
| 591 | char CharValue = CharC->getSExtValue(); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 592 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 593 | // Compute the offset. |
| 594 | uint64_t i = 0; |
| 595 | while (1) { |
| 596 | if (i == Str.size()) // Didn't find the char. strchr returns null. |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 597 | return Constant::getNullValue(CI->getType()); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 598 | // Did we find our match? |
| 599 | if (Str[i] == CharValue) |
| 600 | break; |
| 601 | ++i; |
| 602 | } |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 603 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 604 | // strchr(s+n,c) -> gep(s+n+i,c) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 605 | Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 606 | return B.CreateGEP(SrcStr, Idx, "strchr"); |
| 607 | } |
| 608 | }; |
| 609 | |
| 610 | //===---------------------------------------===// |
| 611 | // 'strcmp' Optimizations |
| 612 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 613 | struct StrCmpOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 614 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 615 | // Verify the "strcmp" function prototype. |
| 616 | const FunctionType *FT = Callee->getFunctionType(); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 617 | if (FT->getNumParams() != 2 || |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 618 | !FT->getReturnType()->isIntegerTy(32) || |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 619 | FT->getParamType(0) != FT->getParamType(1) || |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 620 | FT->getParamType(0) != Type::getInt8PtrTy(*Context)) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 621 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 622 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 623 | Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2); |
| 624 | if (Str1P == Str2P) // strcmp(x,x) -> 0 |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 625 | return ConstantInt::get(CI->getType(), 0); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 626 | |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 627 | std::string Str1, Str2; |
| 628 | bool HasStr1 = GetConstantStringInfo(Str1P, Str1); |
| 629 | bool HasStr2 = GetConstantStringInfo(Str2P, Str2); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 630 | |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 631 | if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 632 | return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType()); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 633 | |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 634 | if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 635 | return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType()); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 636 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 637 | // strcmp(x, y) -> cnst (if both x and y are constant strings) |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 638 | if (HasStr1 && HasStr2) |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 639 | return ConstantInt::get(CI->getType(), |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 640 | strcmp(Str1.c_str(),Str2.c_str())); |
Nick Lewycky | 13a09e2 | 2008-12-21 00:19:21 +0000 | [diff] [blame] | 641 | |
| 642 | // strcmp(P, "x") -> memcmp(P, "x", 2) |
| 643 | uint64_t Len1 = GetStringLength(Str1P); |
| 644 | uint64_t Len2 = GetStringLength(Str2P); |
Chris Lattner | 849832c | 2009-06-19 04:17:36 +0000 | [diff] [blame] | 645 | if (Len1 && Len2) { |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 646 | // These optimizations require TargetData. |
| 647 | if (!TD) return 0; |
| 648 | |
Nick Lewycky | 13a09e2 | 2008-12-21 00:19:21 +0000 | [diff] [blame] | 649 | return EmitMemCmp(Str1P, Str2P, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 650 | ConstantInt::get(TD->getIntPtrType(*Context), |
Chris Lattner | 849832c | 2009-06-19 04:17:36 +0000 | [diff] [blame] | 651 | std::min(Len1, Len2)), B); |
Nick Lewycky | 13a09e2 | 2008-12-21 00:19:21 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 654 | return 0; |
| 655 | } |
| 656 | }; |
| 657 | |
| 658 | //===---------------------------------------===// |
| 659 | // 'strncmp' Optimizations |
| 660 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 661 | struct StrNCmpOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 662 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 663 | // Verify the "strncmp" function prototype. |
| 664 | const FunctionType *FT = Callee->getFunctionType(); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 665 | if (FT->getNumParams() != 3 || |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 666 | !FT->getReturnType()->isIntegerTy(32) || |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 667 | FT->getParamType(0) != FT->getParamType(1) || |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 668 | FT->getParamType(0) != Type::getInt8PtrTy(*Context) || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 669 | !FT->getParamType(2)->isIntegerTy()) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 670 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 671 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 672 | Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2); |
| 673 | if (Str1P == Str2P) // strncmp(x,x,n) -> 0 |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 674 | return ConstantInt::get(CI->getType(), 0); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 675 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 676 | // Get the length argument if it is constant. |
| 677 | uint64_t Length; |
| 678 | if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3))) |
| 679 | Length = LengthArg->getZExtValue(); |
| 680 | else |
| 681 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 682 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 683 | if (Length == 0) // strncmp(x,y,0) -> 0 |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 684 | return ConstantInt::get(CI->getType(), 0); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 685 | |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 686 | std::string Str1, Str2; |
| 687 | bool HasStr1 = GetConstantStringInfo(Str1P, Str1); |
| 688 | bool HasStr2 = GetConstantStringInfo(Str2P, Str2); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 689 | |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 690 | if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 691 | return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType()); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 692 | |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 693 | if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 694 | return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType()); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 695 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 696 | // strncmp(x, y) -> cnst (if both x and y are constant strings) |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 697 | if (HasStr1 && HasStr2) |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 698 | return ConstantInt::get(CI->getType(), |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 699 | strncmp(Str1.c_str(), Str2.c_str(), Length)); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 700 | return 0; |
| 701 | } |
| 702 | }; |
| 703 | |
| 704 | |
| 705 | //===---------------------------------------===// |
| 706 | // 'strcpy' Optimizations |
| 707 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 708 | struct StrCpyOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 709 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 710 | // Verify the "strcpy" function prototype. |
| 711 | const FunctionType *FT = Callee->getFunctionType(); |
| 712 | if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) || |
| 713 | FT->getParamType(0) != FT->getParamType(1) || |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 714 | FT->getParamType(0) != Type::getInt8PtrTy(*Context)) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 715 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 716 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 717 | Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2); |
| 718 | if (Dst == Src) // strcpy(x,x) -> x |
| 719 | return Src; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 720 | |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 721 | // These optimizations require TargetData. |
| 722 | if (!TD) return 0; |
| 723 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 724 | // See if we can get the length of the input string. |
| 725 | uint64_t Len = GetStringLength(Src); |
Chris Lattner | 56b4f2b | 2008-05-01 06:39:12 +0000 | [diff] [blame] | 726 | if (Len == 0) return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 727 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 728 | // We have enough information to now generate the memcpy call to do the |
| 729 | // concatenation for us. Make a memcpy to copy the nul byte with align = 1. |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 730 | EmitMemCpy(Dst, Src, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 731 | ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 732 | return Dst; |
| 733 | } |
| 734 | }; |
| 735 | |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 736 | //===---------------------------------------===// |
| 737 | // 'strncpy' Optimizations |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 738 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 739 | struct StrNCpyOpt : public LibCallOptimization { |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 740 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
| 741 | const FunctionType *FT = Callee->getFunctionType(); |
| 742 | if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || |
| 743 | FT->getParamType(0) != FT->getParamType(1) || |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 744 | FT->getParamType(0) != Type::getInt8PtrTy(*Context) || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 745 | !FT->getParamType(2)->isIntegerTy()) |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 746 | return 0; |
| 747 | |
| 748 | Value *Dst = CI->getOperand(1); |
| 749 | Value *Src = CI->getOperand(2); |
| 750 | Value *LenOp = CI->getOperand(3); |
| 751 | |
| 752 | // See if we can get the length of the input string. |
| 753 | uint64_t SrcLen = GetStringLength(Src); |
| 754 | if (SrcLen == 0) return 0; |
| 755 | --SrcLen; |
| 756 | |
| 757 | if (SrcLen == 0) { |
| 758 | // strncpy(x, "", y) -> memset(x, '\0', y, 1) |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 759 | EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp, |
| 760 | B); |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 761 | return Dst; |
| 762 | } |
| 763 | |
| 764 | uint64_t Len; |
| 765 | if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp)) |
| 766 | Len = LengthArg->getZExtValue(); |
| 767 | else |
| 768 | return 0; |
| 769 | |
| 770 | if (Len == 0) return Dst; // strncpy(x, y, 0) -> x |
| 771 | |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 772 | // These optimizations require TargetData. |
| 773 | if (!TD) return 0; |
| 774 | |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 775 | // Let strncpy handle the zero padding |
| 776 | if (Len > SrcLen+1) return 0; |
| 777 | |
| 778 | // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant] |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 779 | EmitMemCpy(Dst, Src, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 780 | ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B); |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 781 | |
| 782 | return Dst; |
| 783 | } |
| 784 | }; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 785 | |
| 786 | //===---------------------------------------===// |
| 787 | // 'strlen' Optimizations |
| 788 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 789 | struct StrLenOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 790 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 791 | const FunctionType *FT = Callee->getFunctionType(); |
| 792 | if (FT->getNumParams() != 1 || |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 793 | FT->getParamType(0) != Type::getInt8PtrTy(*Context) || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 794 | !FT->getReturnType()->isIntegerTy()) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 795 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 796 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 797 | Value *Src = CI->getOperand(1); |
| 798 | |
| 799 | // Constant folding: strlen("xyz") -> 3 |
| 800 | if (uint64_t Len = GetStringLength(Src)) |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 801 | return ConstantInt::get(CI->getType(), Len-1); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 802 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 803 | // strlen(x) != 0 --> *x != 0 |
| 804 | // strlen(x) == 0 --> *x == 0 |
Chris Lattner | 98d67d7 | 2009-12-23 23:24:51 +0000 | [diff] [blame] | 805 | if (IsOnlyUsedInZeroEqualityComparison(CI)) |
| 806 | return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType()); |
| 807 | return 0; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 808 | } |
| 809 | }; |
| 810 | |
| 811 | //===---------------------------------------===// |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 812 | // 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc. |
Nick Lewycky | 4c49841 | 2009-02-13 15:31:46 +0000 | [diff] [blame] | 813 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 814 | struct StrToOpt : public LibCallOptimization { |
Nick Lewycky | 4c49841 | 2009-02-13 15:31:46 +0000 | [diff] [blame] | 815 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
| 816 | const FunctionType *FT = Callee->getFunctionType(); |
| 817 | if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 818 | !FT->getParamType(0)->isPointerTy() || |
| 819 | !FT->getParamType(1)->isPointerTy()) |
Nick Lewycky | 4c49841 | 2009-02-13 15:31:46 +0000 | [diff] [blame] | 820 | return 0; |
| 821 | |
| 822 | Value *EndPtr = CI->getOperand(2); |
Nick Lewycky | 02b6a6a | 2009-02-13 17:08:33 +0000 | [diff] [blame] | 823 | if (isa<ConstantPointerNull>(EndPtr)) { |
| 824 | CI->setOnlyReadsMemory(); |
Nick Lewycky | 4c49841 | 2009-02-13 15:31:46 +0000 | [diff] [blame] | 825 | CI->addAttribute(1, Attribute::NoCapture); |
Nick Lewycky | 02b6a6a | 2009-02-13 17:08:33 +0000 | [diff] [blame] | 826 | } |
Nick Lewycky | 4c49841 | 2009-02-13 15:31:46 +0000 | [diff] [blame] | 827 | |
| 828 | return 0; |
| 829 | } |
| 830 | }; |
| 831 | |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 832 | //===---------------------------------------===// |
| 833 | // 'strstr' Optimizations |
| 834 | |
| 835 | struct StrStrOpt : public LibCallOptimization { |
| 836 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
| 837 | const FunctionType *FT = Callee->getFunctionType(); |
| 838 | if (FT->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 839 | !FT->getParamType(0)->isPointerTy() || |
| 840 | !FT->getParamType(1)->isPointerTy() || |
| 841 | !FT->getReturnType()->isPointerTy()) |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 842 | return 0; |
| 843 | |
| 844 | // fold strstr(x, x) -> x. |
| 845 | if (CI->getOperand(1) == CI->getOperand(2)) |
| 846 | return B.CreateBitCast(CI->getOperand(1), CI->getType()); |
Mikhail Glushenkov | ed5cb59 | 2010-01-04 07:55:25 +0000 | [diff] [blame] | 847 | |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 848 | // See if either input string is a constant string. |
| 849 | std::string SearchStr, ToFindStr; |
| 850 | bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr); |
| 851 | bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr); |
Mikhail Glushenkov | ed5cb59 | 2010-01-04 07:55:25 +0000 | [diff] [blame] | 852 | |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 853 | // fold strstr(x, "") -> x. |
| 854 | if (HasStr2 && ToFindStr.empty()) |
| 855 | return B.CreateBitCast(CI->getOperand(1), CI->getType()); |
Mikhail Glushenkov | ed5cb59 | 2010-01-04 07:55:25 +0000 | [diff] [blame] | 856 | |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 857 | // If both strings are known, constant fold it. |
| 858 | if (HasStr1 && HasStr2) { |
| 859 | std::string::size_type Offset = SearchStr.find(ToFindStr); |
Mikhail Glushenkov | ed5cb59 | 2010-01-04 07:55:25 +0000 | [diff] [blame] | 860 | |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 861 | if (Offset == std::string::npos) // strstr("foo", "bar") -> null |
| 862 | return Constant::getNullValue(CI->getType()); |
| 863 | |
| 864 | // strstr("abcd", "bc") -> gep((char*)"abcd", 1) |
| 865 | Value *Result = CastToCStr(CI->getOperand(1), B); |
| 866 | Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr"); |
| 867 | return B.CreateBitCast(Result, CI->getType()); |
| 868 | } |
Mikhail Glushenkov | ed5cb59 | 2010-01-04 07:55:25 +0000 | [diff] [blame] | 869 | |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 870 | // fold strstr(x, "y") -> strchr(x, 'y'). |
| 871 | if (HasStr2 && ToFindStr.size() == 1) |
| 872 | return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B), |
| 873 | CI->getType()); |
| 874 | return 0; |
| 875 | } |
| 876 | }; |
Mikhail Glushenkov | ed5cb59 | 2010-01-04 07:55:25 +0000 | [diff] [blame] | 877 | |
Nick Lewycky | 4c49841 | 2009-02-13 15:31:46 +0000 | [diff] [blame] | 878 | |
| 879 | //===---------------------------------------===// |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 880 | // 'memcmp' Optimizations |
| 881 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 882 | struct MemCmpOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 883 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 884 | const FunctionType *FT = Callee->getFunctionType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 885 | if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() || |
| 886 | !FT->getParamType(1)->isPointerTy() || |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 887 | !FT->getReturnType()->isIntegerTy(32)) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 888 | return 0; |
Duncan Sands | ec00fcb | 2008-05-19 09:27:24 +0000 | [diff] [blame] | 889 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 890 | Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2); |
Duncan Sands | ec00fcb | 2008-05-19 09:27:24 +0000 | [diff] [blame] | 891 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 892 | if (LHS == RHS) // memcmp(s,s,x) -> 0 |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 893 | return Constant::getNullValue(CI->getType()); |
Duncan Sands | ec00fcb | 2008-05-19 09:27:24 +0000 | [diff] [blame] | 894 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 895 | // Make sure we have a constant length. |
| 896 | ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3)); |
Chris Lattner | 56b4f2b | 2008-05-01 06:39:12 +0000 | [diff] [blame] | 897 | if (!LenC) return 0; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 898 | uint64_t Len = LenC->getZExtValue(); |
Duncan Sands | ec00fcb | 2008-05-19 09:27:24 +0000 | [diff] [blame] | 899 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 900 | if (Len == 0) // memcmp(s1,s2,0) -> 0 |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 901 | return Constant::getNullValue(CI->getType()); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 902 | |
| 903 | if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS |
| 904 | Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv"); |
| 905 | Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv"); |
Chris Lattner | 0e98e4d | 2009-05-30 18:43:04 +0000 | [diff] [blame] | 906 | return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType()); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 907 | } |
Duncan Sands | ec00fcb | 2008-05-19 09:27:24 +0000 | [diff] [blame] | 908 | |
Benjamin Kramer | 992a637 | 2009-11-05 17:44:22 +0000 | [diff] [blame] | 909 | // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant) |
| 910 | std::string LHSStr, RHSStr; |
| 911 | if (GetConstantStringInfo(LHS, LHSStr) && |
| 912 | GetConstantStringInfo(RHS, RHSStr)) { |
| 913 | // Make sure we're not reading out-of-bounds memory. |
| 914 | if (Len > LHSStr.length() || Len > RHSStr.length()) |
| 915 | return 0; |
| 916 | uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len); |
| 917 | return ConstantInt::get(CI->getType(), Ret); |
| 918 | } |
| 919 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 920 | return 0; |
| 921 | } |
| 922 | }; |
| 923 | |
| 924 | //===---------------------------------------===// |
| 925 | // 'memcpy' Optimizations |
| 926 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 927 | struct MemCpyOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 928 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 929 | // These optimizations require TargetData. |
| 930 | if (!TD) return 0; |
| 931 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 932 | const FunctionType *FT = Callee->getFunctionType(); |
| 933 | if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 934 | !FT->getParamType(0)->isPointerTy() || |
| 935 | !FT->getParamType(1)->isPointerTy() || |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 936 | FT->getParamType(2) != TD->getIntPtrType(*Context)) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 937 | return 0; |
| 938 | |
| 939 | // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1) |
| 940 | EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B); |
| 941 | return CI->getOperand(1); |
| 942 | } |
| 943 | }; |
| 944 | |
Eli Friedman | d83ae7d | 2008-11-30 08:32:11 +0000 | [diff] [blame] | 945 | //===---------------------------------------===// |
| 946 | // 'memmove' Optimizations |
| 947 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 948 | struct MemMoveOpt : public LibCallOptimization { |
Eli Friedman | d83ae7d | 2008-11-30 08:32:11 +0000 | [diff] [blame] | 949 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 950 | // These optimizations require TargetData. |
| 951 | if (!TD) return 0; |
| 952 | |
Eli Friedman | d83ae7d | 2008-11-30 08:32:11 +0000 | [diff] [blame] | 953 | const FunctionType *FT = Callee->getFunctionType(); |
| 954 | if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 955 | !FT->getParamType(0)->isPointerTy() || |
| 956 | !FT->getParamType(1)->isPointerTy() || |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 957 | FT->getParamType(2) != TD->getIntPtrType(*Context)) |
Eli Friedman | d83ae7d | 2008-11-30 08:32:11 +0000 | [diff] [blame] | 958 | return 0; |
| 959 | |
| 960 | // memmove(x, y, n) -> llvm.memmove(x, y, n, 1) |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 961 | EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B); |
Eli Friedman | d83ae7d | 2008-11-30 08:32:11 +0000 | [diff] [blame] | 962 | return CI->getOperand(1); |
| 963 | } |
| 964 | }; |
| 965 | |
| 966 | //===---------------------------------------===// |
| 967 | // 'memset' Optimizations |
| 968 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 969 | struct MemSetOpt : public LibCallOptimization { |
Eli Friedman | d83ae7d | 2008-11-30 08:32:11 +0000 | [diff] [blame] | 970 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 971 | // These optimizations require TargetData. |
| 972 | if (!TD) return 0; |
| 973 | |
Eli Friedman | d83ae7d | 2008-11-30 08:32:11 +0000 | [diff] [blame] | 974 | const FunctionType *FT = Callee->getFunctionType(); |
| 975 | if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 976 | !FT->getParamType(0)->isPointerTy() || |
| 977 | !FT->getParamType(1)->isIntegerTy() || |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 978 | FT->getParamType(2) != TD->getIntPtrType(*Context)) |
Eli Friedman | d83ae7d | 2008-11-30 08:32:11 +0000 | [diff] [blame] | 979 | return 0; |
| 980 | |
| 981 | // memset(p, v, n) -> llvm.memset(p, v, n, 1) |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 982 | Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context), |
| 983 | false); |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 984 | EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B); |
Eli Friedman | d83ae7d | 2008-11-30 08:32:11 +0000 | [diff] [blame] | 985 | return CI->getOperand(1); |
| 986 | } |
| 987 | }; |
| 988 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 989 | //===----------------------------------------------------------------------===// |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 990 | // Object Size Checking Optimizations |
| 991 | //===----------------------------------------------------------------------===// |
| 992 | |
| 993 | //===---------------------------------------===// |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 994 | // 'memcpy_chk' Optimizations |
| 995 | |
| 996 | struct MemCpyChkOpt : public LibCallOptimization { |
| 997 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
| 998 | // These optimizations require TargetData. |
| 999 | if (!TD) return 0; |
| 1000 | |
| 1001 | const FunctionType *FT = Callee->getFunctionType(); |
| 1002 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1003 | !FT->getParamType(0)->isPointerTy() || |
| 1004 | !FT->getParamType(1)->isPointerTy() || |
| 1005 | !FT->getParamType(3)->isIntegerTy() || |
Eric Christopher | f734be2 | 2009-12-22 01:23:51 +0000 | [diff] [blame] | 1006 | FT->getParamType(2) != TD->getIntPtrType(*Context)) |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1007 | return 0; |
| 1008 | |
Evan Cheng | a79eb38 | 2010-03-05 20:59:47 +0000 | [diff] [blame^] | 1009 | ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(4)); |
| 1010 | if (!ObjSizeCI) |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1011 | return 0; |
Evan Cheng | a79eb38 | 2010-03-05 20:59:47 +0000 | [diff] [blame^] | 1012 | ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3)); |
| 1013 | if (ObjSizeCI->isAllOnesValue() || |
| 1014 | (SizeCI && ObjSizeCI->getValue().uge(SizeCI->getValue()))) { |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1015 | EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B); |
| 1016 | return CI->getOperand(1); |
| 1017 | } |
| 1018 | |
| 1019 | return 0; |
| 1020 | } |
| 1021 | }; |
| 1022 | |
| 1023 | //===---------------------------------------===// |
| 1024 | // 'memset_chk' Optimizations |
| 1025 | |
| 1026 | struct MemSetChkOpt : public LibCallOptimization { |
| 1027 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
| 1028 | // These optimizations require TargetData. |
| 1029 | if (!TD) return 0; |
| 1030 | |
| 1031 | const FunctionType *FT = Callee->getFunctionType(); |
| 1032 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1033 | !FT->getParamType(0)->isPointerTy() || |
| 1034 | !FT->getParamType(1)->isIntegerTy() || |
| 1035 | !FT->getParamType(3)->isIntegerTy() || |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1036 | FT->getParamType(2) != TD->getIntPtrType(*Context)) |
| 1037 | return 0; |
| 1038 | |
Evan Cheng | a79eb38 | 2010-03-05 20:59:47 +0000 | [diff] [blame^] | 1039 | ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(4)); |
| 1040 | if (!ObjSizeCI) |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1041 | return 0; |
Evan Cheng | a79eb38 | 2010-03-05 20:59:47 +0000 | [diff] [blame^] | 1042 | ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3)); |
| 1043 | if (ObjSizeCI->isAllOnesValue() || |
| 1044 | (SizeCI && ObjSizeCI->getValue().uge(SizeCI->getValue()))) { |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1045 | Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context), |
| 1046 | false); |
| 1047 | EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B); |
| 1048 | return CI->getOperand(1); |
| 1049 | } |
| 1050 | |
| 1051 | return 0; |
| 1052 | } |
| 1053 | }; |
| 1054 | |
| 1055 | //===---------------------------------------===// |
| 1056 | // 'memmove_chk' Optimizations |
| 1057 | |
| 1058 | struct MemMoveChkOpt : public LibCallOptimization { |
| 1059 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
| 1060 | // These optimizations require TargetData. |
| 1061 | if (!TD) return 0; |
| 1062 | |
| 1063 | const FunctionType *FT = Callee->getFunctionType(); |
| 1064 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1065 | !FT->getParamType(0)->isPointerTy() || |
| 1066 | !FT->getParamType(1)->isPointerTy() || |
| 1067 | !FT->getParamType(3)->isIntegerTy() || |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1068 | FT->getParamType(2) != TD->getIntPtrType(*Context)) |
| 1069 | return 0; |
| 1070 | |
Evan Cheng | a79eb38 | 2010-03-05 20:59:47 +0000 | [diff] [blame^] | 1071 | ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(4)); |
| 1072 | if (!ObjSizeCI) |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1073 | return 0; |
Evan Cheng | a79eb38 | 2010-03-05 20:59:47 +0000 | [diff] [blame^] | 1074 | ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3)); |
| 1075 | if (ObjSizeCI->isAllOnesValue() || |
| 1076 | (SizeCI && ObjSizeCI->getValue().uge(SizeCI->getValue()))) { |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1077 | EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), |
| 1078 | 1, B); |
| 1079 | return CI->getOperand(1); |
| 1080 | } |
| 1081 | |
| 1082 | return 0; |
| 1083 | } |
| 1084 | }; |
| 1085 | |
Eric Christopher | 7672d04 | 2010-01-23 05:29:06 +0000 | [diff] [blame] | 1086 | struct StrCpyChkOpt : public LibCallOptimization { |
| 1087 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Eric Christopher | 7672d04 | 2010-01-23 05:29:06 +0000 | [diff] [blame] | 1088 | const FunctionType *FT = Callee->getFunctionType(); |
| 1089 | if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1090 | !FT->getParamType(0)->isPointerTy() || |
| 1091 | !FT->getParamType(1)->isPointerTy()) |
Eric Christopher | 7672d04 | 2010-01-23 05:29:06 +0000 | [diff] [blame] | 1092 | return 0; |
| 1093 | |
Evan Cheng | a79eb38 | 2010-03-05 20:59:47 +0000 | [diff] [blame^] | 1094 | ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(3)); |
| 1095 | if (!ObjSizeCI) |
Eric Christopher | 7672d04 | 2010-01-23 05:29:06 +0000 | [diff] [blame] | 1096 | return 0; |
| 1097 | |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 1098 | // If a) we don't have any length information, or b) we know this will |
| 1099 | // fit then just lower to a plain strcpy. Otherwise we'll keep our |
| 1100 | // strcpy_chk call which may fail at runtime if the size is too long. |
| 1101 | // TODO: It might be nice to get a maximum length out of the possible |
| 1102 | // string lengths for varying. |
Evan Cheng | a79eb38 | 2010-03-05 20:59:47 +0000 | [diff] [blame^] | 1103 | if (ObjSizeCI->isAllOnesValue() || |
| 1104 | ObjSizeCI->getZExtValue() >= GetStringLength(CI->getOperand(2))) |
Eric Christopher | 7672d04 | 2010-01-23 05:29:06 +0000 | [diff] [blame] | 1105 | return EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B); |
| 1106 | |
| 1107 | return 0; |
| 1108 | } |
| 1109 | }; |
| 1110 | |
| 1111 | |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1112 | //===----------------------------------------------------------------------===// |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1113 | // Math Library Optimizations |
| 1114 | //===----------------------------------------------------------------------===// |
| 1115 | |
| 1116 | //===---------------------------------------===// |
| 1117 | // 'pow*' Optimizations |
| 1118 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1119 | struct PowOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 1120 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1121 | const FunctionType *FT = Callee->getFunctionType(); |
| 1122 | // Just make sure this has 2 arguments of the same FP type, which match the |
| 1123 | // result type. |
| 1124 | if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) || |
| 1125 | FT->getParamType(0) != FT->getParamType(1) || |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1126 | !FT->getParamType(0)->isFloatingPointTy()) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1127 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1128 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1129 | Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2); |
| 1130 | if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) { |
| 1131 | if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0 |
| 1132 | return Op1C; |
| 1133 | if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x) |
Dan Gohman | 76926b6 | 2009-09-26 18:10:13 +0000 | [diff] [blame] | 1134 | return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes()); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1135 | } |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1136 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1137 | ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2); |
| 1138 | if (Op2C == 0) return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1139 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1140 | if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0 |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1141 | return ConstantFP::get(CI->getType(), 1.0); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1142 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1143 | if (Op2C->isExactlyValue(0.5)) { |
Dan Gohman | 79cb840 | 2009-09-25 23:10:17 +0000 | [diff] [blame] | 1144 | // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))). |
| 1145 | // This is faster than calling pow, and still handles negative zero |
| 1146 | // and negative infinite correctly. |
| 1147 | // TODO: In fast-math mode, this could be just sqrt(x). |
| 1148 | // TODO: In finite-only mode, this could be just fabs(sqrt(x)). |
Dan Gohman | a23643d | 2009-09-25 23:40:21 +0000 | [diff] [blame] | 1149 | Value *Inf = ConstantFP::getInfinity(CI->getType()); |
| 1150 | Value *NegInf = ConstantFP::getInfinity(CI->getType(), true); |
Dan Gohman | 76926b6 | 2009-09-26 18:10:13 +0000 | [diff] [blame] | 1151 | Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B, |
| 1152 | Callee->getAttributes()); |
| 1153 | Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B, |
| 1154 | Callee->getAttributes()); |
Dan Gohman | 79cb840 | 2009-09-25 23:10:17 +0000 | [diff] [blame] | 1155 | Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp"); |
| 1156 | Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp"); |
| 1157 | return Sel; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1158 | } |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1159 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1160 | if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x |
| 1161 | return Op1; |
| 1162 | if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1163 | return B.CreateFMul(Op1, Op1, "pow2"); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1164 | if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1165 | return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1166 | Op1, "powrecip"); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1167 | return 0; |
| 1168 | } |
| 1169 | }; |
| 1170 | |
| 1171 | //===---------------------------------------===// |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1172 | // 'exp2' Optimizations |
| 1173 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1174 | struct Exp2Opt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 1175 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1176 | const FunctionType *FT = Callee->getFunctionType(); |
| 1177 | // Just make sure this has 1 argument of FP type, which matches the |
| 1178 | // result type. |
| 1179 | if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) || |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1180 | !FT->getParamType(0)->isFloatingPointTy()) |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1181 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1183 | Value *Op = CI->getOperand(1); |
| 1184 | // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32 |
| 1185 | // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32 |
| 1186 | Value *LdExpArg = 0; |
| 1187 | if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) { |
| 1188 | if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32) |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1189 | LdExpArg = B.CreateSExt(OpC->getOperand(0), |
| 1190 | Type::getInt32Ty(*Context), "tmp"); |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1191 | } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) { |
| 1192 | if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32) |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1193 | LdExpArg = B.CreateZExt(OpC->getOperand(0), |
| 1194 | Type::getInt32Ty(*Context), "tmp"); |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1195 | } |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 1196 | |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1197 | if (LdExpArg) { |
| 1198 | const char *Name; |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1199 | if (Op->getType()->isFloatTy()) |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1200 | Name = "ldexpf"; |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1201 | else if (Op->getType()->isDoubleTy()) |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1202 | Name = "ldexp"; |
| 1203 | else |
| 1204 | Name = "ldexpl"; |
| 1205 | |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1206 | Constant *One = ConstantFP::get(*Context, APFloat(1.0f)); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1207 | if (!Op->getType()->isFloatTy()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1208 | One = ConstantExpr::getFPExtend(One, Op->getType()); |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1209 | |
| 1210 | Module *M = Caller->getParent(); |
| 1211 | Value *Callee = M->getOrInsertFunction(Name, Op->getType(), |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1212 | Op->getType(), |
Eric Christopher | 3a8bb73 | 2010-02-02 00:13:06 +0000 | [diff] [blame] | 1213 | Type::getInt32Ty(*Context),NULL); |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 1214 | CallInst *CI = B.CreateCall2(Callee, One, LdExpArg); |
| 1215 | if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) |
| 1216 | CI->setCallingConv(F->getCallingConv()); |
| 1217 | |
| 1218 | return CI; |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1219 | } |
| 1220 | return 0; |
| 1221 | } |
| 1222 | }; |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1223 | |
| 1224 | //===---------------------------------------===// |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1225 | // Double -> Float Shrinking Optimizations for Unary Functions like 'floor' |
| 1226 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1227 | struct UnaryDoubleFPOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 1228 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1229 | const FunctionType *FT = Callee->getFunctionType(); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1230 | if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() || |
| 1231 | !FT->getParamType(0)->isDoubleTy()) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1232 | return 0; |
Anton Korobeynikov | 9547cdf | 2009-06-18 20:05:31 +0000 | [diff] [blame] | 1233 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1234 | // If this is something like 'floor((double)floatval)', convert to floorf. |
| 1235 | FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1)); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1236 | if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy()) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1237 | return 0; |
| 1238 | |
| 1239 | // floor((double)floatval) -> (double)floorf(floatval) |
| 1240 | Value *V = Cast->getOperand(0); |
Dan Gohman | 76926b6 | 2009-09-26 18:10:13 +0000 | [diff] [blame] | 1241 | V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B, |
| 1242 | Callee->getAttributes()); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1243 | return B.CreateFPExt(V, Type::getDoubleTy(*Context)); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1244 | } |
| 1245 | }; |
| 1246 | |
| 1247 | //===----------------------------------------------------------------------===// |
| 1248 | // Integer Optimizations |
| 1249 | //===----------------------------------------------------------------------===// |
| 1250 | |
| 1251 | //===---------------------------------------===// |
| 1252 | // 'ffs*' Optimizations |
| 1253 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1254 | struct FFSOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 1255 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1256 | const FunctionType *FT = Callee->getFunctionType(); |
| 1257 | // Just make sure this has 2 arguments of the same FP type, which match the |
| 1258 | // result type. |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1259 | if (FT->getNumParams() != 1 || |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1260 | !FT->getReturnType()->isIntegerTy(32) || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1261 | !FT->getParamType(0)->isIntegerTy()) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1262 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1263 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1264 | Value *Op = CI->getOperand(1); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1265 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1266 | // Constant fold. |
| 1267 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { |
| 1268 | if (CI->getValue() == 0) // ffs(0) -> 0. |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1269 | return Constant::getNullValue(CI->getType()); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1270 | return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1 |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1271 | CI->getValue().countTrailingZeros()+1); |
| 1272 | } |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1273 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1274 | // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0 |
| 1275 | const Type *ArgType = Op->getType(); |
| 1276 | Value *F = Intrinsic::getDeclaration(Callee->getParent(), |
| 1277 | Intrinsic::cttz, &ArgType, 1); |
| 1278 | Value *V = B.CreateCall(F, Op, "cttz"); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1279 | V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp"); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1280 | V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp"); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1281 | |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1282 | Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp"); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1283 | return B.CreateSelect(Cond, V, |
| 1284 | ConstantInt::get(Type::getInt32Ty(*Context), 0)); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1285 | } |
| 1286 | }; |
| 1287 | |
| 1288 | //===---------------------------------------===// |
| 1289 | // 'isdigit' Optimizations |
| 1290 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1291 | struct IsDigitOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 1292 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1293 | const FunctionType *FT = Callee->getFunctionType(); |
| 1294 | // We require integer(i32) |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1295 | if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() || |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1296 | !FT->getParamType(0)->isIntegerTy(32)) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1297 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1298 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1299 | // isdigit(c) -> (c-'0') <u 10 |
| 1300 | Value *Op = CI->getOperand(1); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1301 | Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'), |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1302 | "isdigittmp"); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1303 | Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10), |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1304 | "isdigit"); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1305 | return B.CreateZExt(Op, CI->getType()); |
| 1306 | } |
| 1307 | }; |
| 1308 | |
| 1309 | //===---------------------------------------===// |
| 1310 | // 'isascii' Optimizations |
| 1311 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1312 | struct IsAsciiOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 1313 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1314 | const FunctionType *FT = Callee->getFunctionType(); |
| 1315 | // We require integer(i32) |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1316 | if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() || |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1317 | !FT->getParamType(0)->isIntegerTy(32)) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1318 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1319 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1320 | // isascii(c) -> c <u 128 |
| 1321 | Value *Op = CI->getOperand(1); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1322 | Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128), |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1323 | "isascii"); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1324 | return B.CreateZExt(Op, CI->getType()); |
| 1325 | } |
| 1326 | }; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1327 | |
Chris Lattner | 313f0e6 | 2008-06-09 08:26:51 +0000 | [diff] [blame] | 1328 | //===---------------------------------------===// |
| 1329 | // 'abs', 'labs', 'llabs' Optimizations |
| 1330 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1331 | struct AbsOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 1332 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | 313f0e6 | 2008-06-09 08:26:51 +0000 | [diff] [blame] | 1333 | const FunctionType *FT = Callee->getFunctionType(); |
| 1334 | // We require integer(integer) where the types agree. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1335 | if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() || |
Chris Lattner | 313f0e6 | 2008-06-09 08:26:51 +0000 | [diff] [blame] | 1336 | FT->getParamType(0) != FT->getReturnType()) |
| 1337 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1338 | |
Chris Lattner | 313f0e6 | 2008-06-09 08:26:51 +0000 | [diff] [blame] | 1339 | // abs(x) -> x >s -1 ? x : -x |
| 1340 | Value *Op = CI->getOperand(1); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1341 | Value *Pos = B.CreateICmpSGT(Op, |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1342 | Constant::getAllOnesValue(Op->getType()), |
Chris Lattner | 313f0e6 | 2008-06-09 08:26:51 +0000 | [diff] [blame] | 1343 | "ispos"); |
| 1344 | Value *Neg = B.CreateNeg(Op, "neg"); |
| 1345 | return B.CreateSelect(Pos, Op, Neg); |
| 1346 | } |
| 1347 | }; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1348 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1349 | |
| 1350 | //===---------------------------------------===// |
| 1351 | // 'toascii' Optimizations |
| 1352 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1353 | struct ToAsciiOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 1354 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1355 | const FunctionType *FT = Callee->getFunctionType(); |
| 1356 | // We require i32(i32) |
| 1357 | if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) || |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1358 | !FT->getParamType(0)->isIntegerTy(32)) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1359 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1360 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1361 | // isascii(c) -> c & 0x7f |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1362 | return B.CreateAnd(CI->getOperand(1), |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1363 | ConstantInt::get(CI->getType(),0x7F)); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1364 | } |
| 1365 | }; |
| 1366 | |
| 1367 | //===----------------------------------------------------------------------===// |
| 1368 | // Formatting and IO Optimizations |
| 1369 | //===----------------------------------------------------------------------===// |
| 1370 | |
| 1371 | //===---------------------------------------===// |
| 1372 | // 'printf' Optimizations |
| 1373 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1374 | struct PrintFOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 1375 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1376 | // Require one fixed pointer argument and an integer/void result. |
| 1377 | const FunctionType *FT = Callee->getFunctionType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1378 | if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() || |
| 1379 | !(FT->getReturnType()->isIntegerTy() || |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1380 | FT->getReturnType()->isVoidTy())) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1381 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1382 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1383 | // Check for a fixed format string. |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1384 | std::string FormatStr; |
| 1385 | if (!GetConstantStringInfo(CI->getOperand(1), FormatStr)) |
| 1386 | return 0; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Empty format string -> noop. |
| 1389 | if (FormatStr.empty()) // Tolerate printf's declared void. |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1390 | return CI->use_empty() ? (Value*)CI : |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1391 | ConstantInt::get(CI->getType(), 0); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1392 | |
Chris Lattner | 74965f2 | 2009-11-09 04:57:04 +0000 | [diff] [blame] | 1393 | // printf("x") -> putchar('x'), even for '%'. Return the result of putchar |
| 1394 | // in case there is an error writing to stdout. |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1395 | if (FormatStr.size() == 1) { |
Chris Lattner | 74965f2 | 2009-11-09 04:57:04 +0000 | [diff] [blame] | 1396 | Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context), |
| 1397 | FormatStr[0]), B); |
| 1398 | if (CI->use_empty()) return CI; |
| 1399 | return B.CreateIntCast(Res, CI->getType(), true); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1400 | } |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1401 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1402 | // printf("foo\n") --> puts("foo") |
| 1403 | if (FormatStr[FormatStr.size()-1] == '\n' && |
| 1404 | FormatStr.find('%') == std::string::npos) { // no format characters. |
| 1405 | // Create a string literal with no \n on it. We expect the constant merge |
| 1406 | // pass to be run after this pass, to merge duplicate strings. |
| 1407 | FormatStr.erase(FormatStr.end()-1); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1408 | Constant *C = ConstantArray::get(*Context, FormatStr, true); |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 1409 | C = new GlobalVariable(*Callee->getParent(), C->getType(), true, |
| 1410 | GlobalVariable::InternalLinkage, C, "str"); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1411 | EmitPutS(C, B); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1412 | return CI->use_empty() ? (Value*)CI : |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1413 | ConstantInt::get(CI->getType(), FormatStr.size()+1); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1414 | } |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1415 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1416 | // Optimize specific format strings. |
| 1417 | // printf("%c", chr) --> putchar(*(i8*)dst) |
| 1418 | if (FormatStr == "%c" && CI->getNumOperands() > 2 && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1419 | CI->getOperand(2)->getType()->isIntegerTy()) { |
Chris Lattner | 74965f2 | 2009-11-09 04:57:04 +0000 | [diff] [blame] | 1420 | Value *Res = EmitPutChar(CI->getOperand(2), B); |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1421 | |
Chris Lattner | 74965f2 | 2009-11-09 04:57:04 +0000 | [diff] [blame] | 1422 | if (CI->use_empty()) return CI; |
| 1423 | return B.CreateIntCast(Res, CI->getType(), true); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1424 | } |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1425 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1426 | // printf("%s\n", str) --> puts(str) |
| 1427 | if (FormatStr == "%s\n" && CI->getNumOperands() > 2 && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1428 | CI->getOperand(2)->getType()->isPointerTy() && |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1429 | CI->use_empty()) { |
| 1430 | EmitPutS(CI->getOperand(2), B); |
| 1431 | return CI; |
| 1432 | } |
| 1433 | return 0; |
| 1434 | } |
| 1435 | }; |
| 1436 | |
| 1437 | //===---------------------------------------===// |
| 1438 | // 'sprintf' Optimizations |
| 1439 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1440 | struct SPrintFOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 1441 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1442 | // Require two fixed pointer arguments and an integer result. |
| 1443 | const FunctionType *FT = Callee->getFunctionType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1444 | if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() || |
| 1445 | !FT->getParamType(1)->isPointerTy() || |
| 1446 | !FT->getReturnType()->isIntegerTy()) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1447 | return 0; |
| 1448 | |
| 1449 | // Check for a fixed format string. |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1450 | std::string FormatStr; |
| 1451 | if (!GetConstantStringInfo(CI->getOperand(2), FormatStr)) |
| 1452 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1453 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1454 | // If we just have a format string (nothing else crazy) transform it. |
| 1455 | if (CI->getNumOperands() == 3) { |
| 1456 | // Make sure there's no % in the constant array. We could try to handle |
| 1457 | // %% -> % in the future if we cared. |
| 1458 | for (unsigned i = 0, e = FormatStr.size(); i != e; ++i) |
| 1459 | if (FormatStr[i] == '%') |
| 1460 | return 0; // we found a format specifier, bail out. |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 1461 | |
| 1462 | // These optimizations require TargetData. |
| 1463 | if (!TD) return 0; |
| 1464 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1465 | // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1) |
| 1466 | EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte. |
Mikhail Glushenkov | ed5cb59 | 2010-01-04 07:55:25 +0000 | [diff] [blame] | 1467 | ConstantInt::get |
| 1468 | (TD->getIntPtrType(*Context), FormatStr.size()+1),1,B); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1469 | return ConstantInt::get(CI->getType(), FormatStr.size()); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1470 | } |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1471 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1472 | // The remaining optimizations require the format string to be "%s" or "%c" |
| 1473 | // and have an extra operand. |
| 1474 | if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4) |
| 1475 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1476 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1477 | // Decode the second character of the format string. |
| 1478 | if (FormatStr[1] == 'c') { |
Chris Lattner | 56b4f2b | 2008-05-01 06:39:12 +0000 | [diff] [blame] | 1479 | // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0 |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1480 | if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1481 | Value *V = B.CreateTrunc(CI->getOperand(3), |
| 1482 | Type::getInt8Ty(*Context), "char"); |
Chris Lattner | 56b4f2b | 2008-05-01 06:39:12 +0000 | [diff] [blame] | 1483 | Value *Ptr = CastToCStr(CI->getOperand(1), B); |
| 1484 | B.CreateStore(V, Ptr); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1485 | Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1), |
| 1486 | "nul"); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1487 | B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1488 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1489 | return ConstantInt::get(CI->getType(), 1); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1490 | } |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1491 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1492 | if (FormatStr[1] == 's') { |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 1493 | // These optimizations require TargetData. |
| 1494 | if (!TD) return 0; |
| 1495 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1496 | // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1) |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1497 | if (!CI->getOperand(3)->getType()->isPointerTy()) return 0; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1498 | |
| 1499 | Value *Len = EmitStrLen(CI->getOperand(3), B); |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1500 | Value *IncLen = B.CreateAdd(Len, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1501 | ConstantInt::get(Len->getType(), 1), |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1502 | "leninc"); |
| 1503 | EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1504 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1505 | // The sprintf result is the unincremented number of bytes in the string. |
| 1506 | return B.CreateIntCast(Len, CI->getType(), false); |
| 1507 | } |
| 1508 | return 0; |
| 1509 | } |
| 1510 | }; |
| 1511 | |
| 1512 | //===---------------------------------------===// |
| 1513 | // 'fwrite' Optimizations |
| 1514 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1515 | struct FWriteOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 1516 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1517 | // Require a pointer, an integer, an integer, a pointer, returning integer. |
| 1518 | const FunctionType *FT = Callee->getFunctionType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1519 | if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() || |
| 1520 | !FT->getParamType(1)->isIntegerTy() || |
| 1521 | !FT->getParamType(2)->isIntegerTy() || |
| 1522 | !FT->getParamType(3)->isPointerTy() || |
| 1523 | !FT->getReturnType()->isIntegerTy()) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1524 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1525 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1526 | // Get the element size and count. |
| 1527 | ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2)); |
| 1528 | ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3)); |
| 1529 | if (!SizeC || !CountC) return 0; |
| 1530 | uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue(); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1531 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1532 | // If this is writing zero records, remove the call (it's a noop). |
| 1533 | if (Bytes == 0) |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1534 | return ConstantInt::get(CI->getType(), 0); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1535 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1536 | // If this is writing one byte, turn it into fputc. |
| 1537 | if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F) |
| 1538 | Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char"); |
| 1539 | EmitFPutC(Char, CI->getOperand(4), B); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1540 | return ConstantInt::get(CI->getType(), 1); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | return 0; |
| 1544 | } |
| 1545 | }; |
| 1546 | |
| 1547 | //===---------------------------------------===// |
| 1548 | // 'fputs' Optimizations |
| 1549 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1550 | struct FPutsOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 1551 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 1552 | // These optimizations require TargetData. |
| 1553 | if (!TD) return 0; |
| 1554 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1555 | // Require two pointers. Also, we can't optimize if return value is used. |
| 1556 | const FunctionType *FT = Callee->getFunctionType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1557 | if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() || |
| 1558 | !FT->getParamType(1)->isPointerTy() || |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1559 | !CI->use_empty()) |
| 1560 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1561 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1562 | // fputs(s,F) --> fwrite(s,1,strlen(s),F) |
| 1563 | uint64_t Len = GetStringLength(CI->getOperand(1)); |
Chris Lattner | 56b4f2b | 2008-05-01 06:39:12 +0000 | [diff] [blame] | 1564 | if (!Len) return 0; |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1565 | EmitFWrite(CI->getOperand(1), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1566 | ConstantInt::get(TD->getIntPtrType(*Context), Len-1), |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1567 | CI->getOperand(2), B); |
| 1568 | return CI; // Known to have no uses (see above). |
| 1569 | } |
| 1570 | }; |
| 1571 | |
| 1572 | //===---------------------------------------===// |
| 1573 | // 'fprintf' Optimizations |
| 1574 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1575 | struct FPrintFOpt : public LibCallOptimization { |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 1576 | virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1577 | // Require two fixed paramters as pointers and integer result. |
| 1578 | const FunctionType *FT = Callee->getFunctionType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1579 | if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() || |
| 1580 | !FT->getParamType(1)->isPointerTy() || |
| 1581 | !FT->getReturnType()->isIntegerTy()) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1582 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1583 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1584 | // All the optimizations depend on the format string. |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1585 | std::string FormatStr; |
| 1586 | if (!GetConstantStringInfo(CI->getOperand(2), FormatStr)) |
| 1587 | return 0; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1588 | |
| 1589 | // fprintf(F, "foo") --> fwrite("foo", 3, 1, F) |
| 1590 | if (CI->getNumOperands() == 3) { |
| 1591 | for (unsigned i = 0, e = FormatStr.size(); i != e; ++i) |
| 1592 | if (FormatStr[i] == '%') // Could handle %% -> % if we cared. |
Chris Lattner | 56b4f2b | 2008-05-01 06:39:12 +0000 | [diff] [blame] | 1593 | return 0; // We found a format specifier. |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 1594 | |
| 1595 | // These optimizations require TargetData. |
| 1596 | if (!TD) return 0; |
| 1597 | |
Mikhail Glushenkov | ed5cb59 | 2010-01-04 07:55:25 +0000 | [diff] [blame] | 1598 | EmitFWrite(CI->getOperand(2), |
| 1599 | ConstantInt::get(TD->getIntPtrType(*Context), |
| 1600 | FormatStr.size()), |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1601 | CI->getOperand(1), B); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1602 | return ConstantInt::get(CI->getType(), FormatStr.size()); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1603 | } |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1604 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1605 | // The remaining optimizations require the format string to be "%s" or "%c" |
| 1606 | // and have an extra operand. |
| 1607 | if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4) |
| 1608 | return 0; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1609 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1610 | // Decode the second character of the format string. |
| 1611 | if (FormatStr[1] == 'c') { |
| 1612 | // fprintf(F, "%c", chr) --> *(i8*)dst = chr |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1613 | if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1614 | EmitFPutC(CI->getOperand(3), CI->getOperand(1), B); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1615 | return ConstantInt::get(CI->getType(), 1); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1616 | } |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1617 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1618 | if (FormatStr[1] == 's') { |
| 1619 | // fprintf(F, "%s", str) -> fputs(str, F) |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1620 | if (!CI->getOperand(3)->getType()->isPointerTy() || !CI->use_empty()) |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1621 | return 0; |
| 1622 | EmitFPutS(CI->getOperand(3), CI->getOperand(1), B); |
| 1623 | return CI; |
| 1624 | } |
| 1625 | return 0; |
| 1626 | } |
| 1627 | }; |
| 1628 | |
Bill Wendling | ac17822 | 2008-05-05 21:37:59 +0000 | [diff] [blame] | 1629 | } // end anonymous namespace. |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1630 | |
| 1631 | //===----------------------------------------------------------------------===// |
| 1632 | // SimplifyLibCalls Pass Implementation |
| 1633 | //===----------------------------------------------------------------------===// |
| 1634 | |
| 1635 | namespace { |
| 1636 | /// This pass optimizes well known library functions from libc and libm. |
| 1637 | /// |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 1638 | class SimplifyLibCalls : public FunctionPass { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1639 | StringMap<LibCallOptimization*> Optimizations; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1640 | // String and Memory LibCall Optimizations |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 1641 | StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp; |
| 1642 | StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen; |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 1643 | StrToOpt StrTo; StrStrOpt StrStr; |
| 1644 | MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1645 | // Math Library Optimizations |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1646 | PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1647 | // Integer Optimizations |
Chris Lattner | 313f0e6 | 2008-06-09 08:26:51 +0000 | [diff] [blame] | 1648 | FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii; |
| 1649 | ToAsciiOpt ToAscii; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1650 | // Formatting and IO Optimizations |
| 1651 | SPrintFOpt SPrintF; PrintFOpt PrintF; |
| 1652 | FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF; |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1653 | |
| 1654 | // Object Size Checking |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1655 | MemCpyChkOpt MemCpyChk; MemSetChkOpt MemSetChk; MemMoveChkOpt MemMoveChk; |
Eric Christopher | 7672d04 | 2010-01-23 05:29:06 +0000 | [diff] [blame] | 1656 | StrCpyChkOpt StrCpyChk; |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1657 | |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 1658 | bool Modified; // This is only used by doInitialization. |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1659 | public: |
| 1660 | static char ID; // Pass identification |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 1661 | SimplifyLibCalls() : FunctionPass(&ID) {} |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1662 | |
| 1663 | void InitOptimizations(); |
| 1664 | bool runOnFunction(Function &F); |
| 1665 | |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1666 | void setDoesNotAccessMemory(Function &F); |
| 1667 | void setOnlyReadsMemory(Function &F); |
| 1668 | void setDoesNotThrow(Function &F); |
| 1669 | void setDoesNotCapture(Function &F, unsigned n); |
| 1670 | void setDoesNotAlias(Function &F, unsigned n); |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 1671 | bool doInitialization(Module &M); |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1672 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1673 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1674 | } |
| 1675 | }; |
| 1676 | char SimplifyLibCalls::ID = 0; |
| 1677 | } // end anonymous namespace. |
| 1678 | |
| 1679 | static RegisterPass<SimplifyLibCalls> |
| 1680 | X("simplify-libcalls", "Simplify well-known library calls"); |
| 1681 | |
| 1682 | // Public interface to the Simplify LibCalls pass. |
| 1683 | FunctionPass *llvm::createSimplifyLibCallsPass() { |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1684 | return new SimplifyLibCalls(); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /// Optimizations - Populate the Optimizations map with all the optimizations |
| 1688 | /// we know. |
| 1689 | void SimplifyLibCalls::InitOptimizations() { |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1690 | // String and Memory LibCall Optimizations |
| 1691 | Optimizations["strcat"] = &StrCat; |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 1692 | Optimizations["strncat"] = &StrNCat; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1693 | Optimizations["strchr"] = &StrChr; |
| 1694 | Optimizations["strcmp"] = &StrCmp; |
| 1695 | Optimizations["strncmp"] = &StrNCmp; |
| 1696 | Optimizations["strcpy"] = &StrCpy; |
Chris Lattner | f5b6bc7 | 2009-04-12 05:06:39 +0000 | [diff] [blame] | 1697 | Optimizations["strncpy"] = &StrNCpy; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1698 | Optimizations["strlen"] = &StrLen; |
Nick Lewycky | 4c49841 | 2009-02-13 15:31:46 +0000 | [diff] [blame] | 1699 | Optimizations["strtol"] = &StrTo; |
| 1700 | Optimizations["strtod"] = &StrTo; |
| 1701 | Optimizations["strtof"] = &StrTo; |
| 1702 | Optimizations["strtoul"] = &StrTo; |
| 1703 | Optimizations["strtoll"] = &StrTo; |
| 1704 | Optimizations["strtold"] = &StrTo; |
| 1705 | Optimizations["strtoull"] = &StrTo; |
Chris Lattner | 2460411 | 2009-12-16 09:32:05 +0000 | [diff] [blame] | 1706 | Optimizations["strstr"] = &StrStr; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1707 | Optimizations["memcmp"] = &MemCmp; |
| 1708 | Optimizations["memcpy"] = &MemCpy; |
Eli Friedman | d83ae7d | 2008-11-30 08:32:11 +0000 | [diff] [blame] | 1709 | Optimizations["memmove"] = &MemMove; |
| 1710 | Optimizations["memset"] = &MemSet; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1711 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1712 | // Math Library Optimizations |
| 1713 | Optimizations["powf"] = &Pow; |
| 1714 | Optimizations["pow"] = &Pow; |
| 1715 | Optimizations["powl"] = &Pow; |
Dale Johannesen | 53bfbbc | 2008-09-04 18:30:46 +0000 | [diff] [blame] | 1716 | Optimizations["llvm.pow.f32"] = &Pow; |
| 1717 | Optimizations["llvm.pow.f64"] = &Pow; |
| 1718 | Optimizations["llvm.pow.f80"] = &Pow; |
| 1719 | Optimizations["llvm.pow.f128"] = &Pow; |
| 1720 | Optimizations["llvm.pow.ppcf128"] = &Pow; |
Chris Lattner | e818f77 | 2008-05-02 18:43:35 +0000 | [diff] [blame] | 1721 | Optimizations["exp2l"] = &Exp2; |
| 1722 | Optimizations["exp2"] = &Exp2; |
| 1723 | Optimizations["exp2f"] = &Exp2; |
Dale Johannesen | 53bfbbc | 2008-09-04 18:30:46 +0000 | [diff] [blame] | 1724 | Optimizations["llvm.exp2.ppcf128"] = &Exp2; |
| 1725 | Optimizations["llvm.exp2.f128"] = &Exp2; |
| 1726 | Optimizations["llvm.exp2.f80"] = &Exp2; |
| 1727 | Optimizations["llvm.exp2.f64"] = &Exp2; |
| 1728 | Optimizations["llvm.exp2.f32"] = &Exp2; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1729 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1730 | #ifdef HAVE_FLOORF |
| 1731 | Optimizations["floor"] = &UnaryDoubleFP; |
| 1732 | #endif |
| 1733 | #ifdef HAVE_CEILF |
| 1734 | Optimizations["ceil"] = &UnaryDoubleFP; |
| 1735 | #endif |
| 1736 | #ifdef HAVE_ROUNDF |
| 1737 | Optimizations["round"] = &UnaryDoubleFP; |
| 1738 | #endif |
| 1739 | #ifdef HAVE_RINTF |
| 1740 | Optimizations["rint"] = &UnaryDoubleFP; |
| 1741 | #endif |
| 1742 | #ifdef HAVE_NEARBYINTF |
| 1743 | Optimizations["nearbyint"] = &UnaryDoubleFP; |
| 1744 | #endif |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1745 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1746 | // Integer Optimizations |
| 1747 | Optimizations["ffs"] = &FFS; |
| 1748 | Optimizations["ffsl"] = &FFS; |
| 1749 | Optimizations["ffsll"] = &FFS; |
Chris Lattner | 313f0e6 | 2008-06-09 08:26:51 +0000 | [diff] [blame] | 1750 | Optimizations["abs"] = &Abs; |
| 1751 | Optimizations["labs"] = &Abs; |
| 1752 | Optimizations["llabs"] = &Abs; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1753 | Optimizations["isdigit"] = &IsDigit; |
| 1754 | Optimizations["isascii"] = &IsAscii; |
| 1755 | Optimizations["toascii"] = &ToAscii; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1756 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1757 | // Formatting and IO Optimizations |
| 1758 | Optimizations["sprintf"] = &SPrintF; |
| 1759 | Optimizations["printf"] = &PrintF; |
| 1760 | Optimizations["fwrite"] = &FWrite; |
| 1761 | Optimizations["fputs"] = &FPuts; |
| 1762 | Optimizations["fprintf"] = &FPrintF; |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1763 | |
| 1764 | // Object Size Checking |
Eric Christopher | 80bf1d5 | 2009-11-21 01:01:30 +0000 | [diff] [blame] | 1765 | Optimizations["__memcpy_chk"] = &MemCpyChk; |
| 1766 | Optimizations["__memset_chk"] = &MemSetChk; |
| 1767 | Optimizations["__memmove_chk"] = &MemMoveChk; |
Eric Christopher | 7672d04 | 2010-01-23 05:29:06 +0000 | [diff] [blame] | 1768 | Optimizations["__strcpy_chk"] = &StrCpyChk; |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | |
| 1772 | /// runOnFunction - Top level algorithm. |
| 1773 | /// |
| 1774 | bool SimplifyLibCalls::runOnFunction(Function &F) { |
| 1775 | if (Optimizations.empty()) |
| 1776 | InitOptimizations(); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1777 | |
Dan Gohman | f14d919 | 2009-08-18 00:48:13 +0000 | [diff] [blame] | 1778 | const TargetData *TD = getAnalysisIfAvailable<TargetData>(); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1779 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 1780 | IRBuilder<> Builder(F.getContext()); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1781 | |
| 1782 | bool Changed = false; |
| 1783 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 1784 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { |
| 1785 | // Ignore non-calls. |
| 1786 | CallInst *CI = dyn_cast<CallInst>(I++); |
| 1787 | if (!CI) continue; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1788 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1789 | // Ignore indirect calls and calls to non-external functions. |
| 1790 | Function *Callee = CI->getCalledFunction(); |
| 1791 | if (Callee == 0 || !Callee->isDeclaration() || |
| 1792 | !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage())) |
| 1793 | continue; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1794 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1795 | // Ignore unknown calls. |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1796 | LibCallOptimization *LCO = Optimizations.lookup(Callee->getName()); |
| 1797 | if (!LCO) continue; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1798 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1799 | // Set the builder to the instruction after the call. |
| 1800 | Builder.SetInsertPoint(BB, I); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1801 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1802 | // Try to optimize this call. |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1803 | Value *Result = LCO->OptimizeCall(CI, TD, Builder); |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1804 | if (Result == 0) continue; |
| 1805 | |
David Greene | 6a6b90e | 2010-01-05 01:27:21 +0000 | [diff] [blame] | 1806 | DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI; |
| 1807 | dbgs() << " into: " << *Result << "\n"); |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1808 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1809 | // Something changed! |
| 1810 | Changed = true; |
| 1811 | ++NumSimplified; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1812 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1813 | // Inspect the instruction after the call (which was potentially just |
| 1814 | // added) next. |
| 1815 | I = CI; ++I; |
Eric Christopher | 37c8b86 | 2009-10-07 21:14:25 +0000 | [diff] [blame] | 1816 | |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 1817 | if (CI != Result && !CI->use_empty()) { |
| 1818 | CI->replaceAllUsesWith(Result); |
| 1819 | if (!Result->hasName()) |
| 1820 | Result->takeName(CI); |
| 1821 | } |
| 1822 | CI->eraseFromParent(); |
| 1823 | } |
| 1824 | } |
| 1825 | return Changed; |
| 1826 | } |
| 1827 | |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 1828 | // Utility methods for doInitialization. |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1829 | |
| 1830 | void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) { |
| 1831 | if (!F.doesNotAccessMemory()) { |
| 1832 | F.setDoesNotAccessMemory(); |
| 1833 | ++NumAnnotated; |
| 1834 | Modified = true; |
| 1835 | } |
| 1836 | } |
| 1837 | void SimplifyLibCalls::setOnlyReadsMemory(Function &F) { |
| 1838 | if (!F.onlyReadsMemory()) { |
| 1839 | F.setOnlyReadsMemory(); |
| 1840 | ++NumAnnotated; |
| 1841 | Modified = true; |
| 1842 | } |
| 1843 | } |
| 1844 | void SimplifyLibCalls::setDoesNotThrow(Function &F) { |
| 1845 | if (!F.doesNotThrow()) { |
| 1846 | F.setDoesNotThrow(); |
| 1847 | ++NumAnnotated; |
| 1848 | Modified = true; |
| 1849 | } |
| 1850 | } |
| 1851 | void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) { |
| 1852 | if (!F.doesNotCapture(n)) { |
| 1853 | F.setDoesNotCapture(n); |
| 1854 | ++NumAnnotated; |
| 1855 | Modified = true; |
| 1856 | } |
| 1857 | } |
| 1858 | void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) { |
| 1859 | if (!F.doesNotAlias(n)) { |
| 1860 | F.setDoesNotAlias(n); |
| 1861 | ++NumAnnotated; |
| 1862 | Modified = true; |
| 1863 | } |
| 1864 | } |
| 1865 | |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 1866 | /// doInitialization - Add attributes to well-known functions. |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1867 | /// |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 1868 | bool SimplifyLibCalls::doInitialization(Module &M) { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1869 | Modified = false; |
| 1870 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 1871 | Function &F = *I; |
| 1872 | if (!F.isDeclaration()) |
| 1873 | continue; |
| 1874 | |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1875 | if (!F.hasName()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1876 | continue; |
| 1877 | |
| 1878 | const FunctionType *FTy = F.getFunctionType(); |
| 1879 | |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1880 | StringRef Name = F.getName(); |
| 1881 | switch (Name[0]) { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1882 | case 's': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1883 | if (Name == "strlen") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1884 | if (FTy->getNumParams() != 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1885 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1886 | continue; |
| 1887 | setOnlyReadsMemory(F); |
| 1888 | setDoesNotThrow(F); |
| 1889 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1890 | } else if (Name == "strcpy" || |
| 1891 | Name == "stpcpy" || |
| 1892 | Name == "strcat" || |
| 1893 | Name == "strtol" || |
| 1894 | Name == "strtod" || |
| 1895 | Name == "strtof" || |
| 1896 | Name == "strtoul" || |
| 1897 | Name == "strtoll" || |
| 1898 | Name == "strtold" || |
| 1899 | Name == "strncat" || |
| 1900 | Name == "strncpy" || |
| 1901 | Name == "strtoull") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1902 | if (FTy->getNumParams() < 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1903 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1904 | continue; |
| 1905 | setDoesNotThrow(F); |
| 1906 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1907 | } else if (Name == "strxfrm") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1908 | if (FTy->getNumParams() != 3 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1909 | !FTy->getParamType(0)->isPointerTy() || |
| 1910 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1911 | continue; |
| 1912 | setDoesNotThrow(F); |
| 1913 | setDoesNotCapture(F, 1); |
| 1914 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1915 | } else if (Name == "strcmp" || |
| 1916 | Name == "strspn" || |
| 1917 | Name == "strncmp" || |
| 1918 | Name ==" strcspn" || |
| 1919 | Name == "strcoll" || |
| 1920 | Name == "strcasecmp" || |
| 1921 | Name == "strncasecmp") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1922 | if (FTy->getNumParams() < 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1923 | !FTy->getParamType(0)->isPointerTy() || |
| 1924 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1925 | continue; |
| 1926 | setOnlyReadsMemory(F); |
| 1927 | setDoesNotThrow(F); |
| 1928 | setDoesNotCapture(F, 1); |
| 1929 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1930 | } else if (Name == "strstr" || |
| 1931 | Name == "strpbrk") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1932 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1933 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1934 | continue; |
| 1935 | setOnlyReadsMemory(F); |
| 1936 | setDoesNotThrow(F); |
| 1937 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1938 | } else if (Name == "strtok" || |
| 1939 | Name == "strtok_r") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1940 | if (FTy->getNumParams() < 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1941 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1942 | continue; |
| 1943 | setDoesNotThrow(F); |
| 1944 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1945 | } else if (Name == "scanf" || |
| 1946 | Name == "setbuf" || |
| 1947 | Name == "setvbuf") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1948 | if (FTy->getNumParams() < 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1949 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1950 | continue; |
| 1951 | setDoesNotThrow(F); |
| 1952 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1953 | } else if (Name == "strdup" || |
| 1954 | Name == "strndup") { |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 1955 | if (FTy->getNumParams() < 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1956 | !FTy->getReturnType()->isPointerTy() || |
| 1957 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 6cd0c04 | 2009-01-05 00:07:50 +0000 | [diff] [blame] | 1958 | continue; |
| 1959 | setDoesNotThrow(F); |
| 1960 | setDoesNotAlias(F, 0); |
| 1961 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1962 | } else if (Name == "stat" || |
| 1963 | Name == "sscanf" || |
| 1964 | Name == "sprintf" || |
| 1965 | Name == "statvfs") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 1966 | if (FTy->getNumParams() < 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1967 | !FTy->getParamType(0)->isPointerTy() || |
| 1968 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 1969 | continue; |
| 1970 | setDoesNotThrow(F); |
| 1971 | setDoesNotCapture(F, 1); |
| 1972 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1973 | } else if (Name == "snprintf") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 1974 | if (FTy->getNumParams() != 3 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1975 | !FTy->getParamType(0)->isPointerTy() || |
| 1976 | !FTy->getParamType(2)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 1977 | continue; |
| 1978 | setDoesNotThrow(F); |
| 1979 | setDoesNotCapture(F, 1); |
| 1980 | setDoesNotCapture(F, 3); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1981 | } else if (Name == "setitimer") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 1982 | if (FTy->getNumParams() != 3 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1983 | !FTy->getParamType(1)->isPointerTy() || |
| 1984 | !FTy->getParamType(2)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 1985 | continue; |
| 1986 | setDoesNotThrow(F); |
| 1987 | setDoesNotCapture(F, 2); |
| 1988 | setDoesNotCapture(F, 3); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1989 | } else if (Name == "system") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 1990 | if (FTy->getNumParams() != 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1991 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 1992 | continue; |
| 1993 | // May throw; "system" is a valid pthread cancellation point. |
| 1994 | setDoesNotCapture(F, 1); |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 1995 | } |
| 1996 | break; |
| 1997 | case 'm': |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1998 | if (Name == "malloc") { |
| 1999 | if (FTy->getNumParams() != 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2000 | !FTy->getReturnType()->isPointerTy()) |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 2001 | continue; |
| 2002 | setDoesNotThrow(F); |
| 2003 | setDoesNotAlias(F, 0); |
| 2004 | } else if (Name == "memcmp") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2005 | if (FTy->getNumParams() != 3 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2006 | !FTy->getParamType(0)->isPointerTy() || |
| 2007 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2008 | continue; |
| 2009 | setOnlyReadsMemory(F); |
| 2010 | setDoesNotThrow(F); |
| 2011 | setDoesNotCapture(F, 1); |
| 2012 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2013 | } else if (Name == "memchr" || |
| 2014 | Name == "memrchr") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2015 | if (FTy->getNumParams() != 3) |
| 2016 | continue; |
| 2017 | setOnlyReadsMemory(F); |
| 2018 | setDoesNotThrow(F); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2019 | } else if (Name == "modf" || |
| 2020 | Name == "modff" || |
| 2021 | Name == "modfl" || |
| 2022 | Name == "memcpy" || |
| 2023 | Name == "memccpy" || |
| 2024 | Name == "memmove") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2025 | if (FTy->getNumParams() < 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2026 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2027 | continue; |
| 2028 | setDoesNotThrow(F); |
| 2029 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2030 | } else if (Name == "memalign") { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2031 | if (!FTy->getReturnType()->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2032 | continue; |
| 2033 | setDoesNotAlias(F, 0); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2034 | } else if (Name == "mkdir" || |
| 2035 | Name == "mktime") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2036 | if (FTy->getNumParams() == 0 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2037 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2038 | continue; |
| 2039 | setDoesNotThrow(F); |
| 2040 | setDoesNotCapture(F, 1); |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2041 | } |
| 2042 | break; |
| 2043 | case 'r': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2044 | if (Name == "realloc") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2045 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2046 | !FTy->getParamType(0)->isPointerTy() || |
| 2047 | !FTy->getReturnType()->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2048 | continue; |
| 2049 | setDoesNotThrow(F); |
| 2050 | setDoesNotAlias(F, 0); |
| 2051 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2052 | } else if (Name == "read") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2053 | if (FTy->getNumParams() != 3 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2054 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2055 | continue; |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2056 | // May throw; "read" is a valid pthread cancellation point. |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2057 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2058 | } else if (Name == "rmdir" || |
| 2059 | Name == "rewind" || |
| 2060 | Name == "remove" || |
| 2061 | Name == "realpath") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2062 | if (FTy->getNumParams() < 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2063 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2064 | continue; |
| 2065 | setDoesNotThrow(F); |
| 2066 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2067 | } else if (Name == "rename" || |
| 2068 | Name == "readlink") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2069 | if (FTy->getNumParams() < 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2070 | !FTy->getParamType(0)->isPointerTy() || |
| 2071 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2072 | continue; |
| 2073 | setDoesNotThrow(F); |
| 2074 | setDoesNotCapture(F, 1); |
| 2075 | setDoesNotCapture(F, 2); |
| 2076 | } |
| 2077 | break; |
| 2078 | case 'w': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2079 | if (Name == "write") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2080 | if (FTy->getNumParams() != 3 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2081 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2082 | continue; |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2083 | // May throw; "write" is a valid pthread cancellation point. |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2084 | setDoesNotCapture(F, 2); |
| 2085 | } |
| 2086 | break; |
| 2087 | case 'b': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2088 | if (Name == "bcopy") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2089 | if (FTy->getNumParams() != 3 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2090 | !FTy->getParamType(0)->isPointerTy() || |
| 2091 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2092 | continue; |
| 2093 | setDoesNotThrow(F); |
| 2094 | setDoesNotCapture(F, 1); |
| 2095 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2096 | } else if (Name == "bcmp") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2097 | if (FTy->getNumParams() != 3 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2098 | !FTy->getParamType(0)->isPointerTy() || |
| 2099 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2100 | continue; |
| 2101 | setDoesNotThrow(F); |
| 2102 | setOnlyReadsMemory(F); |
| 2103 | setDoesNotCapture(F, 1); |
| 2104 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2105 | } else if (Name == "bzero") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2106 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2107 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2108 | continue; |
| 2109 | setDoesNotThrow(F); |
| 2110 | setDoesNotCapture(F, 1); |
| 2111 | } |
| 2112 | break; |
| 2113 | case 'c': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2114 | if (Name == "calloc") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2115 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2116 | !FTy->getReturnType()->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2117 | continue; |
| 2118 | setDoesNotThrow(F); |
| 2119 | setDoesNotAlias(F, 0); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2120 | } else if (Name == "chmod" || |
| 2121 | Name == "chown" || |
| 2122 | Name == "ctermid" || |
| 2123 | Name == "clearerr" || |
| 2124 | Name == "closedir") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2125 | if (FTy->getNumParams() == 0 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2126 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2127 | continue; |
| 2128 | setDoesNotThrow(F); |
| 2129 | setDoesNotCapture(F, 1); |
| 2130 | } |
| 2131 | break; |
| 2132 | case 'a': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2133 | if (Name == "atoi" || |
| 2134 | Name == "atol" || |
| 2135 | Name == "atof" || |
| 2136 | Name == "atoll") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2137 | if (FTy->getNumParams() != 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2138 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2139 | continue; |
| 2140 | setDoesNotThrow(F); |
| 2141 | setOnlyReadsMemory(F); |
| 2142 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2143 | } else if (Name == "access") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2144 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2145 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2146 | continue; |
| 2147 | setDoesNotThrow(F); |
| 2148 | setDoesNotCapture(F, 1); |
| 2149 | } |
| 2150 | break; |
| 2151 | case 'f': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2152 | if (Name == "fopen") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2153 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2154 | !FTy->getReturnType()->isPointerTy() || |
| 2155 | !FTy->getParamType(0)->isPointerTy() || |
| 2156 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2157 | continue; |
| 2158 | setDoesNotThrow(F); |
| 2159 | setDoesNotAlias(F, 0); |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2160 | setDoesNotCapture(F, 1); |
| 2161 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2162 | } else if (Name == "fdopen") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2163 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2164 | !FTy->getReturnType()->isPointerTy() || |
| 2165 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2166 | continue; |
| 2167 | setDoesNotThrow(F); |
| 2168 | setDoesNotAlias(F, 0); |
| 2169 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2170 | } else if (Name == "feof" || |
| 2171 | Name == "free" || |
| 2172 | Name == "fseek" || |
| 2173 | Name == "ftell" || |
| 2174 | Name == "fgetc" || |
| 2175 | Name == "fseeko" || |
| 2176 | Name == "ftello" || |
| 2177 | Name == "fileno" || |
| 2178 | Name == "fflush" || |
| 2179 | Name == "fclose" || |
| 2180 | Name == "fsetpos" || |
| 2181 | Name == "flockfile" || |
| 2182 | Name == "funlockfile" || |
| 2183 | Name == "ftrylockfile") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2184 | if (FTy->getNumParams() == 0 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2185 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2186 | continue; |
| 2187 | setDoesNotThrow(F); |
| 2188 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2189 | } else if (Name == "ferror") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2190 | if (FTy->getNumParams() != 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2191 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2192 | continue; |
| 2193 | setDoesNotThrow(F); |
| 2194 | setDoesNotCapture(F, 1); |
| 2195 | setOnlyReadsMemory(F); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2196 | } else if (Name == "fputc" || |
| 2197 | Name == "fstat" || |
| 2198 | Name == "frexp" || |
| 2199 | Name == "frexpf" || |
| 2200 | Name == "frexpl" || |
| 2201 | Name == "fstatvfs") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2202 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2203 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2204 | continue; |
| 2205 | setDoesNotThrow(F); |
| 2206 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2207 | } else if (Name == "fgets") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2208 | if (FTy->getNumParams() != 3 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2209 | !FTy->getParamType(0)->isPointerTy() || |
| 2210 | !FTy->getParamType(2)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2211 | continue; |
| 2212 | setDoesNotThrow(F); |
| 2213 | setDoesNotCapture(F, 3); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2214 | } else if (Name == "fread" || |
| 2215 | Name == "fwrite") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2216 | if (FTy->getNumParams() != 4 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2217 | !FTy->getParamType(0)->isPointerTy() || |
| 2218 | !FTy->getParamType(3)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2219 | continue; |
| 2220 | setDoesNotThrow(F); |
| 2221 | setDoesNotCapture(F, 1); |
| 2222 | setDoesNotCapture(F, 4); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2223 | } else if (Name == "fputs" || |
| 2224 | Name == "fscanf" || |
| 2225 | Name == "fprintf" || |
| 2226 | Name == "fgetpos") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2227 | if (FTy->getNumParams() < 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2228 | !FTy->getParamType(0)->isPointerTy() || |
| 2229 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2230 | continue; |
| 2231 | setDoesNotThrow(F); |
| 2232 | setDoesNotCapture(F, 1); |
| 2233 | setDoesNotCapture(F, 2); |
| 2234 | } |
| 2235 | break; |
| 2236 | case 'g': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2237 | if (Name == "getc" || |
| 2238 | Name == "getlogin_r" || |
| 2239 | Name == "getc_unlocked") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2240 | if (FTy->getNumParams() == 0 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2241 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2242 | continue; |
| 2243 | setDoesNotThrow(F); |
| 2244 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2245 | } else if (Name == "getenv") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2246 | if (FTy->getNumParams() != 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2247 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2248 | continue; |
| 2249 | setDoesNotThrow(F); |
| 2250 | setOnlyReadsMemory(F); |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2251 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2252 | } else if (Name == "gets" || |
| 2253 | Name == "getchar") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2254 | setDoesNotThrow(F); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2255 | } else if (Name == "getitimer") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2256 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2257 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2258 | continue; |
| 2259 | setDoesNotThrow(F); |
| 2260 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2261 | } else if (Name == "getpwnam") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2262 | if (FTy->getNumParams() != 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2263 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2264 | continue; |
| 2265 | setDoesNotThrow(F); |
| 2266 | setDoesNotCapture(F, 1); |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2267 | } |
| 2268 | break; |
| 2269 | case 'u': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2270 | if (Name == "ungetc") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2271 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2272 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2273 | continue; |
| 2274 | setDoesNotThrow(F); |
| 2275 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2276 | } else if (Name == "uname" || |
| 2277 | Name == "unlink" || |
| 2278 | Name == "unsetenv") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2279 | if (FTy->getNumParams() != 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2280 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2281 | continue; |
| 2282 | setDoesNotThrow(F); |
| 2283 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2284 | } else if (Name == "utime" || |
| 2285 | Name == "utimes") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2286 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2287 | !FTy->getParamType(0)->isPointerTy() || |
| 2288 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2289 | continue; |
| 2290 | setDoesNotThrow(F); |
| 2291 | setDoesNotCapture(F, 1); |
| 2292 | setDoesNotCapture(F, 2); |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2293 | } |
| 2294 | break; |
| 2295 | case 'p': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2296 | if (Name == "putc") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2297 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2298 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2299 | continue; |
| 2300 | setDoesNotThrow(F); |
| 2301 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2302 | } else if (Name == "puts" || |
| 2303 | Name == "printf" || |
| 2304 | Name == "perror") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2305 | if (FTy->getNumParams() != 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2306 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2307 | continue; |
| 2308 | setDoesNotThrow(F); |
| 2309 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2310 | } else if (Name == "pread" || |
| 2311 | Name == "pwrite") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2312 | if (FTy->getNumParams() != 4 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2313 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2314 | continue; |
| 2315 | // May throw; these are valid pthread cancellation points. |
| 2316 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2317 | } else if (Name == "putchar") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2318 | setDoesNotThrow(F); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2319 | } else if (Name == "popen") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2320 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2321 | !FTy->getReturnType()->isPointerTy() || |
| 2322 | !FTy->getParamType(0)->isPointerTy() || |
| 2323 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2324 | continue; |
| 2325 | setDoesNotThrow(F); |
| 2326 | setDoesNotAlias(F, 0); |
| 2327 | setDoesNotCapture(F, 1); |
| 2328 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2329 | } else if (Name == "pclose") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2330 | if (FTy->getNumParams() != 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2331 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2332 | continue; |
| 2333 | setDoesNotThrow(F); |
| 2334 | setDoesNotCapture(F, 1); |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2335 | } |
| 2336 | break; |
| 2337 | case 'v': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2338 | if (Name == "vscanf") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2339 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2340 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2341 | continue; |
| 2342 | setDoesNotThrow(F); |
| 2343 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2344 | } else if (Name == "vsscanf" || |
| 2345 | Name == "vfscanf") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2346 | if (FTy->getNumParams() != 3 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2347 | !FTy->getParamType(1)->isPointerTy() || |
| 2348 | !FTy->getParamType(2)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2349 | continue; |
| 2350 | setDoesNotThrow(F); |
| 2351 | setDoesNotCapture(F, 1); |
| 2352 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2353 | } else if (Name == "valloc") { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2354 | if (!FTy->getReturnType()->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2355 | continue; |
| 2356 | setDoesNotThrow(F); |
| 2357 | setDoesNotAlias(F, 0); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2358 | } else if (Name == "vprintf") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2359 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2360 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2361 | continue; |
| 2362 | setDoesNotThrow(F); |
| 2363 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2364 | } else if (Name == "vfprintf" || |
| 2365 | Name == "vsprintf") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2366 | if (FTy->getNumParams() != 3 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2367 | !FTy->getParamType(0)->isPointerTy() || |
| 2368 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2369 | continue; |
| 2370 | setDoesNotThrow(F); |
| 2371 | setDoesNotCapture(F, 1); |
| 2372 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2373 | } else if (Name == "vsnprintf") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2374 | if (FTy->getNumParams() != 4 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2375 | !FTy->getParamType(0)->isPointerTy() || |
| 2376 | !FTy->getParamType(2)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2377 | continue; |
| 2378 | setDoesNotThrow(F); |
| 2379 | setDoesNotCapture(F, 1); |
| 2380 | setDoesNotCapture(F, 3); |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2381 | } |
| 2382 | break; |
| 2383 | case 'o': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2384 | if (Name == "open") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2385 | if (FTy->getNumParams() < 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2386 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2387 | continue; |
| 2388 | // May throw; "open" is a valid pthread cancellation point. |
| 2389 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2390 | } else if (Name == "opendir") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2391 | if (FTy->getNumParams() != 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2392 | !FTy->getReturnType()->isPointerTy() || |
| 2393 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2394 | continue; |
| 2395 | setDoesNotThrow(F); |
| 2396 | setDoesNotAlias(F, 0); |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2397 | setDoesNotCapture(F, 1); |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2398 | } |
| 2399 | break; |
| 2400 | case 't': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2401 | if (Name == "tmpfile") { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2402 | if (!FTy->getReturnType()->isPointerTy()) |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2403 | continue; |
| 2404 | setDoesNotThrow(F); |
| 2405 | setDoesNotAlias(F, 0); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2406 | } else if (Name == "times") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2407 | if (FTy->getNumParams() != 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2408 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2409 | continue; |
| 2410 | setDoesNotThrow(F); |
| 2411 | setDoesNotCapture(F, 1); |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2412 | } |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2413 | break; |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2414 | case 'h': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2415 | if (Name == "htonl" || |
| 2416 | Name == "htons") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2417 | setDoesNotThrow(F); |
| 2418 | setDoesNotAccessMemory(F); |
| 2419 | } |
| 2420 | break; |
| 2421 | case 'n': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2422 | if (Name == "ntohl" || |
| 2423 | Name == "ntohs") { |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2424 | setDoesNotThrow(F); |
| 2425 | setDoesNotAccessMemory(F); |
| 2426 | } |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2427 | break; |
| 2428 | case 'l': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2429 | if (Name == "lstat") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2430 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2431 | !FTy->getParamType(0)->isPointerTy() || |
| 2432 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2433 | continue; |
| 2434 | setDoesNotThrow(F); |
| 2435 | setDoesNotCapture(F, 1); |
| 2436 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2437 | } else if (Name == "lchown") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2438 | if (FTy->getNumParams() != 3 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2439 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2440 | continue; |
| 2441 | setDoesNotThrow(F); |
| 2442 | setDoesNotCapture(F, 1); |
| 2443 | } |
| 2444 | break; |
| 2445 | case 'q': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2446 | if (Name == "qsort") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2447 | if (FTy->getNumParams() != 4 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2448 | !FTy->getParamType(3)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2449 | continue; |
| 2450 | // May throw; places call through function pointer. |
| 2451 | setDoesNotCapture(F, 4); |
| 2452 | } |
| 2453 | break; |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2454 | case '_': |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2455 | if (Name == "__strdup" || |
| 2456 | Name == "__strndup") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2457 | if (FTy->getNumParams() < 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2458 | !FTy->getReturnType()->isPointerTy() || |
| 2459 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2460 | continue; |
| 2461 | setDoesNotThrow(F); |
| 2462 | setDoesNotAlias(F, 0); |
| 2463 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2464 | } else if (Name == "__strtok_r") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2465 | if (FTy->getNumParams() != 3 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2466 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2467 | continue; |
| 2468 | setDoesNotThrow(F); |
| 2469 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2470 | } else if (Name == "_IO_getc") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2471 | if (FTy->getNumParams() != 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2472 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2473 | continue; |
| 2474 | setDoesNotThrow(F); |
| 2475 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2476 | } else if (Name == "_IO_putc") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2477 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2478 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2479 | continue; |
| 2480 | setDoesNotThrow(F); |
| 2481 | setDoesNotCapture(F, 2); |
| 2482 | } |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2483 | break; |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2484 | case 1: |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2485 | if (Name == "\1__isoc99_scanf") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2486 | if (FTy->getNumParams() < 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2487 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2488 | continue; |
| 2489 | setDoesNotThrow(F); |
| 2490 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2491 | } else if (Name == "\1stat64" || |
| 2492 | Name == "\1lstat64" || |
| 2493 | Name == "\1statvfs64" || |
| 2494 | Name == "\1__isoc99_sscanf") { |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2495 | if (FTy->getNumParams() < 1 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2496 | !FTy->getParamType(0)->isPointerTy() || |
| 2497 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2498 | continue; |
| 2499 | setDoesNotThrow(F); |
| 2500 | setDoesNotCapture(F, 1); |
| 2501 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2502 | } else if (Name == "\1fopen64") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2503 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2504 | !FTy->getReturnType()->isPointerTy() || |
| 2505 | !FTy->getParamType(0)->isPointerTy() || |
| 2506 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2507 | continue; |
| 2508 | setDoesNotThrow(F); |
| 2509 | setDoesNotAlias(F, 0); |
| 2510 | setDoesNotCapture(F, 1); |
| 2511 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2512 | } else if (Name == "\1fseeko64" || |
| 2513 | Name == "\1ftello64") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2514 | if (FTy->getNumParams() == 0 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2515 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2516 | continue; |
| 2517 | setDoesNotThrow(F); |
| 2518 | setDoesNotCapture(F, 1); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2519 | } else if (Name == "\1tmpfile64") { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2520 | if (!FTy->getReturnType()->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2521 | continue; |
| 2522 | setDoesNotThrow(F); |
| 2523 | setDoesNotAlias(F, 0); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2524 | } else if (Name == "\1fstat64" || |
| 2525 | Name == "\1fstatvfs64") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2526 | if (FTy->getNumParams() != 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2527 | !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2528 | continue; |
| 2529 | setDoesNotThrow(F); |
| 2530 | setDoesNotCapture(F, 2); |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 2531 | } else if (Name == "\1open64") { |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2532 | if (FTy->getNumParams() < 2 || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2533 | !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | 225f747 | 2009-02-15 22:47:25 +0000 | [diff] [blame] | 2534 | continue; |
| 2535 | // May throw; "open" is a valid pthread cancellation point. |
| 2536 | setDoesNotCapture(F, 1); |
Nick Lewycky | 0b6679d | 2009-01-18 04:34:36 +0000 | [diff] [blame] | 2537 | } |
Nick Lewycky | 0f8df9a | 2009-01-04 20:27:34 +0000 | [diff] [blame] | 2538 | break; |
| 2539 | } |
| 2540 | } |
| 2541 | return Modified; |
| 2542 | } |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 2543 | |
| 2544 | // TODO: |
| 2545 | // Additional cases that we need to add to this file: |
| 2546 | // |
| 2547 | // cbrt: |
| 2548 | // * cbrt(expN(X)) -> expN(x/3) |
| 2549 | // * cbrt(sqrt(x)) -> pow(x,1/6) |
| 2550 | // * cbrt(sqrt(x)) -> pow(x,1/9) |
| 2551 | // |
| 2552 | // cos, cosf, cosl: |
| 2553 | // * cos(-x) -> cos(x) |
| 2554 | // |
| 2555 | // exp, expf, expl: |
| 2556 | // * exp(log(x)) -> x |
| 2557 | // |
| 2558 | // log, logf, logl: |
| 2559 | // * log(exp(x)) -> x |
| 2560 | // * log(x**y) -> y*log(x) |
| 2561 | // * log(exp(y)) -> y*log(e) |
| 2562 | // * log(exp2(y)) -> y*log(2) |
| 2563 | // * log(exp10(y)) -> y*log(10) |
| 2564 | // * log(sqrt(x)) -> 0.5*log(x) |
| 2565 | // * log(pow(x,y)) -> y*log(x) |
| 2566 | // |
| 2567 | // lround, lroundf, lroundl: |
| 2568 | // * lround(cnst) -> cnst' |
| 2569 | // |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 2570 | // pow, powf, powl: |
| 2571 | // * pow(exp(x),y) -> exp(x*y) |
| 2572 | // * pow(sqrt(x),y) -> pow(x,y*0.5) |
| 2573 | // * pow(pow(x,y),z)-> pow(x,y*z) |
| 2574 | // |
| 2575 | // puts: |
| 2576 | // * puts("") -> putchar("\n") |
| 2577 | // |
| 2578 | // round, roundf, roundl: |
| 2579 | // * round(cnst) -> cnst' |
| 2580 | // |
| 2581 | // signbit: |
| 2582 | // * signbit(cnst) -> cnst' |
| 2583 | // * signbit(nncst) -> 0 (if pstv is a non-negative constant) |
| 2584 | // |
| 2585 | // sqrt, sqrtf, sqrtl: |
| 2586 | // * sqrt(expN(x)) -> expN(x*0.5) |
| 2587 | // * sqrt(Nroot(x)) -> pow(x,1/(2*N)) |
| 2588 | // * sqrt(pow(x,y)) -> pow(|x|,y*0.5) |
| 2589 | // |
| 2590 | // stpcpy: |
| 2591 | // * stpcpy(str, "literal") -> |
| 2592 | // llvm.memcpy(str,"literal",strlen("literal")+1,1) |
| 2593 | // strrchr: |
| 2594 | // * strrchr(s,c) -> reverse_offset_of_in(c,s) |
| 2595 | // (if c is a constant integer and s is a constant string) |
| 2596 | // * strrchr(s1,0) -> strchr(s1,0) |
| 2597 | // |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 2598 | // strpbrk: |
| 2599 | // * strpbrk(s,a) -> offset_in_for(s,a) |
| 2600 | // (if s and a are both constant strings) |
| 2601 | // * strpbrk(s,"") -> 0 |
| 2602 | // * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1) |
| 2603 | // |
| 2604 | // strspn, strcspn: |
| 2605 | // * strspn(s,a) -> const_int (if both args are constant) |
| 2606 | // * strspn("",a) -> 0 |
| 2607 | // * strspn(s,"") -> 0 |
| 2608 | // * strcspn(s,a) -> const_int (if both args are constant) |
| 2609 | // * strcspn("",a) -> 0 |
| 2610 | // * strcspn(s,"") -> strlen(a) |
| 2611 | // |
Chris Lattner | fd1cbbe | 2008-05-01 06:25:24 +0000 | [diff] [blame] | 2612 | // tan, tanf, tanl: |
| 2613 | // * tan(atan(x)) -> x |
| 2614 | // |
| 2615 | // trunc, truncf, truncl: |
| 2616 | // * trunc(cnst) -> cnst' |
| 2617 | // |
| 2618 | // |