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