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