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