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