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