blob: f562a1b8feed0e03badb43f9149cc010aa346859 [file] [log] [blame]
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001//===- 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 Lattnere9f9a7e2009-09-03 05:19:59 +000012// 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 Lattnerfd1cbbe2008-05-01 06:25:24 +000015//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "simplify-libcalls"
19#include "llvm/Transforms/Scalar.h"
Eric Christopherb6174e32010-03-05 22:25:30 +000020#include "llvm/Transforms/Utils/BuildLibCalls.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000021#include "llvm/Intrinsics.h"
Owen Andersonfa5cbd62009-07-03 19:42:02 +000022#include "llvm/LLVMContext.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000023#include "llvm/Module.h"
24#include "llvm/Pass.h"
25#include "llvm/Support/IRBuilder.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000026#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000027#include "llvm/Target/TargetData.h"
28#include "llvm/ADT/SmallPtrSet.h"
29#include "llvm/ADT/StringMap.h"
30#include "llvm/ADT/Statistic.h"
Daniel Dunbar473955f2009-07-29 22:00:43 +000031#include "llvm/ADT/STLExtras.h"
Chris Lattner56b4f2b2008-05-01 06:39:12 +000032#include "llvm/Support/Debug.h"
Daniel Dunbarf0443c12009-07-26 08:34:35 +000033#include "llvm/Support/raw_ostream.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000034#include "llvm/Config/config.h"
35using namespace llvm;
36
37STATISTIC(NumSimplified, "Number of library calls simplified");
Nick Lewycky0f8df9a2009-01-04 20:27:34 +000038STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000039
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.
46namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +000047class LibCallOptimization {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000048protected:
49 Function *Caller;
50 const TargetData *TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000051 LLVMContext* Context;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000052public:
Evan Chengeb8c6452010-03-24 20:19:04 +000053 LibCallOptimization() { }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000054 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 Christopher37c8b862009-10-07 21:14:25 +000061 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
Eric Christopher7a61d702008-08-08 19:39:37 +000062 =0;
Eric Christopher37c8b862009-10-07 21:14:25 +000063
Dan Gohmanf14d9192009-08-18 00:48:13 +000064 Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000065 Caller = CI->getParent()->getParent();
Dan Gohmanf14d9192009-08-18 00:48:13 +000066 this->TD = TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000067 if (CI->getCalledFunction())
Owen Andersone922c022009-07-22 00:24:57 +000068 Context = &CI->getCalledFunction()->getContext();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000069 return CallOptimizer(CI->getCalledFunction(), CI, B);
70 }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000071};
72} // End anonymous namespace.
73
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000074
75//===----------------------------------------------------------------------===//
76// Helper Functions
77//===----------------------------------------------------------------------===//
78
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000079/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +000080/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000081static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
82 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
83 UI != E; ++UI) {
84 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
85 if (IC->isEquality())
86 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
87 if (C->isNullValue())
88 continue;
89 // Unknown instruction.
90 return false;
91 }
92 return true;
93}
94
Benjamin Kramer386e9182010-06-15 21:34:25 +000095/// IsOnlyUsedInEqualityComparison - Return true if it is only used in equality
96/// comparisons with With.
97static bool IsOnlyUsedInEqualityComparison(Value *V, Value *With) {
98 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
99 UI != E; ++UI) {
100 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
101 if (IC->isEquality() && IC->getOperand(1) == With)
102 continue;
103 // Unknown instruction.
104 return false;
105 }
106 return true;
107}
108
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000109//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000110// String and Memory LibCall Optimizations
111//===----------------------------------------------------------------------===//
112
113//===---------------------------------------===//
114// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000115namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000116struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000117 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000118 // Verify the "strcat" function prototype.
119 const FunctionType *FT = Callee->getFunctionType();
120 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000121 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000122 FT->getParamType(0) != FT->getReturnType() ||
123 FT->getParamType(1) != FT->getReturnType())
124 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000125
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000126 // Extract some information from the instruction
Eric Christopher551754c2010-04-16 23:37:20 +0000127 Value *Dst = CI->getOperand(1);
128 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000129
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000130 // See if we can get the length of the input string.
131 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000132 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000133 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000134
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000135 // Handle the simple, do-nothing case: strcat(x, "") -> x
136 if (Len == 0)
137 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000138
139 // These optimizations require TargetData.
140 if (!TD) return 0;
141
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000142 EmitStrLenMemCpy(Src, Dst, Len, B);
143 return Dst;
144 }
145
146 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000147 // We need to find the end of the destination string. That's where the
148 // memory is to be moved to. We just generate a call to strlen.
Eric Christopherb6174e32010-03-05 22:25:30 +0000149 Value *DstLen = EmitStrLen(Dst, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000150
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000151 // Now that we have the destination's length, we must index into the
152 // destination's pointer to get the actual memcpy destination (end of
153 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000154 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000155
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000156 // We have enough information to now generate the memcpy call to do the
157 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000158 EmitMemCpy(CpyDst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000159 ConstantInt::get(TD->getIntPtrType(*Context), Len+1),
160 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000161 }
162};
163
164//===---------------------------------------===//
165// 'strncat' Optimizations
166
Chris Lattner3e8b6632009-09-02 06:11:42 +0000167struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000168 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
169 // Verify the "strncat" function prototype.
170 const FunctionType *FT = Callee->getFunctionType();
171 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000172 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000173 FT->getParamType(0) != FT->getReturnType() ||
174 FT->getParamType(1) != FT->getReturnType() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000175 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000176 return 0;
177
178 // Extract some information from the instruction
Eric Christopher551754c2010-04-16 23:37:20 +0000179 Value *Dst = CI->getOperand(1);
180 Value *Src = CI->getOperand(2);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000181 uint64_t Len;
182
183 // We don't do anything if length is not constant
Eric Christopher551754c2010-04-16 23:37:20 +0000184 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000185 Len = LengthArg->getZExtValue();
186 else
187 return 0;
188
189 // See if we can get the length of the input string.
190 uint64_t SrcLen = GetStringLength(Src);
191 if (SrcLen == 0) return 0;
192 --SrcLen; // Unbias length.
193
194 // Handle the simple, do-nothing cases:
195 // strncat(x, "", c) -> x
196 // strncat(x, c, 0) -> x
197 if (SrcLen == 0 || Len == 0) return Dst;
198
Dan Gohmanf14d9192009-08-18 00:48:13 +0000199 // These optimizations require TargetData.
200 if (!TD) return 0;
201
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000202 // We don't optimize this case
203 if (Len < SrcLen) return 0;
204
205 // strncat(x, s, c) -> strcat(x, s)
206 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000207 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000208 return Dst;
209 }
210};
211
212//===---------------------------------------===//
213// 'strchr' Optimizations
214
Chris Lattner3e8b6632009-09-02 06:11:42 +0000215struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000216 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000217 // Verify the "strchr" function prototype.
218 const FunctionType *FT = Callee->getFunctionType();
219 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000220 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000221 FT->getParamType(0) != FT->getReturnType())
222 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000223
Eric Christopher551754c2010-04-16 23:37:20 +0000224 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000225
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000226 // If the second operand is non-constant, see if we can compute the length
227 // of the input string and turn this into memchr.
Eric Christopher551754c2010-04-16 23:37:20 +0000228 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000229 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000230 // These optimizations require TargetData.
231 if (!TD) return 0;
232
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000233 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000234 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000235 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000236
Eric Christopher551754c2010-04-16 23:37:20 +0000237 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000238 ConstantInt::get(TD->getIntPtrType(*Context), Len),
239 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000240 }
241
242 // Otherwise, the character is a constant, see if the first argument is
243 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000244 std::string Str;
245 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000246 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000247
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000248 // strchr can find the nul character.
249 Str += '\0';
250 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000251
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000252 // Compute the offset.
253 uint64_t i = 0;
254 while (1) {
255 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000256 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000257 // Did we find our match?
258 if (Str[i] == CharValue)
259 break;
260 ++i;
261 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000262
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000263 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000264 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000265 return B.CreateGEP(SrcStr, Idx, "strchr");
266 }
267};
268
269//===---------------------------------------===//
270// 'strcmp' Optimizations
271
Chris Lattner3e8b6632009-09-02 06:11:42 +0000272struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000273 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000274 // Verify the "strcmp" function prototype.
275 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000276 if (FT->getNumParams() != 2 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000277 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000278 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000279 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000280 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000281
Eric Christopher551754c2010-04-16 23:37:20 +0000282 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000283 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000284 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000285
Bill Wendling0582ae92009-03-13 04:39:26 +0000286 std::string Str1, Str2;
287 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
288 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000289
Bill Wendling0582ae92009-03-13 04:39:26 +0000290 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000291 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000292
Bill Wendling0582ae92009-03-13 04:39:26 +0000293 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000294 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000295
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000296 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000297 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000298 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000299 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000300
301 // strcmp(P, "x") -> memcmp(P, "x", 2)
302 uint64_t Len1 = GetStringLength(Str1P);
303 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000304 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000305 // These optimizations require TargetData.
306 if (!TD) return 0;
307
Nick Lewycky13a09e22008-12-21 00:19:21 +0000308 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000309 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000310 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000311 }
312
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000313 return 0;
314 }
315};
316
317//===---------------------------------------===//
318// 'strncmp' Optimizations
319
Chris Lattner3e8b6632009-09-02 06:11:42 +0000320struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000321 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000322 // Verify the "strncmp" function prototype.
323 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000324 if (FT->getNumParams() != 3 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000325 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000326 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000327 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000328 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000329 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000330
Eric Christopher551754c2010-04-16 23:37:20 +0000331 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000332 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000333 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000334
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000335 // Get the length argument if it is constant.
336 uint64_t Length;
Eric Christopher551754c2010-04-16 23:37:20 +0000337 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000338 Length = LengthArg->getZExtValue();
339 else
340 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000341
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000342 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000343 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000344
Bill Wendling0582ae92009-03-13 04:39:26 +0000345 std::string Str1, Str2;
346 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
347 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000348
Bill Wendling0582ae92009-03-13 04:39:26 +0000349 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000350 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000351
Bill Wendling0582ae92009-03-13 04:39:26 +0000352 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000353 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000354
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000355 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000356 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000357 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000358 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000359 return 0;
360 }
361};
362
363
364//===---------------------------------------===//
365// 'strcpy' Optimizations
366
Chris Lattner3e8b6632009-09-02 06:11:42 +0000367struct StrCpyOpt : public LibCallOptimization {
Evan Chengeb8c6452010-03-24 20:19:04 +0000368 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
369
370 StrCpyOpt(bool c) : OptChkCall(c) {}
371
Eric Christopher7a61d702008-08-08 19:39:37 +0000372 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000373 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000374 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000375 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000376 if (FT->getNumParams() != NumParams ||
377 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000378 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000379 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000380 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000381
Eric Christopher551754c2010-04-16 23:37:20 +0000382 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000383 if (Dst == Src) // strcpy(x,x) -> x
384 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000385
Dan Gohmanf14d9192009-08-18 00:48:13 +0000386 // These optimizations require TargetData.
387 if (!TD) return 0;
388
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000389 // See if we can get the length of the input string.
390 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000391 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000392
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000393 // We have enough information to now generate the memcpy call to do the
394 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000395 if (OptChkCall)
396 EmitMemCpyChk(Dst, Src,
397 ConstantInt::get(TD->getIntPtrType(*Context), Len),
Eric Christopher551754c2010-04-16 23:37:20 +0000398 CI->getOperand(3), B, TD);
Evan Cheng0289b412010-03-23 15:48:04 +0000399 else
400 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000401 ConstantInt::get(TD->getIntPtrType(*Context), Len),
402 1, false, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000403 return Dst;
404 }
405};
406
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000407//===---------------------------------------===//
408// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000409
Chris Lattner3e8b6632009-09-02 06:11:42 +0000410struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000411 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
412 const FunctionType *FT = Callee->getFunctionType();
413 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
414 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000415 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000416 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000417 return 0;
418
Eric Christopher551754c2010-04-16 23:37:20 +0000419 Value *Dst = CI->getOperand(1);
420 Value *Src = CI->getOperand(2);
421 Value *LenOp = CI->getOperand(3);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000422
423 // See if we can get the length of the input string.
424 uint64_t SrcLen = GetStringLength(Src);
425 if (SrcLen == 0) return 0;
426 --SrcLen;
427
428 if (SrcLen == 0) {
429 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Mon P Wang20adc9d2010-04-04 03:10:48 +0000430 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'),
431 LenOp, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000432 return Dst;
433 }
434
435 uint64_t Len;
436 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
437 Len = LengthArg->getZExtValue();
438 else
439 return 0;
440
441 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
442
Dan Gohmanf14d9192009-08-18 00:48:13 +0000443 // These optimizations require TargetData.
444 if (!TD) return 0;
445
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000446 // Let strncpy handle the zero padding
447 if (Len > SrcLen+1) return 0;
448
449 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000450 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000451 ConstantInt::get(TD->getIntPtrType(*Context), Len),
452 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000453
454 return Dst;
455 }
456};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000457
458//===---------------------------------------===//
459// 'strlen' Optimizations
460
Chris Lattner3e8b6632009-09-02 06:11:42 +0000461struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000462 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000463 const FunctionType *FT = Callee->getFunctionType();
464 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000465 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000466 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000467 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000468
Eric Christopher551754c2010-04-16 23:37:20 +0000469 Value *Src = CI->getOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000470
471 // Constant folding: strlen("xyz") -> 3
472 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000473 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000474
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000475 // strlen(x) != 0 --> *x != 0
476 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000477 if (IsOnlyUsedInZeroEqualityComparison(CI))
478 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
479 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000480 }
481};
482
483//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000484// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000485
Chris Lattner3e8b6632009-09-02 06:11:42 +0000486struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000487 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
488 const FunctionType *FT = Callee->getFunctionType();
489 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000490 !FT->getParamType(0)->isPointerTy() ||
491 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000492 return 0;
493
Eric Christopher551754c2010-04-16 23:37:20 +0000494 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000495 if (isa<ConstantPointerNull>(EndPtr)) {
496 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000497 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000498 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000499
500 return 0;
501 }
502};
503
Chris Lattner24604112009-12-16 09:32:05 +0000504//===---------------------------------------===//
505// 'strstr' Optimizations
506
507struct StrStrOpt : public LibCallOptimization {
508 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
509 const FunctionType *FT = Callee->getFunctionType();
510 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000511 !FT->getParamType(0)->isPointerTy() ||
512 !FT->getParamType(1)->isPointerTy() ||
513 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000514 return 0;
515
516 // fold strstr(x, x) -> x.
Eric Christopher551754c2010-04-16 23:37:20 +0000517 if (CI->getOperand(1) == CI->getOperand(2))
518 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000519
Benjamin Kramer386e9182010-06-15 21:34:25 +0000520 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
521 if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getOperand(1))) {
522 Value *StrLen = EmitStrLen(CI->getOperand(2), B, TD);
523 Value *StrNCmp = EmitStrNCmp(CI->getOperand(1), CI->getOperand(2),
524 StrLen, B, TD);
525 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
526 UI != UE; ) {
527 ICmpInst *Old = cast<ICmpInst>(UI++);
528 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
529 ConstantInt::getNullValue(StrNCmp->getType()),
530 "cmp");
531 Old->replaceAllUsesWith(Cmp);
532 Old->eraseFromParent();
533 }
534 return CI;
535 }
536
Chris Lattner24604112009-12-16 09:32:05 +0000537 // See if either input string is a constant string.
538 std::string SearchStr, ToFindStr;
Eric Christopher551754c2010-04-16 23:37:20 +0000539 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
540 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000541
Chris Lattner24604112009-12-16 09:32:05 +0000542 // fold strstr(x, "") -> x.
543 if (HasStr2 && ToFindStr.empty())
Eric Christopher551754c2010-04-16 23:37:20 +0000544 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000545
Chris Lattner24604112009-12-16 09:32:05 +0000546 // If both strings are known, constant fold it.
547 if (HasStr1 && HasStr2) {
548 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000549
Chris Lattner24604112009-12-16 09:32:05 +0000550 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
551 return Constant::getNullValue(CI->getType());
552
553 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000554 Value *Result = CastToCStr(CI->getOperand(1), B);
Chris Lattner24604112009-12-16 09:32:05 +0000555 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
556 return B.CreateBitCast(Result, CI->getType());
557 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000558
Chris Lattner24604112009-12-16 09:32:05 +0000559 // fold strstr(x, "y") -> strchr(x, 'y').
560 if (HasStr2 && ToFindStr.size() == 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000561 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B, TD),
Chris Lattner24604112009-12-16 09:32:05 +0000562 CI->getType());
563 return 0;
564 }
565};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000566
Nick Lewycky4c498412009-02-13 15:31:46 +0000567
568//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000569// 'memcmp' Optimizations
570
Chris Lattner3e8b6632009-09-02 06:11:42 +0000571struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000572 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000573 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000574 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
575 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000576 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000577 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000578
Eric Christopher551754c2010-04-16 23:37:20 +0000579 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000580
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000581 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000582 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000583
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000584 // Make sure we have a constant length.
Eric Christopher551754c2010-04-16 23:37:20 +0000585 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000586 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000587 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000588
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000589 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000590 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000591
Benjamin Kramer48aefe12010-05-25 22:53:43 +0000592 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
593 if (Len == 1) {
594 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
595 CI->getType(), "lhsv");
596 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
597 CI->getType(), "rhsv");
Benjamin Kramer1464c1d2010-05-26 09:45:04 +0000598 return B.CreateSub(LHSV, RHSV, "chardiff");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000599 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000600
Benjamin Kramer992a6372009-11-05 17:44:22 +0000601 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
602 std::string LHSStr, RHSStr;
603 if (GetConstantStringInfo(LHS, LHSStr) &&
604 GetConstantStringInfo(RHS, RHSStr)) {
605 // Make sure we're not reading out-of-bounds memory.
606 if (Len > LHSStr.length() || Len > RHSStr.length())
607 return 0;
608 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
609 return ConstantInt::get(CI->getType(), Ret);
610 }
611
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000612 return 0;
613 }
614};
615
616//===---------------------------------------===//
617// 'memcpy' Optimizations
618
Chris Lattner3e8b6632009-09-02 06:11:42 +0000619struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000620 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000621 // These optimizations require TargetData.
622 if (!TD) return 0;
623
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000624 const FunctionType *FT = Callee->getFunctionType();
625 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000626 !FT->getParamType(0)->isPointerTy() ||
627 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000628 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000629 return 0;
630
631 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000632 EmitMemCpy(CI->getOperand(1), CI->getOperand(2),
633 CI->getOperand(3), 1, false, B, TD);
634 return CI->getOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000635 }
636};
637
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000638//===---------------------------------------===//
639// 'memmove' Optimizations
640
Chris Lattner3e8b6632009-09-02 06:11:42 +0000641struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000642 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000643 // These optimizations require TargetData.
644 if (!TD) return 0;
645
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000646 const FunctionType *FT = Callee->getFunctionType();
647 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000648 !FT->getParamType(0)->isPointerTy() ||
649 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000650 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000651 return 0;
652
653 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000654 EmitMemMove(CI->getOperand(1), CI->getOperand(2),
655 CI->getOperand(3), 1, false, B, TD);
656 return CI->getOperand(1);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000657 }
658};
659
660//===---------------------------------------===//
661// 'memset' Optimizations
662
Chris Lattner3e8b6632009-09-02 06:11:42 +0000663struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000664 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000665 // These optimizations require TargetData.
666 if (!TD) return 0;
667
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000668 const FunctionType *FT = Callee->getFunctionType();
669 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000670 !FT->getParamType(0)->isPointerTy() ||
671 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000672 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000673 return 0;
674
675 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000676 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
677 false);
678 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), false, B, TD);
679 return CI->getOperand(1);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000680 }
681};
682
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000683//===----------------------------------------------------------------------===//
684// Math Library Optimizations
685//===----------------------------------------------------------------------===//
686
687//===---------------------------------------===//
688// 'pow*' Optimizations
689
Chris Lattner3e8b6632009-09-02 06:11:42 +0000690struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000691 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000692 const FunctionType *FT = Callee->getFunctionType();
693 // Just make sure this has 2 arguments of the same FP type, which match the
694 // result type.
695 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
696 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000697 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000698 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000699
Eric Christopher551754c2010-04-16 23:37:20 +0000700 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000701 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
702 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
703 return Op1C;
704 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000705 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000706 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000707
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000708 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
709 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000710
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000711 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000712 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000713
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000714 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000715 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
716 // This is faster than calling pow, and still handles negative zero
717 // and negative infinite correctly.
718 // TODO: In fast-math mode, this could be just sqrt(x).
719 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000720 Value *Inf = ConstantFP::getInfinity(CI->getType());
721 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000722 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
723 Callee->getAttributes());
724 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
725 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000726 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
727 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
728 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000729 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000730
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000731 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
732 return Op1;
733 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000734 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000735 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000736 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000737 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000738 return 0;
739 }
740};
741
742//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000743// 'exp2' Optimizations
744
Chris Lattner3e8b6632009-09-02 06:11:42 +0000745struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000746 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000747 const FunctionType *FT = Callee->getFunctionType();
748 // Just make sure this has 1 argument of FP type, which matches the
749 // result type.
750 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000751 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000752 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000753
Eric Christopher551754c2010-04-16 23:37:20 +0000754 Value *Op = CI->getOperand(1);
Chris Lattnere818f772008-05-02 18:43:35 +0000755 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
756 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
757 Value *LdExpArg = 0;
758 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
759 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000760 LdExpArg = B.CreateSExt(OpC->getOperand(0),
761 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000762 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
763 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000764 LdExpArg = B.CreateZExt(OpC->getOperand(0),
765 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000766 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000767
Chris Lattnere818f772008-05-02 18:43:35 +0000768 if (LdExpArg) {
769 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000770 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000771 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000772 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000773 Name = "ldexp";
774 else
775 Name = "ldexpl";
776
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000777 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000778 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000779 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000780
781 Module *M = Caller->getParent();
782 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000783 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000784 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000785 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
786 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
787 CI->setCallingConv(F->getCallingConv());
788
789 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000790 }
791 return 0;
792 }
793};
Chris Lattnere818f772008-05-02 18:43:35 +0000794
795//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000796// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
797
Chris Lattner3e8b6632009-09-02 06:11:42 +0000798struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000799 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000800 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000801 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
802 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000803 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000804
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000805 // If this is something like 'floor((double)floatval)', convert to floorf.
Eric Christopher551754c2010-04-16 23:37:20 +0000806 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000807 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000808 return 0;
809
810 // floor((double)floatval) -> (double)floorf(floatval)
811 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000812 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
813 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000814 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000815 }
816};
817
818//===----------------------------------------------------------------------===//
819// Integer Optimizations
820//===----------------------------------------------------------------------===//
821
822//===---------------------------------------===//
823// 'ffs*' Optimizations
824
Chris Lattner3e8b6632009-09-02 06:11:42 +0000825struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000826 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000827 const FunctionType *FT = Callee->getFunctionType();
828 // Just make sure this has 2 arguments of the same FP type, which match the
829 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000830 if (FT->getNumParams() != 1 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000831 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000832 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000833 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000834
Eric Christopher551754c2010-04-16 23:37:20 +0000835 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000836
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000837 // Constant fold.
838 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
839 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000840 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000841 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000842 CI->getValue().countTrailingZeros()+1);
843 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000844
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000845 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
846 const Type *ArgType = Op->getType();
847 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
848 Intrinsic::cttz, &ArgType, 1);
849 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000850 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000851 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000852
Owen Andersona7235ea2009-07-31 20:28:14 +0000853 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000854 return B.CreateSelect(Cond, V,
855 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000856 }
857};
858
859//===---------------------------------------===//
860// 'isdigit' Optimizations
861
Chris Lattner3e8b6632009-09-02 06:11:42 +0000862struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000863 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000864 const FunctionType *FT = Callee->getFunctionType();
865 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000866 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000867 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000868 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000869
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000870 // isdigit(c) -> (c-'0') <u 10
Eric Christopher551754c2010-04-16 23:37:20 +0000871 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000872 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000873 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000874 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000875 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000876 return B.CreateZExt(Op, CI->getType());
877 }
878};
879
880//===---------------------------------------===//
881// 'isascii' Optimizations
882
Chris Lattner3e8b6632009-09-02 06:11:42 +0000883struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000884 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000885 const FunctionType *FT = Callee->getFunctionType();
886 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000887 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000888 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000889 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000890
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000891 // isascii(c) -> c <u 128
Eric Christopher551754c2010-04-16 23:37:20 +0000892 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000893 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000894 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000895 return B.CreateZExt(Op, CI->getType());
896 }
897};
Eric Christopher37c8b862009-10-07 21:14:25 +0000898
Chris Lattner313f0e62008-06-09 08:26:51 +0000899//===---------------------------------------===//
900// 'abs', 'labs', 'llabs' Optimizations
901
Chris Lattner3e8b6632009-09-02 06:11:42 +0000902struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000903 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +0000904 const FunctionType *FT = Callee->getFunctionType();
905 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +0000906 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +0000907 FT->getParamType(0) != FT->getReturnType())
908 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000909
Chris Lattner313f0e62008-06-09 08:26:51 +0000910 // abs(x) -> x >s -1 ? x : -x
Eric Christopher551754c2010-04-16 23:37:20 +0000911 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000912 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +0000913 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +0000914 "ispos");
915 Value *Neg = B.CreateNeg(Op, "neg");
916 return B.CreateSelect(Pos, Op, Neg);
917 }
918};
Eric Christopher37c8b862009-10-07 21:14:25 +0000919
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000920
921//===---------------------------------------===//
922// 'toascii' Optimizations
923
Chris Lattner3e8b6632009-09-02 06:11:42 +0000924struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000925 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000926 const FunctionType *FT = Callee->getFunctionType();
927 // We require i32(i32)
928 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000929 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000930 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000931
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000932 // isascii(c) -> c & 0x7f
Eric Christopher551754c2010-04-16 23:37:20 +0000933 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +0000934 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000935 }
936};
937
938//===----------------------------------------------------------------------===//
939// Formatting and IO Optimizations
940//===----------------------------------------------------------------------===//
941
942//===---------------------------------------===//
943// 'printf' Optimizations
944
Chris Lattner3e8b6632009-09-02 06:11:42 +0000945struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000946 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000947 // Require one fixed pointer argument and an integer/void result.
948 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000949 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
950 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000951 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000952 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000953
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000954 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000955 std::string FormatStr;
Eric Christopher551754c2010-04-16 23:37:20 +0000956 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +0000957 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000958
959 // Empty format string -> noop.
960 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +0000961 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000962 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000963
Chris Lattner74965f22009-11-09 04:57:04 +0000964 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
965 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000966 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +0000967 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000968 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +0000969 if (CI->use_empty()) return CI;
970 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000971 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000972
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000973 // printf("foo\n") --> puts("foo")
974 if (FormatStr[FormatStr.size()-1] == '\n' &&
975 FormatStr.find('%') == std::string::npos) { // no format characters.
976 // Create a string literal with no \n on it. We expect the constant merge
977 // pass to be run after this pass, to merge duplicate strings.
978 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000979 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +0000980 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
981 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +0000982 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000983 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000984 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000985 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000986
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000987 // Optimize specific format strings.
Eric Christopher551754c2010-04-16 23:37:20 +0000988 // printf("%c", chr) --> putchar(*(i8*)dst)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000989 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
Eric Christopher551754c2010-04-16 23:37:20 +0000990 CI->getOperand(2)->getType()->isIntegerTy()) {
991 Value *Res = EmitPutChar(CI->getOperand(2), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000992
Chris Lattner74965f22009-11-09 04:57:04 +0000993 if (CI->use_empty()) return CI;
994 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000995 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000996
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000997 // printf("%s\n", str) --> puts(str)
998 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
Eric Christopher551754c2010-04-16 23:37:20 +0000999 CI->getOperand(2)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001000 CI->use_empty()) {
Eric Christopher551754c2010-04-16 23:37:20 +00001001 EmitPutS(CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001002 return CI;
1003 }
1004 return 0;
1005 }
1006};
1007
1008//===---------------------------------------===//
1009// 'sprintf' Optimizations
1010
Chris Lattner3e8b6632009-09-02 06:11:42 +00001011struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001012 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001013 // Require two fixed pointer arguments and an integer result.
1014 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001015 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1016 !FT->getParamType(1)->isPointerTy() ||
1017 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001018 return 0;
1019
1020 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001021 std::string FormatStr;
Eric Christopher551754c2010-04-16 23:37:20 +00001022 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001023 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001024
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001025 // If we just have a format string (nothing else crazy) transform it.
1026 if (CI->getNumOperands() == 3) {
1027 // Make sure there's no % in the constant array. We could try to handle
1028 // %% -> % in the future if we cared.
1029 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1030 if (FormatStr[i] == '%')
1031 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001032
1033 // These optimizations require TargetData.
1034 if (!TD) return 0;
1035
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001036 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
Eric Christopher551754c2010-04-16 23:37:20 +00001037 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Eric Christopherb6174e32010-03-05 22:25:30 +00001038 ConstantInt::get(TD->getIntPtrType(*Context),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001039 FormatStr.size()+1), 1, false, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001040 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001041 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001042
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001043 // The remaining optimizations require the format string to be "%s" or "%c"
1044 // and have an extra operand.
1045 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1046 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001047
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001048 // Decode the second character of the format string.
1049 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001050 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Eric Christopher551754c2010-04-16 23:37:20 +00001051 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
1052 Value *V = B.CreateTrunc(CI->getOperand(3),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001053 Type::getInt8Ty(*Context), "char");
Eric Christopher551754c2010-04-16 23:37:20 +00001054 Value *Ptr = CastToCStr(CI->getOperand(1), B);
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001055 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001056 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001057 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001058 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001059
Owen Andersoneed707b2009-07-24 23:12:02 +00001060 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001061 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001062
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001063 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001064 // These optimizations require TargetData.
1065 if (!TD) return 0;
1066
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001067 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Eric Christopher551754c2010-04-16 23:37:20 +00001068 if (!CI->getOperand(3)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001069
Eric Christopher551754c2010-04-16 23:37:20 +00001070 Value *Len = EmitStrLen(CI->getOperand(3), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001071 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001072 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001073 "leninc");
Eric Christopher551754c2010-04-16 23:37:20 +00001074 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, false, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001075
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001076 // The sprintf result is the unincremented number of bytes in the string.
1077 return B.CreateIntCast(Len, CI->getType(), false);
1078 }
1079 return 0;
1080 }
1081};
1082
1083//===---------------------------------------===//
1084// 'fwrite' Optimizations
1085
Chris Lattner3e8b6632009-09-02 06:11:42 +00001086struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001087 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001088 // Require a pointer, an integer, an integer, a pointer, returning integer.
1089 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001090 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1091 !FT->getParamType(1)->isIntegerTy() ||
1092 !FT->getParamType(2)->isIntegerTy() ||
1093 !FT->getParamType(3)->isPointerTy() ||
1094 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001095 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001096
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001097 // Get the element size and count.
Eric Christopher551754c2010-04-16 23:37:20 +00001098 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1099 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001100 if (!SizeC || !CountC) return 0;
1101 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001102
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001103 // If this is writing zero records, remove the call (it's a noop).
1104 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001105 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001106
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001107 // If this is writing one byte, turn it into fputc.
1108 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Eric Christopher551754c2010-04-16 23:37:20 +00001109 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1110 EmitFPutC(Char, CI->getOperand(4), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001111 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001112 }
1113
1114 return 0;
1115 }
1116};
1117
1118//===---------------------------------------===//
1119// 'fputs' Optimizations
1120
Chris Lattner3e8b6632009-09-02 06:11:42 +00001121struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001122 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001123 // These optimizations require TargetData.
1124 if (!TD) return 0;
1125
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001126 // Require two pointers. Also, we can't optimize if return value is used.
1127 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001128 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1129 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001130 !CI->use_empty())
1131 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001132
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001133 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
Eric Christopher551754c2010-04-16 23:37:20 +00001134 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001135 if (!Len) return 0;
Eric Christopher551754c2010-04-16 23:37:20 +00001136 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001137 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Eric Christopher551754c2010-04-16 23:37:20 +00001138 CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001139 return CI; // Known to have no uses (see above).
1140 }
1141};
1142
1143//===---------------------------------------===//
1144// 'fprintf' Optimizations
1145
Chris Lattner3e8b6632009-09-02 06:11:42 +00001146struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001147 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001148 // Require two fixed paramters as pointers and integer result.
1149 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001150 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1151 !FT->getParamType(1)->isPointerTy() ||
1152 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001153 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001154
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001155 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001156 std::string FormatStr;
Eric Christopher551754c2010-04-16 23:37:20 +00001157 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001158 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001159
1160 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1161 if (CI->getNumOperands() == 3) {
1162 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1163 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001164 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001165
1166 // These optimizations require TargetData.
1167 if (!TD) return 0;
1168
Eric Christopher551754c2010-04-16 23:37:20 +00001169 EmitFWrite(CI->getOperand(2),
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001170 ConstantInt::get(TD->getIntPtrType(*Context),
1171 FormatStr.size()),
Eric Christopher551754c2010-04-16 23:37:20 +00001172 CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001173 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001174 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001175
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001176 // The remaining optimizations require the format string to be "%s" or "%c"
1177 // and have an extra operand.
1178 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1179 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001180
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001181 // Decode the second character of the format string.
1182 if (FormatStr[1] == 'c') {
Eric Christopher551754c2010-04-16 23:37:20 +00001183 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1184 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
1185 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001186 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001187 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001188
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001189 if (FormatStr[1] == 's') {
Eric Christopher551754c2010-04-16 23:37:20 +00001190 // fprintf(F, "%s", str) -> fputs(str, F)
1191 if (!CI->getOperand(3)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001192 return 0;
Eric Christopher551754c2010-04-16 23:37:20 +00001193 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001194 return CI;
1195 }
1196 return 0;
1197 }
1198};
1199
Bill Wendlingac178222008-05-05 21:37:59 +00001200} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001201
1202//===----------------------------------------------------------------------===//
1203// SimplifyLibCalls Pass Implementation
1204//===----------------------------------------------------------------------===//
1205
1206namespace {
1207 /// This pass optimizes well known library functions from libc and libm.
1208 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001209 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001210 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001211 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001212 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
Evan Cheng0289b412010-03-23 15:48:04 +00001213 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
1214 StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001215 StrToOpt StrTo; StrStrOpt StrStr;
1216 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001217 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001218 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001219 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001220 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1221 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001222 // Formatting and IO Optimizations
1223 SPrintFOpt SPrintF; PrintFOpt PrintF;
1224 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001225
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001226 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001227 public:
1228 static char ID; // Pass identification
Evan Chengeb8c6452010-03-24 20:19:04 +00001229 SimplifyLibCalls() : FunctionPass(&ID), StrCpy(false), StrCpyChk(true) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001230 void InitOptimizations();
1231 bool runOnFunction(Function &F);
1232
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001233 void setDoesNotAccessMemory(Function &F);
1234 void setOnlyReadsMemory(Function &F);
1235 void setDoesNotThrow(Function &F);
1236 void setDoesNotCapture(Function &F, unsigned n);
1237 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001238 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001239
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001240 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001241 }
1242 };
1243 char SimplifyLibCalls::ID = 0;
1244} // end anonymous namespace.
1245
1246static RegisterPass<SimplifyLibCalls>
1247X("simplify-libcalls", "Simplify well-known library calls");
1248
1249// Public interface to the Simplify LibCalls pass.
1250FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001251 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001252}
1253
1254/// Optimizations - Populate the Optimizations map with all the optimizations
1255/// we know.
1256void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001257 // String and Memory LibCall Optimizations
1258 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001259 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001260 Optimizations["strchr"] = &StrChr;
1261 Optimizations["strcmp"] = &StrCmp;
1262 Optimizations["strncmp"] = &StrNCmp;
1263 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001264 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001265 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001266 Optimizations["strtol"] = &StrTo;
1267 Optimizations["strtod"] = &StrTo;
1268 Optimizations["strtof"] = &StrTo;
1269 Optimizations["strtoul"] = &StrTo;
1270 Optimizations["strtoll"] = &StrTo;
1271 Optimizations["strtold"] = &StrTo;
1272 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001273 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001274 Optimizations["memcmp"] = &MemCmp;
1275 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001276 Optimizations["memmove"] = &MemMove;
1277 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001278
Evan Cheng0289b412010-03-23 15:48:04 +00001279 // _chk variants of String and Memory LibCall Optimizations.
Evan Cheng0289b412010-03-23 15:48:04 +00001280 Optimizations["__strcpy_chk"] = &StrCpyChk;
1281
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001282 // Math Library Optimizations
1283 Optimizations["powf"] = &Pow;
1284 Optimizations["pow"] = &Pow;
1285 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001286 Optimizations["llvm.pow.f32"] = &Pow;
1287 Optimizations["llvm.pow.f64"] = &Pow;
1288 Optimizations["llvm.pow.f80"] = &Pow;
1289 Optimizations["llvm.pow.f128"] = &Pow;
1290 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001291 Optimizations["exp2l"] = &Exp2;
1292 Optimizations["exp2"] = &Exp2;
1293 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001294 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1295 Optimizations["llvm.exp2.f128"] = &Exp2;
1296 Optimizations["llvm.exp2.f80"] = &Exp2;
1297 Optimizations["llvm.exp2.f64"] = &Exp2;
1298 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001299
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001300#ifdef HAVE_FLOORF
1301 Optimizations["floor"] = &UnaryDoubleFP;
1302#endif
1303#ifdef HAVE_CEILF
1304 Optimizations["ceil"] = &UnaryDoubleFP;
1305#endif
1306#ifdef HAVE_ROUNDF
1307 Optimizations["round"] = &UnaryDoubleFP;
1308#endif
1309#ifdef HAVE_RINTF
1310 Optimizations["rint"] = &UnaryDoubleFP;
1311#endif
1312#ifdef HAVE_NEARBYINTF
1313 Optimizations["nearbyint"] = &UnaryDoubleFP;
1314#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001315
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001316 // Integer Optimizations
1317 Optimizations["ffs"] = &FFS;
1318 Optimizations["ffsl"] = &FFS;
1319 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001320 Optimizations["abs"] = &Abs;
1321 Optimizations["labs"] = &Abs;
1322 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001323 Optimizations["isdigit"] = &IsDigit;
1324 Optimizations["isascii"] = &IsAscii;
1325 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001326
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001327 // Formatting and IO Optimizations
1328 Optimizations["sprintf"] = &SPrintF;
1329 Optimizations["printf"] = &PrintF;
1330 Optimizations["fwrite"] = &FWrite;
1331 Optimizations["fputs"] = &FPuts;
1332 Optimizations["fprintf"] = &FPrintF;
1333}
1334
1335
1336/// runOnFunction - Top level algorithm.
1337///
1338bool SimplifyLibCalls::runOnFunction(Function &F) {
1339 if (Optimizations.empty())
1340 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001341
Dan Gohmanf14d9192009-08-18 00:48:13 +00001342 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001343
Owen Andersone922c022009-07-22 00:24:57 +00001344 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001345
1346 bool Changed = false;
1347 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1348 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1349 // Ignore non-calls.
1350 CallInst *CI = dyn_cast<CallInst>(I++);
1351 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001352
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001353 // Ignore indirect calls and calls to non-external functions.
1354 Function *Callee = CI->getCalledFunction();
1355 if (Callee == 0 || !Callee->isDeclaration() ||
1356 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1357 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001358
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001359 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001360 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1361 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001362
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001363 // Set the builder to the instruction after the call.
1364 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001365
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001366 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001367 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001368 if (Result == 0) continue;
1369
David Greene6a6b90e2010-01-05 01:27:21 +00001370 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1371 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001372
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001373 // Something changed!
1374 Changed = true;
1375 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001376
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001377 // Inspect the instruction after the call (which was potentially just
1378 // added) next.
1379 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001380
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001381 if (CI != Result && !CI->use_empty()) {
1382 CI->replaceAllUsesWith(Result);
1383 if (!Result->hasName())
1384 Result->takeName(CI);
1385 }
1386 CI->eraseFromParent();
1387 }
1388 }
1389 return Changed;
1390}
1391
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001392// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001393
1394void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1395 if (!F.doesNotAccessMemory()) {
1396 F.setDoesNotAccessMemory();
1397 ++NumAnnotated;
1398 Modified = true;
1399 }
1400}
1401void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1402 if (!F.onlyReadsMemory()) {
1403 F.setOnlyReadsMemory();
1404 ++NumAnnotated;
1405 Modified = true;
1406 }
1407}
1408void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1409 if (!F.doesNotThrow()) {
1410 F.setDoesNotThrow();
1411 ++NumAnnotated;
1412 Modified = true;
1413 }
1414}
1415void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1416 if (!F.doesNotCapture(n)) {
1417 F.setDoesNotCapture(n);
1418 ++NumAnnotated;
1419 Modified = true;
1420 }
1421}
1422void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1423 if (!F.doesNotAlias(n)) {
1424 F.setDoesNotAlias(n);
1425 ++NumAnnotated;
1426 Modified = true;
1427 }
1428}
1429
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001430/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001431///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001432bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001433 Modified = false;
1434 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1435 Function &F = *I;
1436 if (!F.isDeclaration())
1437 continue;
1438
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001439 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001440 continue;
1441
1442 const FunctionType *FTy = F.getFunctionType();
1443
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001444 StringRef Name = F.getName();
1445 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001446 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001447 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001448 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001449 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001450 continue;
1451 setOnlyReadsMemory(F);
1452 setDoesNotThrow(F);
1453 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001454 } else if (Name == "strchr" ||
1455 Name == "strrchr") {
1456 if (FTy->getNumParams() != 2 ||
1457 !FTy->getParamType(0)->isPointerTy() ||
1458 !FTy->getParamType(1)->isIntegerTy())
1459 continue;
1460 setOnlyReadsMemory(F);
1461 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001462 } else if (Name == "strcpy" ||
1463 Name == "stpcpy" ||
1464 Name == "strcat" ||
1465 Name == "strtol" ||
1466 Name == "strtod" ||
1467 Name == "strtof" ||
1468 Name == "strtoul" ||
1469 Name == "strtoll" ||
1470 Name == "strtold" ||
1471 Name == "strncat" ||
1472 Name == "strncpy" ||
1473 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001474 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001475 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001476 continue;
1477 setDoesNotThrow(F);
1478 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001479 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001480 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001481 !FTy->getParamType(0)->isPointerTy() ||
1482 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001483 continue;
1484 setDoesNotThrow(F);
1485 setDoesNotCapture(F, 1);
1486 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001487 } else if (Name == "strcmp" ||
1488 Name == "strspn" ||
1489 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001490 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001491 Name == "strcoll" ||
1492 Name == "strcasecmp" ||
1493 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001494 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001495 !FTy->getParamType(0)->isPointerTy() ||
1496 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001497 continue;
1498 setOnlyReadsMemory(F);
1499 setDoesNotThrow(F);
1500 setDoesNotCapture(F, 1);
1501 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001502 } else if (Name == "strstr" ||
1503 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001504 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001505 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001506 continue;
1507 setOnlyReadsMemory(F);
1508 setDoesNotThrow(F);
1509 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001510 } else if (Name == "strtok" ||
1511 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001512 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001513 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001514 continue;
1515 setDoesNotThrow(F);
1516 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001517 } else if (Name == "scanf" ||
1518 Name == "setbuf" ||
1519 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001520 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001521 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001522 continue;
1523 setDoesNotThrow(F);
1524 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001525 } else if (Name == "strdup" ||
1526 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001527 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001528 !FTy->getReturnType()->isPointerTy() ||
1529 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001530 continue;
1531 setDoesNotThrow(F);
1532 setDoesNotAlias(F, 0);
1533 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001534 } else if (Name == "stat" ||
1535 Name == "sscanf" ||
1536 Name == "sprintf" ||
1537 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001538 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001539 !FTy->getParamType(0)->isPointerTy() ||
1540 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001541 continue;
1542 setDoesNotThrow(F);
1543 setDoesNotCapture(F, 1);
1544 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001545 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001546 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001547 !FTy->getParamType(0)->isPointerTy() ||
1548 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001549 continue;
1550 setDoesNotThrow(F);
1551 setDoesNotCapture(F, 1);
1552 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001553 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001554 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001555 !FTy->getParamType(1)->isPointerTy() ||
1556 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001557 continue;
1558 setDoesNotThrow(F);
1559 setDoesNotCapture(F, 2);
1560 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001561 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001562 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001563 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001564 continue;
1565 // May throw; "system" is a valid pthread cancellation point.
1566 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001567 }
1568 break;
1569 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001570 if (Name == "malloc") {
1571 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001572 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001573 continue;
1574 setDoesNotThrow(F);
1575 setDoesNotAlias(F, 0);
1576 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001577 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001578 !FTy->getParamType(0)->isPointerTy() ||
1579 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001580 continue;
1581 setOnlyReadsMemory(F);
1582 setDoesNotThrow(F);
1583 setDoesNotCapture(F, 1);
1584 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001585 } else if (Name == "memchr" ||
1586 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001587 if (FTy->getNumParams() != 3)
1588 continue;
1589 setOnlyReadsMemory(F);
1590 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001591 } else if (Name == "modf" ||
1592 Name == "modff" ||
1593 Name == "modfl" ||
1594 Name == "memcpy" ||
1595 Name == "memccpy" ||
1596 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001597 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001598 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001599 continue;
1600 setDoesNotThrow(F);
1601 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001602 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001603 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001604 continue;
1605 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001606 } else if (Name == "mkdir" ||
1607 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001608 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001609 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001610 continue;
1611 setDoesNotThrow(F);
1612 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001613 }
1614 break;
1615 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001616 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001617 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001618 !FTy->getParamType(0)->isPointerTy() ||
1619 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001620 continue;
1621 setDoesNotThrow(F);
1622 setDoesNotAlias(F, 0);
1623 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001624 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001625 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001626 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001627 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001628 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001629 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001630 } else if (Name == "rmdir" ||
1631 Name == "rewind" ||
1632 Name == "remove" ||
1633 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001634 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001635 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001636 continue;
1637 setDoesNotThrow(F);
1638 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001639 } else if (Name == "rename" ||
1640 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001641 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001642 !FTy->getParamType(0)->isPointerTy() ||
1643 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001644 continue;
1645 setDoesNotThrow(F);
1646 setDoesNotCapture(F, 1);
1647 setDoesNotCapture(F, 2);
1648 }
1649 break;
1650 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001651 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001652 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001653 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001654 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001655 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001656 setDoesNotCapture(F, 2);
1657 }
1658 break;
1659 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001660 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001661 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001662 !FTy->getParamType(0)->isPointerTy() ||
1663 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001664 continue;
1665 setDoesNotThrow(F);
1666 setDoesNotCapture(F, 1);
1667 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001668 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001669 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001670 !FTy->getParamType(0)->isPointerTy() ||
1671 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001672 continue;
1673 setDoesNotThrow(F);
1674 setOnlyReadsMemory(F);
1675 setDoesNotCapture(F, 1);
1676 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001677 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001678 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001679 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001680 continue;
1681 setDoesNotThrow(F);
1682 setDoesNotCapture(F, 1);
1683 }
1684 break;
1685 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001686 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001687 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001688 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001689 continue;
1690 setDoesNotThrow(F);
1691 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001692 } else if (Name == "chmod" ||
1693 Name == "chown" ||
1694 Name == "ctermid" ||
1695 Name == "clearerr" ||
1696 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001697 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001698 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001699 continue;
1700 setDoesNotThrow(F);
1701 setDoesNotCapture(F, 1);
1702 }
1703 break;
1704 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001705 if (Name == "atoi" ||
1706 Name == "atol" ||
1707 Name == "atof" ||
1708 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001709 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001710 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001711 continue;
1712 setDoesNotThrow(F);
1713 setOnlyReadsMemory(F);
1714 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001715 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001716 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001717 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001718 continue;
1719 setDoesNotThrow(F);
1720 setDoesNotCapture(F, 1);
1721 }
1722 break;
1723 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001724 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001725 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001726 !FTy->getReturnType()->isPointerTy() ||
1727 !FTy->getParamType(0)->isPointerTy() ||
1728 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001729 continue;
1730 setDoesNotThrow(F);
1731 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001732 setDoesNotCapture(F, 1);
1733 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001734 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001735 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001736 !FTy->getReturnType()->isPointerTy() ||
1737 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001738 continue;
1739 setDoesNotThrow(F);
1740 setDoesNotAlias(F, 0);
1741 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001742 } else if (Name == "feof" ||
1743 Name == "free" ||
1744 Name == "fseek" ||
1745 Name == "ftell" ||
1746 Name == "fgetc" ||
1747 Name == "fseeko" ||
1748 Name == "ftello" ||
1749 Name == "fileno" ||
1750 Name == "fflush" ||
1751 Name == "fclose" ||
1752 Name == "fsetpos" ||
1753 Name == "flockfile" ||
1754 Name == "funlockfile" ||
1755 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001756 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001757 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001758 continue;
1759 setDoesNotThrow(F);
1760 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001761 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001762 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001763 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001764 continue;
1765 setDoesNotThrow(F);
1766 setDoesNotCapture(F, 1);
1767 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001768 } else if (Name == "fputc" ||
1769 Name == "fstat" ||
1770 Name == "frexp" ||
1771 Name == "frexpf" ||
1772 Name == "frexpl" ||
1773 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001774 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001775 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001776 continue;
1777 setDoesNotThrow(F);
1778 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001779 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001780 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001781 !FTy->getParamType(0)->isPointerTy() ||
1782 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001783 continue;
1784 setDoesNotThrow(F);
1785 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001786 } else if (Name == "fread" ||
1787 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001788 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001789 !FTy->getParamType(0)->isPointerTy() ||
1790 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001791 continue;
1792 setDoesNotThrow(F);
1793 setDoesNotCapture(F, 1);
1794 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001795 } else if (Name == "fputs" ||
1796 Name == "fscanf" ||
1797 Name == "fprintf" ||
1798 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001799 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001800 !FTy->getParamType(0)->isPointerTy() ||
1801 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001802 continue;
1803 setDoesNotThrow(F);
1804 setDoesNotCapture(F, 1);
1805 setDoesNotCapture(F, 2);
1806 }
1807 break;
1808 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001809 if (Name == "getc" ||
1810 Name == "getlogin_r" ||
1811 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001812 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001813 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001814 continue;
1815 setDoesNotThrow(F);
1816 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001817 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001818 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001819 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001820 continue;
1821 setDoesNotThrow(F);
1822 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001823 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001824 } else if (Name == "gets" ||
1825 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001826 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001827 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001828 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001829 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001830 continue;
1831 setDoesNotThrow(F);
1832 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001833 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001834 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001835 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001836 continue;
1837 setDoesNotThrow(F);
1838 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001839 }
1840 break;
1841 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001842 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001843 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001844 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001845 continue;
1846 setDoesNotThrow(F);
1847 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001848 } else if (Name == "uname" ||
1849 Name == "unlink" ||
1850 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001851 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001852 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001853 continue;
1854 setDoesNotThrow(F);
1855 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001856 } else if (Name == "utime" ||
1857 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001858 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001859 !FTy->getParamType(0)->isPointerTy() ||
1860 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001861 continue;
1862 setDoesNotThrow(F);
1863 setDoesNotCapture(F, 1);
1864 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001865 }
1866 break;
1867 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001868 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001869 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001870 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001871 continue;
1872 setDoesNotThrow(F);
1873 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001874 } else if (Name == "puts" ||
1875 Name == "printf" ||
1876 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001877 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001878 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001879 continue;
1880 setDoesNotThrow(F);
1881 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001882 } else if (Name == "pread" ||
1883 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001884 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001885 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001886 continue;
1887 // May throw; these are valid pthread cancellation points.
1888 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001889 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001890 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001891 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001892 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001893 !FTy->getReturnType()->isPointerTy() ||
1894 !FTy->getParamType(0)->isPointerTy() ||
1895 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001896 continue;
1897 setDoesNotThrow(F);
1898 setDoesNotAlias(F, 0);
1899 setDoesNotCapture(F, 1);
1900 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001901 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001902 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001903 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001904 continue;
1905 setDoesNotThrow(F);
1906 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001907 }
1908 break;
1909 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001910 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001911 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001912 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001913 continue;
1914 setDoesNotThrow(F);
1915 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001916 } else if (Name == "vsscanf" ||
1917 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001918 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001919 !FTy->getParamType(1)->isPointerTy() ||
1920 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001921 continue;
1922 setDoesNotThrow(F);
1923 setDoesNotCapture(F, 1);
1924 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001925 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00001926 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001927 continue;
1928 setDoesNotThrow(F);
1929 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001930 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001931 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001932 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001933 continue;
1934 setDoesNotThrow(F);
1935 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001936 } else if (Name == "vfprintf" ||
1937 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001938 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001939 !FTy->getParamType(0)->isPointerTy() ||
1940 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001941 continue;
1942 setDoesNotThrow(F);
1943 setDoesNotCapture(F, 1);
1944 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001945 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001946 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001947 !FTy->getParamType(0)->isPointerTy() ||
1948 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001949 continue;
1950 setDoesNotThrow(F);
1951 setDoesNotCapture(F, 1);
1952 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001953 }
1954 break;
1955 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001956 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001957 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001958 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001959 continue;
1960 // May throw; "open" is a valid pthread cancellation point.
1961 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001962 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001963 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001964 !FTy->getReturnType()->isPointerTy() ||
1965 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001966 continue;
1967 setDoesNotThrow(F);
1968 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00001969 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001970 }
1971 break;
1972 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001973 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00001974 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001975 continue;
1976 setDoesNotThrow(F);
1977 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001978 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001979 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001980 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001981 continue;
1982 setDoesNotThrow(F);
1983 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001984 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001985 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001986 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001987 if (Name == "htonl" ||
1988 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001989 setDoesNotThrow(F);
1990 setDoesNotAccessMemory(F);
1991 }
1992 break;
1993 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001994 if (Name == "ntohl" ||
1995 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001996 setDoesNotThrow(F);
1997 setDoesNotAccessMemory(F);
1998 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001999 break;
2000 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002001 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002002 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002003 !FTy->getParamType(0)->isPointerTy() ||
2004 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002005 continue;
2006 setDoesNotThrow(F);
2007 setDoesNotCapture(F, 1);
2008 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002009 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002010 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002011 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002012 continue;
2013 setDoesNotThrow(F);
2014 setDoesNotCapture(F, 1);
2015 }
2016 break;
2017 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002018 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002019 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002020 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002021 continue;
2022 // May throw; places call through function pointer.
2023 setDoesNotCapture(F, 4);
2024 }
2025 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002026 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002027 if (Name == "__strdup" ||
2028 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002029 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002030 !FTy->getReturnType()->isPointerTy() ||
2031 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002032 continue;
2033 setDoesNotThrow(F);
2034 setDoesNotAlias(F, 0);
2035 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002036 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002037 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002038 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002039 continue;
2040 setDoesNotThrow(F);
2041 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002042 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002043 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002044 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002045 continue;
2046 setDoesNotThrow(F);
2047 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002048 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002049 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002050 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002051 continue;
2052 setDoesNotThrow(F);
2053 setDoesNotCapture(F, 2);
2054 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002055 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002056 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002057 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002058 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002059 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002060 continue;
2061 setDoesNotThrow(F);
2062 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002063 } else if (Name == "\1stat64" ||
2064 Name == "\1lstat64" ||
2065 Name == "\1statvfs64" ||
2066 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002067 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002068 !FTy->getParamType(0)->isPointerTy() ||
2069 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002070 continue;
2071 setDoesNotThrow(F);
2072 setDoesNotCapture(F, 1);
2073 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002074 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002075 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002076 !FTy->getReturnType()->isPointerTy() ||
2077 !FTy->getParamType(0)->isPointerTy() ||
2078 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002079 continue;
2080 setDoesNotThrow(F);
2081 setDoesNotAlias(F, 0);
2082 setDoesNotCapture(F, 1);
2083 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002084 } else if (Name == "\1fseeko64" ||
2085 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002086 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002087 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002088 continue;
2089 setDoesNotThrow(F);
2090 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002091 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002092 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002093 continue;
2094 setDoesNotThrow(F);
2095 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002096 } else if (Name == "\1fstat64" ||
2097 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002098 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002099 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002100 continue;
2101 setDoesNotThrow(F);
2102 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002103 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002104 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002105 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002106 continue;
2107 // May throw; "open" is a valid pthread cancellation point.
2108 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002109 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002110 break;
2111 }
2112 }
2113 return Modified;
2114}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002115
2116// TODO:
2117// Additional cases that we need to add to this file:
2118//
2119// cbrt:
2120// * cbrt(expN(X)) -> expN(x/3)
2121// * cbrt(sqrt(x)) -> pow(x,1/6)
2122// * cbrt(sqrt(x)) -> pow(x,1/9)
2123//
2124// cos, cosf, cosl:
2125// * cos(-x) -> cos(x)
2126//
2127// exp, expf, expl:
2128// * exp(log(x)) -> x
2129//
2130// log, logf, logl:
2131// * log(exp(x)) -> x
2132// * log(x**y) -> y*log(x)
2133// * log(exp(y)) -> y*log(e)
2134// * log(exp2(y)) -> y*log(2)
2135// * log(exp10(y)) -> y*log(10)
2136// * log(sqrt(x)) -> 0.5*log(x)
2137// * log(pow(x,y)) -> y*log(x)
2138//
2139// lround, lroundf, lroundl:
2140// * lround(cnst) -> cnst'
2141//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002142// pow, powf, powl:
2143// * pow(exp(x),y) -> exp(x*y)
2144// * pow(sqrt(x),y) -> pow(x,y*0.5)
2145// * pow(pow(x,y),z)-> pow(x,y*z)
2146//
2147// puts:
2148// * puts("") -> putchar("\n")
2149//
2150// round, roundf, roundl:
2151// * round(cnst) -> cnst'
2152//
2153// signbit:
2154// * signbit(cnst) -> cnst'
2155// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2156//
2157// sqrt, sqrtf, sqrtl:
2158// * sqrt(expN(x)) -> expN(x*0.5)
2159// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2160// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2161//
2162// stpcpy:
2163// * stpcpy(str, "literal") ->
2164// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2165// strrchr:
2166// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2167// (if c is a constant integer and s is a constant string)
2168// * strrchr(s1,0) -> strchr(s1,0)
2169//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002170// strpbrk:
2171// * strpbrk(s,a) -> offset_in_for(s,a)
2172// (if s and a are both constant strings)
2173// * strpbrk(s,"") -> 0
2174// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2175//
2176// strspn, strcspn:
2177// * strspn(s,a) -> const_int (if both args are constant)
2178// * strspn("",a) -> 0
2179// * strspn(s,"") -> 0
2180// * strcspn(s,a) -> const_int (if both args are constant)
2181// * strcspn("",a) -> 0
2182// * strcspn(s,"") -> strlen(a)
2183//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002184// tan, tanf, tanl:
2185// * tan(atan(x)) -> x
2186//
2187// trunc, truncf, truncl:
2188// * trunc(cnst) -> cnst'
2189//
2190//