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