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