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