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