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