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