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