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