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