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