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