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