blob: 3ae79eeca478f2b8ecb7bae723d36bb976540248 [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
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000345 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
346 return EmitMemCmp(Str1P, Str2P, CI->getOperand(3), B, TD);
347
Bill Wendling0582ae92009-03-13 04:39:26 +0000348 std::string Str1, Str2;
349 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
350 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000351
Bill Wendling0582ae92009-03-13 04:39:26 +0000352 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000353 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000354
Bill Wendling0582ae92009-03-13 04:39:26 +0000355 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000356 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000357
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000358 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000359 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000360 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000361 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000362 return 0;
363 }
364};
365
366
367//===---------------------------------------===//
368// 'strcpy' Optimizations
369
Chris Lattner3e8b6632009-09-02 06:11:42 +0000370struct StrCpyOpt : public LibCallOptimization {
Evan Chengeb8c6452010-03-24 20:19:04 +0000371 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
372
373 StrCpyOpt(bool c) : OptChkCall(c) {}
374
Eric Christopher7a61d702008-08-08 19:39:37 +0000375 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000376 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000377 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000378 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000379 if (FT->getNumParams() != NumParams ||
380 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000381 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000382 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000383 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000384
Eric Christopher551754c2010-04-16 23:37:20 +0000385 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000386 if (Dst == Src) // strcpy(x,x) -> x
387 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000388
Dan Gohmanf14d9192009-08-18 00:48:13 +0000389 // These optimizations require TargetData.
390 if (!TD) return 0;
391
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000392 // See if we can get the length of the input string.
393 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000394 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000395
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000396 // We have enough information to now generate the memcpy call to do the
397 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000398 if (OptChkCall)
399 EmitMemCpyChk(Dst, Src,
400 ConstantInt::get(TD->getIntPtrType(*Context), Len),
Eric Christopher551754c2010-04-16 23:37:20 +0000401 CI->getOperand(3), B, TD);
Evan Cheng0289b412010-03-23 15:48:04 +0000402 else
403 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000404 ConstantInt::get(TD->getIntPtrType(*Context), Len),
405 1, false, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000406 return Dst;
407 }
408};
409
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000410//===---------------------------------------===//
411// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000412
Chris Lattner3e8b6632009-09-02 06:11:42 +0000413struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000414 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
415 const FunctionType *FT = Callee->getFunctionType();
416 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
417 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000418 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000419 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000420 return 0;
421
Eric Christopher551754c2010-04-16 23:37:20 +0000422 Value *Dst = CI->getOperand(1);
423 Value *Src = CI->getOperand(2);
424 Value *LenOp = CI->getOperand(3);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000425
426 // See if we can get the length of the input string.
427 uint64_t SrcLen = GetStringLength(Src);
428 if (SrcLen == 0) return 0;
429 --SrcLen;
430
431 if (SrcLen == 0) {
432 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Mon P Wang20adc9d2010-04-04 03:10:48 +0000433 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'),
434 LenOp, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000435 return Dst;
436 }
437
438 uint64_t Len;
439 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
440 Len = LengthArg->getZExtValue();
441 else
442 return 0;
443
444 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
445
Dan Gohmanf14d9192009-08-18 00:48:13 +0000446 // These optimizations require TargetData.
447 if (!TD) return 0;
448
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000449 // Let strncpy handle the zero padding
450 if (Len > SrcLen+1) return 0;
451
452 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000453 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000454 ConstantInt::get(TD->getIntPtrType(*Context), Len),
455 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000456
457 return Dst;
458 }
459};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000460
461//===---------------------------------------===//
462// 'strlen' Optimizations
463
Chris Lattner3e8b6632009-09-02 06:11:42 +0000464struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000465 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000466 const FunctionType *FT = Callee->getFunctionType();
467 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000468 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000469 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000470 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000471
Eric Christopher551754c2010-04-16 23:37:20 +0000472 Value *Src = CI->getOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000473
474 // Constant folding: strlen("xyz") -> 3
475 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000476 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000477
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000478 // strlen(x) != 0 --> *x != 0
479 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000480 if (IsOnlyUsedInZeroEqualityComparison(CI))
481 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
482 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000483 }
484};
485
486//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000487// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000488
Chris Lattner3e8b6632009-09-02 06:11:42 +0000489struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000490 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
491 const FunctionType *FT = Callee->getFunctionType();
492 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000493 !FT->getParamType(0)->isPointerTy() ||
494 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000495 return 0;
496
Eric Christopher551754c2010-04-16 23:37:20 +0000497 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000498 if (isa<ConstantPointerNull>(EndPtr)) {
499 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000500 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000501 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000502
503 return 0;
504 }
505};
506
Chris Lattner24604112009-12-16 09:32:05 +0000507//===---------------------------------------===//
508// 'strstr' Optimizations
509
510struct StrStrOpt : public LibCallOptimization {
511 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
512 const FunctionType *FT = Callee->getFunctionType();
513 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000514 !FT->getParamType(0)->isPointerTy() ||
515 !FT->getParamType(1)->isPointerTy() ||
516 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000517 return 0;
518
519 // fold strstr(x, x) -> x.
Eric Christopher551754c2010-04-16 23:37:20 +0000520 if (CI->getOperand(1) == CI->getOperand(2))
521 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000522
Benjamin Kramer386e9182010-06-15 21:34:25 +0000523 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
524 if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getOperand(1))) {
525 Value *StrLen = EmitStrLen(CI->getOperand(2), B, TD);
526 Value *StrNCmp = EmitStrNCmp(CI->getOperand(1), CI->getOperand(2),
527 StrLen, B, TD);
528 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
529 UI != UE; ) {
530 ICmpInst *Old = cast<ICmpInst>(UI++);
531 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
532 ConstantInt::getNullValue(StrNCmp->getType()),
533 "cmp");
534 Old->replaceAllUsesWith(Cmp);
535 Old->eraseFromParent();
536 }
537 return CI;
538 }
539
Chris Lattner24604112009-12-16 09:32:05 +0000540 // See if either input string is a constant string.
541 std::string SearchStr, ToFindStr;
Eric Christopher551754c2010-04-16 23:37:20 +0000542 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
543 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000544
Chris Lattner24604112009-12-16 09:32:05 +0000545 // fold strstr(x, "") -> x.
546 if (HasStr2 && ToFindStr.empty())
Eric Christopher551754c2010-04-16 23:37:20 +0000547 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000548
Chris Lattner24604112009-12-16 09:32:05 +0000549 // If both strings are known, constant fold it.
550 if (HasStr1 && HasStr2) {
551 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000552
Chris Lattner24604112009-12-16 09:32:05 +0000553 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
554 return Constant::getNullValue(CI->getType());
555
556 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000557 Value *Result = CastToCStr(CI->getOperand(1), B);
Chris Lattner24604112009-12-16 09:32:05 +0000558 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
559 return B.CreateBitCast(Result, CI->getType());
560 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000561
Chris Lattner24604112009-12-16 09:32:05 +0000562 // fold strstr(x, "y") -> strchr(x, 'y').
563 if (HasStr2 && ToFindStr.size() == 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000564 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B, TD),
Chris Lattner24604112009-12-16 09:32:05 +0000565 CI->getType());
566 return 0;
567 }
568};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000569
Nick Lewycky4c498412009-02-13 15:31:46 +0000570
571//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000572// 'memcmp' Optimizations
573
Chris Lattner3e8b6632009-09-02 06:11:42 +0000574struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000575 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000576 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000577 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
578 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000579 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000580 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000581
Eric Christopher551754c2010-04-16 23:37:20 +0000582 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000583
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000584 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000585 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000586
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000587 // Make sure we have a constant length.
Eric Christopher551754c2010-04-16 23:37:20 +0000588 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000589 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000590 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000591
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000592 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000593 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000594
Benjamin Kramer48aefe12010-05-25 22:53:43 +0000595 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
596 if (Len == 1) {
597 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
598 CI->getType(), "lhsv");
599 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
600 CI->getType(), "rhsv");
Benjamin Kramer1464c1d2010-05-26 09:45:04 +0000601 return B.CreateSub(LHSV, RHSV, "chardiff");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000602 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000603
Benjamin Kramer992a6372009-11-05 17:44:22 +0000604 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
605 std::string LHSStr, RHSStr;
606 if (GetConstantStringInfo(LHS, LHSStr) &&
607 GetConstantStringInfo(RHS, RHSStr)) {
608 // Make sure we're not reading out-of-bounds memory.
609 if (Len > LHSStr.length() || Len > RHSStr.length())
610 return 0;
611 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
612 return ConstantInt::get(CI->getType(), Ret);
613 }
614
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000615 return 0;
616 }
617};
618
619//===---------------------------------------===//
620// 'memcpy' Optimizations
621
Chris Lattner3e8b6632009-09-02 06:11:42 +0000622struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000623 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000624 // These optimizations require TargetData.
625 if (!TD) return 0;
626
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000627 const FunctionType *FT = Callee->getFunctionType();
628 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000629 !FT->getParamType(0)->isPointerTy() ||
630 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000631 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000632 return 0;
633
634 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000635 EmitMemCpy(CI->getOperand(1), CI->getOperand(2),
636 CI->getOperand(3), 1, false, B, TD);
637 return CI->getOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000638 }
639};
640
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000641//===---------------------------------------===//
642// 'memmove' Optimizations
643
Chris Lattner3e8b6632009-09-02 06:11:42 +0000644struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000645 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000646 // These optimizations require TargetData.
647 if (!TD) return 0;
648
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000649 const FunctionType *FT = Callee->getFunctionType();
650 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000651 !FT->getParamType(0)->isPointerTy() ||
652 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000653 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000654 return 0;
655
656 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000657 EmitMemMove(CI->getOperand(1), CI->getOperand(2),
658 CI->getOperand(3), 1, false, B, TD);
659 return CI->getOperand(1);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000660 }
661};
662
663//===---------------------------------------===//
664// 'memset' Optimizations
665
Chris Lattner3e8b6632009-09-02 06:11:42 +0000666struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000667 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000668 // These optimizations require TargetData.
669 if (!TD) return 0;
670
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000671 const FunctionType *FT = Callee->getFunctionType();
672 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000673 !FT->getParamType(0)->isPointerTy() ||
674 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000675 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000676 return 0;
677
678 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000679 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
680 false);
681 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), false, B, TD);
682 return CI->getOperand(1);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000683 }
684};
685
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000686//===----------------------------------------------------------------------===//
687// Math Library Optimizations
688//===----------------------------------------------------------------------===//
689
690//===---------------------------------------===//
691// 'pow*' Optimizations
692
Chris Lattner3e8b6632009-09-02 06:11:42 +0000693struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000694 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000695 const FunctionType *FT = Callee->getFunctionType();
696 // Just make sure this has 2 arguments of the same FP type, which match the
697 // result type.
698 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
699 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000700 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000701 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000702
Eric Christopher551754c2010-04-16 23:37:20 +0000703 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000704 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
705 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
706 return Op1C;
707 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000708 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000709 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000710
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000711 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
712 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000713
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000714 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000715 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000716
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000717 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000718 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
719 // This is faster than calling pow, and still handles negative zero
720 // and negative infinite correctly.
721 // TODO: In fast-math mode, this could be just sqrt(x).
722 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000723 Value *Inf = ConstantFP::getInfinity(CI->getType());
724 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000725 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
726 Callee->getAttributes());
727 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
728 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000729 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
730 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
731 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000732 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000733
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000734 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
735 return Op1;
736 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000737 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000738 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000739 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000740 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000741 return 0;
742 }
743};
744
745//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000746// 'exp2' Optimizations
747
Chris Lattner3e8b6632009-09-02 06:11:42 +0000748struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000749 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000750 const FunctionType *FT = Callee->getFunctionType();
751 // Just make sure this has 1 argument of FP type, which matches the
752 // result type.
753 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000754 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000755 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000756
Eric Christopher551754c2010-04-16 23:37:20 +0000757 Value *Op = CI->getOperand(1);
Chris Lattnere818f772008-05-02 18:43:35 +0000758 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
759 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
760 Value *LdExpArg = 0;
761 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
762 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000763 LdExpArg = B.CreateSExt(OpC->getOperand(0),
764 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000765 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
766 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000767 LdExpArg = B.CreateZExt(OpC->getOperand(0),
768 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000769 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000770
Chris Lattnere818f772008-05-02 18:43:35 +0000771 if (LdExpArg) {
772 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000773 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000774 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000775 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000776 Name = "ldexp";
777 else
778 Name = "ldexpl";
779
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000780 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000781 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000782 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000783
784 Module *M = Caller->getParent();
785 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000786 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000787 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000788 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
789 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
790 CI->setCallingConv(F->getCallingConv());
791
792 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000793 }
794 return 0;
795 }
796};
Chris Lattnere818f772008-05-02 18:43:35 +0000797
798//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000799// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
800
Chris Lattner3e8b6632009-09-02 06:11:42 +0000801struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000802 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000803 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000804 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
805 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000806 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000807
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000808 // If this is something like 'floor((double)floatval)', convert to floorf.
Eric Christopher551754c2010-04-16 23:37:20 +0000809 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000810 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000811 return 0;
812
813 // floor((double)floatval) -> (double)floorf(floatval)
814 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000815 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
816 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000817 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000818 }
819};
820
821//===----------------------------------------------------------------------===//
822// Integer Optimizations
823//===----------------------------------------------------------------------===//
824
825//===---------------------------------------===//
826// 'ffs*' Optimizations
827
Chris Lattner3e8b6632009-09-02 06:11:42 +0000828struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000829 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000830 const FunctionType *FT = Callee->getFunctionType();
831 // Just make sure this has 2 arguments of the same FP type, which match the
832 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000833 if (FT->getNumParams() != 1 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000834 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000835 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000836 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000837
Eric Christopher551754c2010-04-16 23:37:20 +0000838 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000839
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000840 // Constant fold.
841 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
842 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000843 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000844 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000845 CI->getValue().countTrailingZeros()+1);
846 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000847
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000848 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
849 const Type *ArgType = Op->getType();
850 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
851 Intrinsic::cttz, &ArgType, 1);
852 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000853 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000854 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000855
Owen Andersona7235ea2009-07-31 20:28:14 +0000856 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000857 return B.CreateSelect(Cond, V,
858 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000859 }
860};
861
862//===---------------------------------------===//
863// 'isdigit' Optimizations
864
Chris Lattner3e8b6632009-09-02 06:11:42 +0000865struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000866 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000867 const FunctionType *FT = Callee->getFunctionType();
868 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000869 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000870 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000871 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000872
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000873 // isdigit(c) -> (c-'0') <u 10
Eric Christopher551754c2010-04-16 23:37:20 +0000874 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000875 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000876 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000877 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000878 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000879 return B.CreateZExt(Op, CI->getType());
880 }
881};
882
883//===---------------------------------------===//
884// 'isascii' Optimizations
885
Chris Lattner3e8b6632009-09-02 06:11:42 +0000886struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000887 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000888 const FunctionType *FT = Callee->getFunctionType();
889 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000890 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000891 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000892 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000893
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000894 // isascii(c) -> c <u 128
Eric Christopher551754c2010-04-16 23:37:20 +0000895 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000896 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000897 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000898 return B.CreateZExt(Op, CI->getType());
899 }
900};
Eric Christopher37c8b862009-10-07 21:14:25 +0000901
Chris Lattner313f0e62008-06-09 08:26:51 +0000902//===---------------------------------------===//
903// 'abs', 'labs', 'llabs' Optimizations
904
Chris Lattner3e8b6632009-09-02 06:11:42 +0000905struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000906 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +0000907 const FunctionType *FT = Callee->getFunctionType();
908 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +0000909 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +0000910 FT->getParamType(0) != FT->getReturnType())
911 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000912
Chris Lattner313f0e62008-06-09 08:26:51 +0000913 // abs(x) -> x >s -1 ? x : -x
Eric Christopher551754c2010-04-16 23:37:20 +0000914 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000915 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +0000916 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +0000917 "ispos");
918 Value *Neg = B.CreateNeg(Op, "neg");
919 return B.CreateSelect(Pos, Op, Neg);
920 }
921};
Eric Christopher37c8b862009-10-07 21:14:25 +0000922
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000923
924//===---------------------------------------===//
925// 'toascii' Optimizations
926
Chris Lattner3e8b6632009-09-02 06:11:42 +0000927struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000928 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000929 const FunctionType *FT = Callee->getFunctionType();
930 // We require i32(i32)
931 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000932 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000933 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000934
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000935 // isascii(c) -> c & 0x7f
Eric Christopher551754c2010-04-16 23:37:20 +0000936 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +0000937 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000938 }
939};
940
941//===----------------------------------------------------------------------===//
942// Formatting and IO Optimizations
943//===----------------------------------------------------------------------===//
944
945//===---------------------------------------===//
946// 'printf' Optimizations
947
Chris Lattner3e8b6632009-09-02 06:11:42 +0000948struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000949 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000950 // Require one fixed pointer argument and an integer/void result.
951 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000952 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
953 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000954 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000955 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000956
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000957 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000958 std::string FormatStr;
Eric Christopher551754c2010-04-16 23:37:20 +0000959 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +0000960 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000961
962 // Empty format string -> noop.
963 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +0000964 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000965 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000966
Chris Lattner74965f22009-11-09 04:57:04 +0000967 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
968 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000969 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +0000970 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000971 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +0000972 if (CI->use_empty()) return CI;
973 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000974 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000975
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000976 // printf("foo\n") --> puts("foo")
977 if (FormatStr[FormatStr.size()-1] == '\n' &&
978 FormatStr.find('%') == std::string::npos) { // no format characters.
979 // Create a string literal with no \n on it. We expect the constant merge
980 // pass to be run after this pass, to merge duplicate strings.
981 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000982 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +0000983 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
984 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +0000985 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000986 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000987 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000988 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000989
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000990 // Optimize specific format strings.
Eric Christopher551754c2010-04-16 23:37:20 +0000991 // printf("%c", chr) --> putchar(*(i8*)dst)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000992 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
Eric Christopher551754c2010-04-16 23:37:20 +0000993 CI->getOperand(2)->getType()->isIntegerTy()) {
994 Value *Res = EmitPutChar(CI->getOperand(2), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000995
Chris Lattner74965f22009-11-09 04:57:04 +0000996 if (CI->use_empty()) return CI;
997 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000998 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000999
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001000 // printf("%s\n", str) --> puts(str)
1001 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
Eric Christopher551754c2010-04-16 23:37:20 +00001002 CI->getOperand(2)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001003 CI->use_empty()) {
Eric Christopher551754c2010-04-16 23:37:20 +00001004 EmitPutS(CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001005 return CI;
1006 }
1007 return 0;
1008 }
1009};
1010
1011//===---------------------------------------===//
1012// 'sprintf' Optimizations
1013
Chris Lattner3e8b6632009-09-02 06:11:42 +00001014struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001015 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001016 // Require two fixed pointer arguments and an integer result.
1017 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001018 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1019 !FT->getParamType(1)->isPointerTy() ||
1020 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001021 return 0;
1022
1023 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001024 std::string FormatStr;
Eric Christopher551754c2010-04-16 23:37:20 +00001025 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001026 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001027
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001028 // If we just have a format string (nothing else crazy) transform it.
1029 if (CI->getNumOperands() == 3) {
1030 // Make sure there's no % in the constant array. We could try to handle
1031 // %% -> % in the future if we cared.
1032 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1033 if (FormatStr[i] == '%')
1034 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001035
1036 // These optimizations require TargetData.
1037 if (!TD) return 0;
1038
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001039 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
Eric Christopher551754c2010-04-16 23:37:20 +00001040 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Eric Christopherb6174e32010-03-05 22:25:30 +00001041 ConstantInt::get(TD->getIntPtrType(*Context),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001042 FormatStr.size()+1), 1, false, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001043 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001044 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001045
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001046 // The remaining optimizations require the format string to be "%s" or "%c"
1047 // and have an extra operand.
1048 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1049 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001050
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001051 // Decode the second character of the format string.
1052 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001053 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Eric Christopher551754c2010-04-16 23:37:20 +00001054 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
1055 Value *V = B.CreateTrunc(CI->getOperand(3),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001056 Type::getInt8Ty(*Context), "char");
Eric Christopher551754c2010-04-16 23:37:20 +00001057 Value *Ptr = CastToCStr(CI->getOperand(1), B);
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001058 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001059 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001060 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001061 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001062
Owen Andersoneed707b2009-07-24 23:12:02 +00001063 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001064 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001065
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001066 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001067 // These optimizations require TargetData.
1068 if (!TD) return 0;
1069
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001070 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Eric Christopher551754c2010-04-16 23:37:20 +00001071 if (!CI->getOperand(3)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001072
Eric Christopher551754c2010-04-16 23:37:20 +00001073 Value *Len = EmitStrLen(CI->getOperand(3), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001074 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001075 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001076 "leninc");
Eric Christopher551754c2010-04-16 23:37:20 +00001077 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, false, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001078
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001079 // The sprintf result is the unincremented number of bytes in the string.
1080 return B.CreateIntCast(Len, CI->getType(), false);
1081 }
1082 return 0;
1083 }
1084};
1085
1086//===---------------------------------------===//
1087// 'fwrite' Optimizations
1088
Chris Lattner3e8b6632009-09-02 06:11:42 +00001089struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001090 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001091 // Require a pointer, an integer, an integer, a pointer, returning integer.
1092 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001093 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1094 !FT->getParamType(1)->isIntegerTy() ||
1095 !FT->getParamType(2)->isIntegerTy() ||
1096 !FT->getParamType(3)->isPointerTy() ||
1097 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001098 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001099
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001100 // Get the element size and count.
Eric Christopher551754c2010-04-16 23:37:20 +00001101 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1102 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001103 if (!SizeC || !CountC) return 0;
1104 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001105
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001106 // If this is writing zero records, remove the call (it's a noop).
1107 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001108 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001109
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001110 // If this is writing one byte, turn it into fputc.
1111 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Eric Christopher551754c2010-04-16 23:37:20 +00001112 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1113 EmitFPutC(Char, CI->getOperand(4), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001114 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001115 }
1116
1117 return 0;
1118 }
1119};
1120
1121//===---------------------------------------===//
1122// 'fputs' Optimizations
1123
Chris Lattner3e8b6632009-09-02 06:11:42 +00001124struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001125 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001126 // These optimizations require TargetData.
1127 if (!TD) return 0;
1128
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001129 // Require two pointers. Also, we can't optimize if return value is used.
1130 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001131 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1132 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001133 !CI->use_empty())
1134 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001135
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001136 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
Eric Christopher551754c2010-04-16 23:37:20 +00001137 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001138 if (!Len) return 0;
Eric Christopher551754c2010-04-16 23:37:20 +00001139 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001140 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Eric Christopher551754c2010-04-16 23:37:20 +00001141 CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001142 return CI; // Known to have no uses (see above).
1143 }
1144};
1145
1146//===---------------------------------------===//
1147// 'fprintf' Optimizations
1148
Chris Lattner3e8b6632009-09-02 06:11:42 +00001149struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001150 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001151 // Require two fixed paramters as pointers and integer result.
1152 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001153 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1154 !FT->getParamType(1)->isPointerTy() ||
1155 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001156 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001157
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001158 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001159 std::string FormatStr;
Eric Christopher551754c2010-04-16 23:37:20 +00001160 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001161 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001162
1163 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1164 if (CI->getNumOperands() == 3) {
1165 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1166 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001167 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001168
1169 // These optimizations require TargetData.
1170 if (!TD) return 0;
1171
Eric Christopher551754c2010-04-16 23:37:20 +00001172 EmitFWrite(CI->getOperand(2),
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001173 ConstantInt::get(TD->getIntPtrType(*Context),
1174 FormatStr.size()),
Eric Christopher551754c2010-04-16 23:37:20 +00001175 CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001176 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001177 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001178
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001179 // The remaining optimizations require the format string to be "%s" or "%c"
1180 // and have an extra operand.
1181 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1182 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001183
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001184 // Decode the second character of the format string.
1185 if (FormatStr[1] == 'c') {
Eric Christopher551754c2010-04-16 23:37:20 +00001186 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1187 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
1188 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001189 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001190 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001191
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001192 if (FormatStr[1] == 's') {
Eric Christopher551754c2010-04-16 23:37:20 +00001193 // fprintf(F, "%s", str) -> fputs(str, F)
1194 if (!CI->getOperand(3)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001195 return 0;
Eric Christopher551754c2010-04-16 23:37:20 +00001196 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001197 return CI;
1198 }
1199 return 0;
1200 }
1201};
1202
Bill Wendlingac178222008-05-05 21:37:59 +00001203} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001204
1205//===----------------------------------------------------------------------===//
1206// SimplifyLibCalls Pass Implementation
1207//===----------------------------------------------------------------------===//
1208
1209namespace {
1210 /// This pass optimizes well known library functions from libc and libm.
1211 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001212 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001213 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001214 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001215 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
Evan Cheng0289b412010-03-23 15:48:04 +00001216 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
1217 StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001218 StrToOpt StrTo; StrStrOpt StrStr;
1219 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001220 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001221 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001222 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001223 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1224 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001225 // Formatting and IO Optimizations
1226 SPrintFOpt SPrintF; PrintFOpt PrintF;
1227 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001228
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001229 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001230 public:
1231 static char ID; // Pass identification
Evan Chengeb8c6452010-03-24 20:19:04 +00001232 SimplifyLibCalls() : FunctionPass(&ID), StrCpy(false), StrCpyChk(true) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001233 void InitOptimizations();
1234 bool runOnFunction(Function &F);
1235
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001236 void setDoesNotAccessMemory(Function &F);
1237 void setOnlyReadsMemory(Function &F);
1238 void setDoesNotThrow(Function &F);
1239 void setDoesNotCapture(Function &F, unsigned n);
1240 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001241 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001242
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001243 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001244 }
1245 };
1246 char SimplifyLibCalls::ID = 0;
1247} // end anonymous namespace.
1248
1249static RegisterPass<SimplifyLibCalls>
1250X("simplify-libcalls", "Simplify well-known library calls");
1251
1252// Public interface to the Simplify LibCalls pass.
1253FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001254 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001255}
1256
1257/// Optimizations - Populate the Optimizations map with all the optimizations
1258/// we know.
1259void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001260 // String and Memory LibCall Optimizations
1261 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001262 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001263 Optimizations["strchr"] = &StrChr;
1264 Optimizations["strcmp"] = &StrCmp;
1265 Optimizations["strncmp"] = &StrNCmp;
1266 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001267 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001268 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001269 Optimizations["strtol"] = &StrTo;
1270 Optimizations["strtod"] = &StrTo;
1271 Optimizations["strtof"] = &StrTo;
1272 Optimizations["strtoul"] = &StrTo;
1273 Optimizations["strtoll"] = &StrTo;
1274 Optimizations["strtold"] = &StrTo;
1275 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001276 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001277 Optimizations["memcmp"] = &MemCmp;
1278 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001279 Optimizations["memmove"] = &MemMove;
1280 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001281
Evan Cheng0289b412010-03-23 15:48:04 +00001282 // _chk variants of String and Memory LibCall Optimizations.
Evan Cheng0289b412010-03-23 15:48:04 +00001283 Optimizations["__strcpy_chk"] = &StrCpyChk;
1284
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001285 // Math Library Optimizations
1286 Optimizations["powf"] = &Pow;
1287 Optimizations["pow"] = &Pow;
1288 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001289 Optimizations["llvm.pow.f32"] = &Pow;
1290 Optimizations["llvm.pow.f64"] = &Pow;
1291 Optimizations["llvm.pow.f80"] = &Pow;
1292 Optimizations["llvm.pow.f128"] = &Pow;
1293 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001294 Optimizations["exp2l"] = &Exp2;
1295 Optimizations["exp2"] = &Exp2;
1296 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001297 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1298 Optimizations["llvm.exp2.f128"] = &Exp2;
1299 Optimizations["llvm.exp2.f80"] = &Exp2;
1300 Optimizations["llvm.exp2.f64"] = &Exp2;
1301 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001302
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001303#ifdef HAVE_FLOORF
1304 Optimizations["floor"] = &UnaryDoubleFP;
1305#endif
1306#ifdef HAVE_CEILF
1307 Optimizations["ceil"] = &UnaryDoubleFP;
1308#endif
1309#ifdef HAVE_ROUNDF
1310 Optimizations["round"] = &UnaryDoubleFP;
1311#endif
1312#ifdef HAVE_RINTF
1313 Optimizations["rint"] = &UnaryDoubleFP;
1314#endif
1315#ifdef HAVE_NEARBYINTF
1316 Optimizations["nearbyint"] = &UnaryDoubleFP;
1317#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001318
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001319 // Integer Optimizations
1320 Optimizations["ffs"] = &FFS;
1321 Optimizations["ffsl"] = &FFS;
1322 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001323 Optimizations["abs"] = &Abs;
1324 Optimizations["labs"] = &Abs;
1325 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001326 Optimizations["isdigit"] = &IsDigit;
1327 Optimizations["isascii"] = &IsAscii;
1328 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001329
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001330 // Formatting and IO Optimizations
1331 Optimizations["sprintf"] = &SPrintF;
1332 Optimizations["printf"] = &PrintF;
1333 Optimizations["fwrite"] = &FWrite;
1334 Optimizations["fputs"] = &FPuts;
1335 Optimizations["fprintf"] = &FPrintF;
1336}
1337
1338
1339/// runOnFunction - Top level algorithm.
1340///
1341bool SimplifyLibCalls::runOnFunction(Function &F) {
1342 if (Optimizations.empty())
1343 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001344
Dan Gohmanf14d9192009-08-18 00:48:13 +00001345 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001346
Owen Andersone922c022009-07-22 00:24:57 +00001347 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001348
1349 bool Changed = false;
1350 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1351 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1352 // Ignore non-calls.
1353 CallInst *CI = dyn_cast<CallInst>(I++);
1354 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001355
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001356 // Ignore indirect calls and calls to non-external functions.
1357 Function *Callee = CI->getCalledFunction();
1358 if (Callee == 0 || !Callee->isDeclaration() ||
1359 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1360 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001361
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001362 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001363 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1364 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001365
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001366 // Set the builder to the instruction after the call.
1367 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001368
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001369 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001370 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001371 if (Result == 0) continue;
1372
David Greene6a6b90e2010-01-05 01:27:21 +00001373 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1374 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001375
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001376 // Something changed!
1377 Changed = true;
1378 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001379
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001380 // Inspect the instruction after the call (which was potentially just
1381 // added) next.
1382 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001383
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001384 if (CI != Result && !CI->use_empty()) {
1385 CI->replaceAllUsesWith(Result);
1386 if (!Result->hasName())
1387 Result->takeName(CI);
1388 }
1389 CI->eraseFromParent();
1390 }
1391 }
1392 return Changed;
1393}
1394
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001395// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001396
1397void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1398 if (!F.doesNotAccessMemory()) {
1399 F.setDoesNotAccessMemory();
1400 ++NumAnnotated;
1401 Modified = true;
1402 }
1403}
1404void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1405 if (!F.onlyReadsMemory()) {
1406 F.setOnlyReadsMemory();
1407 ++NumAnnotated;
1408 Modified = true;
1409 }
1410}
1411void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1412 if (!F.doesNotThrow()) {
1413 F.setDoesNotThrow();
1414 ++NumAnnotated;
1415 Modified = true;
1416 }
1417}
1418void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1419 if (!F.doesNotCapture(n)) {
1420 F.setDoesNotCapture(n);
1421 ++NumAnnotated;
1422 Modified = true;
1423 }
1424}
1425void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1426 if (!F.doesNotAlias(n)) {
1427 F.setDoesNotAlias(n);
1428 ++NumAnnotated;
1429 Modified = true;
1430 }
1431}
1432
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001433/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001434///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001435bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001436 Modified = false;
1437 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1438 Function &F = *I;
1439 if (!F.isDeclaration())
1440 continue;
1441
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001442 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001443 continue;
1444
1445 const FunctionType *FTy = F.getFunctionType();
1446
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001447 StringRef Name = F.getName();
1448 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001449 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001450 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001451 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001452 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001453 continue;
1454 setOnlyReadsMemory(F);
1455 setDoesNotThrow(F);
1456 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001457 } else if (Name == "strchr" ||
1458 Name == "strrchr") {
1459 if (FTy->getNumParams() != 2 ||
1460 !FTy->getParamType(0)->isPointerTy() ||
1461 !FTy->getParamType(1)->isIntegerTy())
1462 continue;
1463 setOnlyReadsMemory(F);
1464 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001465 } else if (Name == "strcpy" ||
1466 Name == "stpcpy" ||
1467 Name == "strcat" ||
1468 Name == "strtol" ||
1469 Name == "strtod" ||
1470 Name == "strtof" ||
1471 Name == "strtoul" ||
1472 Name == "strtoll" ||
1473 Name == "strtold" ||
1474 Name == "strncat" ||
1475 Name == "strncpy" ||
1476 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001477 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001478 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001479 continue;
1480 setDoesNotThrow(F);
1481 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001482 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001483 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001484 !FTy->getParamType(0)->isPointerTy() ||
1485 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001486 continue;
1487 setDoesNotThrow(F);
1488 setDoesNotCapture(F, 1);
1489 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001490 } else if (Name == "strcmp" ||
1491 Name == "strspn" ||
1492 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001493 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001494 Name == "strcoll" ||
1495 Name == "strcasecmp" ||
1496 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001497 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001498 !FTy->getParamType(0)->isPointerTy() ||
1499 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001500 continue;
1501 setOnlyReadsMemory(F);
1502 setDoesNotThrow(F);
1503 setDoesNotCapture(F, 1);
1504 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001505 } else if (Name == "strstr" ||
1506 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001507 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001508 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001509 continue;
1510 setOnlyReadsMemory(F);
1511 setDoesNotThrow(F);
1512 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001513 } else if (Name == "strtok" ||
1514 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001515 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001516 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001517 continue;
1518 setDoesNotThrow(F);
1519 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001520 } else if (Name == "scanf" ||
1521 Name == "setbuf" ||
1522 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001523 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001524 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001525 continue;
1526 setDoesNotThrow(F);
1527 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001528 } else if (Name == "strdup" ||
1529 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001530 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001531 !FTy->getReturnType()->isPointerTy() ||
1532 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001533 continue;
1534 setDoesNotThrow(F);
1535 setDoesNotAlias(F, 0);
1536 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001537 } else if (Name == "stat" ||
1538 Name == "sscanf" ||
1539 Name == "sprintf" ||
1540 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001541 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001542 !FTy->getParamType(0)->isPointerTy() ||
1543 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001544 continue;
1545 setDoesNotThrow(F);
1546 setDoesNotCapture(F, 1);
1547 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001548 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001549 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001550 !FTy->getParamType(0)->isPointerTy() ||
1551 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001552 continue;
1553 setDoesNotThrow(F);
1554 setDoesNotCapture(F, 1);
1555 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001556 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001557 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001558 !FTy->getParamType(1)->isPointerTy() ||
1559 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001560 continue;
1561 setDoesNotThrow(F);
1562 setDoesNotCapture(F, 2);
1563 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001564 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001565 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001566 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001567 continue;
1568 // May throw; "system" is a valid pthread cancellation point.
1569 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001570 }
1571 break;
1572 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001573 if (Name == "malloc") {
1574 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001575 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001576 continue;
1577 setDoesNotThrow(F);
1578 setDoesNotAlias(F, 0);
1579 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001580 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001581 !FTy->getParamType(0)->isPointerTy() ||
1582 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001583 continue;
1584 setOnlyReadsMemory(F);
1585 setDoesNotThrow(F);
1586 setDoesNotCapture(F, 1);
1587 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001588 } else if (Name == "memchr" ||
1589 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001590 if (FTy->getNumParams() != 3)
1591 continue;
1592 setOnlyReadsMemory(F);
1593 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001594 } else if (Name == "modf" ||
1595 Name == "modff" ||
1596 Name == "modfl" ||
1597 Name == "memcpy" ||
1598 Name == "memccpy" ||
1599 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001600 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001601 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001602 continue;
1603 setDoesNotThrow(F);
1604 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001605 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001606 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001607 continue;
1608 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001609 } else if (Name == "mkdir" ||
1610 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001611 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001612 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001613 continue;
1614 setDoesNotThrow(F);
1615 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001616 }
1617 break;
1618 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001619 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001620 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001621 !FTy->getParamType(0)->isPointerTy() ||
1622 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001623 continue;
1624 setDoesNotThrow(F);
1625 setDoesNotAlias(F, 0);
1626 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001627 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001628 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001629 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001630 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001631 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001632 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001633 } else if (Name == "rmdir" ||
1634 Name == "rewind" ||
1635 Name == "remove" ||
1636 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001637 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001638 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001639 continue;
1640 setDoesNotThrow(F);
1641 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001642 } else if (Name == "rename" ||
1643 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001644 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001645 !FTy->getParamType(0)->isPointerTy() ||
1646 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001647 continue;
1648 setDoesNotThrow(F);
1649 setDoesNotCapture(F, 1);
1650 setDoesNotCapture(F, 2);
1651 }
1652 break;
1653 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001654 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001655 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001656 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001657 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001658 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001659 setDoesNotCapture(F, 2);
1660 }
1661 break;
1662 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001663 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001664 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001665 !FTy->getParamType(0)->isPointerTy() ||
1666 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001667 continue;
1668 setDoesNotThrow(F);
1669 setDoesNotCapture(F, 1);
1670 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001671 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001672 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001673 !FTy->getParamType(0)->isPointerTy() ||
1674 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001675 continue;
1676 setDoesNotThrow(F);
1677 setOnlyReadsMemory(F);
1678 setDoesNotCapture(F, 1);
1679 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001680 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001681 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001682 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001683 continue;
1684 setDoesNotThrow(F);
1685 setDoesNotCapture(F, 1);
1686 }
1687 break;
1688 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001689 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001690 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001691 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001692 continue;
1693 setDoesNotThrow(F);
1694 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001695 } else if (Name == "chmod" ||
1696 Name == "chown" ||
1697 Name == "ctermid" ||
1698 Name == "clearerr" ||
1699 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001700 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001701 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001702 continue;
1703 setDoesNotThrow(F);
1704 setDoesNotCapture(F, 1);
1705 }
1706 break;
1707 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001708 if (Name == "atoi" ||
1709 Name == "atol" ||
1710 Name == "atof" ||
1711 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001712 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001713 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001714 continue;
1715 setDoesNotThrow(F);
1716 setOnlyReadsMemory(F);
1717 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001718 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001719 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001720 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001721 continue;
1722 setDoesNotThrow(F);
1723 setDoesNotCapture(F, 1);
1724 }
1725 break;
1726 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001727 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001728 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001729 !FTy->getReturnType()->isPointerTy() ||
1730 !FTy->getParamType(0)->isPointerTy() ||
1731 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001732 continue;
1733 setDoesNotThrow(F);
1734 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001735 setDoesNotCapture(F, 1);
1736 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001737 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001738 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001739 !FTy->getReturnType()->isPointerTy() ||
1740 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001741 continue;
1742 setDoesNotThrow(F);
1743 setDoesNotAlias(F, 0);
1744 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001745 } else if (Name == "feof" ||
1746 Name == "free" ||
1747 Name == "fseek" ||
1748 Name == "ftell" ||
1749 Name == "fgetc" ||
1750 Name == "fseeko" ||
1751 Name == "ftello" ||
1752 Name == "fileno" ||
1753 Name == "fflush" ||
1754 Name == "fclose" ||
1755 Name == "fsetpos" ||
1756 Name == "flockfile" ||
1757 Name == "funlockfile" ||
1758 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001759 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001760 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001761 continue;
1762 setDoesNotThrow(F);
1763 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001764 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001765 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001766 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001767 continue;
1768 setDoesNotThrow(F);
1769 setDoesNotCapture(F, 1);
1770 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001771 } else if (Name == "fputc" ||
1772 Name == "fstat" ||
1773 Name == "frexp" ||
1774 Name == "frexpf" ||
1775 Name == "frexpl" ||
1776 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001777 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001778 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001779 continue;
1780 setDoesNotThrow(F);
1781 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001782 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001783 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001784 !FTy->getParamType(0)->isPointerTy() ||
1785 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001786 continue;
1787 setDoesNotThrow(F);
1788 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001789 } else if (Name == "fread" ||
1790 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001791 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001792 !FTy->getParamType(0)->isPointerTy() ||
1793 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001794 continue;
1795 setDoesNotThrow(F);
1796 setDoesNotCapture(F, 1);
1797 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001798 } else if (Name == "fputs" ||
1799 Name == "fscanf" ||
1800 Name == "fprintf" ||
1801 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001802 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001803 !FTy->getParamType(0)->isPointerTy() ||
1804 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001805 continue;
1806 setDoesNotThrow(F);
1807 setDoesNotCapture(F, 1);
1808 setDoesNotCapture(F, 2);
1809 }
1810 break;
1811 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001812 if (Name == "getc" ||
1813 Name == "getlogin_r" ||
1814 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001815 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001816 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001817 continue;
1818 setDoesNotThrow(F);
1819 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001820 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001821 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001822 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001823 continue;
1824 setDoesNotThrow(F);
1825 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001826 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001827 } else if (Name == "gets" ||
1828 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001829 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001830 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001831 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001832 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001833 continue;
1834 setDoesNotThrow(F);
1835 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001836 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001837 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001838 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001839 continue;
1840 setDoesNotThrow(F);
1841 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001842 }
1843 break;
1844 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001845 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001846 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001847 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001848 continue;
1849 setDoesNotThrow(F);
1850 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001851 } else if (Name == "uname" ||
1852 Name == "unlink" ||
1853 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001854 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001855 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001856 continue;
1857 setDoesNotThrow(F);
1858 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001859 } else if (Name == "utime" ||
1860 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001861 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001862 !FTy->getParamType(0)->isPointerTy() ||
1863 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001864 continue;
1865 setDoesNotThrow(F);
1866 setDoesNotCapture(F, 1);
1867 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001868 }
1869 break;
1870 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001871 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001872 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001873 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001874 continue;
1875 setDoesNotThrow(F);
1876 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001877 } else if (Name == "puts" ||
1878 Name == "printf" ||
1879 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001880 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001881 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001882 continue;
1883 setDoesNotThrow(F);
1884 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001885 } else if (Name == "pread" ||
1886 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001887 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001888 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001889 continue;
1890 // May throw; these are valid pthread cancellation points.
1891 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001892 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001893 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001894 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001895 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001896 !FTy->getReturnType()->isPointerTy() ||
1897 !FTy->getParamType(0)->isPointerTy() ||
1898 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001899 continue;
1900 setDoesNotThrow(F);
1901 setDoesNotAlias(F, 0);
1902 setDoesNotCapture(F, 1);
1903 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001904 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001905 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001906 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001907 continue;
1908 setDoesNotThrow(F);
1909 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001910 }
1911 break;
1912 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001913 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001914 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001915 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001916 continue;
1917 setDoesNotThrow(F);
1918 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001919 } else if (Name == "vsscanf" ||
1920 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001921 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001922 !FTy->getParamType(1)->isPointerTy() ||
1923 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001924 continue;
1925 setDoesNotThrow(F);
1926 setDoesNotCapture(F, 1);
1927 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001928 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00001929 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001930 continue;
1931 setDoesNotThrow(F);
1932 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001933 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001934 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001935 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001936 continue;
1937 setDoesNotThrow(F);
1938 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001939 } else if (Name == "vfprintf" ||
1940 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001941 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001942 !FTy->getParamType(0)->isPointerTy() ||
1943 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001944 continue;
1945 setDoesNotThrow(F);
1946 setDoesNotCapture(F, 1);
1947 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001948 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001949 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001950 !FTy->getParamType(0)->isPointerTy() ||
1951 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001952 continue;
1953 setDoesNotThrow(F);
1954 setDoesNotCapture(F, 1);
1955 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001956 }
1957 break;
1958 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001959 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001960 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001961 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001962 continue;
1963 // May throw; "open" is a valid pthread cancellation point.
1964 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001965 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001966 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001967 !FTy->getReturnType()->isPointerTy() ||
1968 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001969 continue;
1970 setDoesNotThrow(F);
1971 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00001972 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001973 }
1974 break;
1975 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001976 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00001977 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001978 continue;
1979 setDoesNotThrow(F);
1980 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001981 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001982 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001983 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001984 continue;
1985 setDoesNotThrow(F);
1986 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001987 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001988 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001989 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001990 if (Name == "htonl" ||
1991 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001992 setDoesNotThrow(F);
1993 setDoesNotAccessMemory(F);
1994 }
1995 break;
1996 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001997 if (Name == "ntohl" ||
1998 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001999 setDoesNotThrow(F);
2000 setDoesNotAccessMemory(F);
2001 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002002 break;
2003 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002004 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002005 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002006 !FTy->getParamType(0)->isPointerTy() ||
2007 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002008 continue;
2009 setDoesNotThrow(F);
2010 setDoesNotCapture(F, 1);
2011 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002012 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002013 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002014 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002015 continue;
2016 setDoesNotThrow(F);
2017 setDoesNotCapture(F, 1);
2018 }
2019 break;
2020 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002021 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002022 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002023 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002024 continue;
2025 // May throw; places call through function pointer.
2026 setDoesNotCapture(F, 4);
2027 }
2028 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002029 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002030 if (Name == "__strdup" ||
2031 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002032 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002033 !FTy->getReturnType()->isPointerTy() ||
2034 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002035 continue;
2036 setDoesNotThrow(F);
2037 setDoesNotAlias(F, 0);
2038 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002039 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002040 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002041 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002042 continue;
2043 setDoesNotThrow(F);
2044 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002045 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002046 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002047 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002048 continue;
2049 setDoesNotThrow(F);
2050 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002051 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002052 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002053 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002054 continue;
2055 setDoesNotThrow(F);
2056 setDoesNotCapture(F, 2);
2057 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002058 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002059 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002060 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002061 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002062 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002063 continue;
2064 setDoesNotThrow(F);
2065 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002066 } else if (Name == "\1stat64" ||
2067 Name == "\1lstat64" ||
2068 Name == "\1statvfs64" ||
2069 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002070 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002071 !FTy->getParamType(0)->isPointerTy() ||
2072 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002073 continue;
2074 setDoesNotThrow(F);
2075 setDoesNotCapture(F, 1);
2076 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002077 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002078 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002079 !FTy->getReturnType()->isPointerTy() ||
2080 !FTy->getParamType(0)->isPointerTy() ||
2081 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002082 continue;
2083 setDoesNotThrow(F);
2084 setDoesNotAlias(F, 0);
2085 setDoesNotCapture(F, 1);
2086 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002087 } else if (Name == "\1fseeko64" ||
2088 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002089 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002090 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002091 continue;
2092 setDoesNotThrow(F);
2093 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002094 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002095 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002096 continue;
2097 setDoesNotThrow(F);
2098 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002099 } else if (Name == "\1fstat64" ||
2100 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002101 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002102 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002103 continue;
2104 setDoesNotThrow(F);
2105 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002106 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002107 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002108 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002109 continue;
2110 // May throw; "open" is a valid pthread cancellation point.
2111 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002112 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002113 break;
2114 }
2115 }
2116 return Modified;
2117}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002118
2119// TODO:
2120// Additional cases that we need to add to this file:
2121//
2122// cbrt:
2123// * cbrt(expN(X)) -> expN(x/3)
2124// * cbrt(sqrt(x)) -> pow(x,1/6)
2125// * cbrt(sqrt(x)) -> pow(x,1/9)
2126//
2127// cos, cosf, cosl:
2128// * cos(-x) -> cos(x)
2129//
2130// exp, expf, expl:
2131// * exp(log(x)) -> x
2132//
2133// log, logf, logl:
2134// * log(exp(x)) -> x
2135// * log(x**y) -> y*log(x)
2136// * log(exp(y)) -> y*log(e)
2137// * log(exp2(y)) -> y*log(2)
2138// * log(exp10(y)) -> y*log(10)
2139// * log(sqrt(x)) -> 0.5*log(x)
2140// * log(pow(x,y)) -> y*log(x)
2141//
2142// lround, lroundf, lroundl:
2143// * lround(cnst) -> cnst'
2144//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002145// pow, powf, powl:
2146// * pow(exp(x),y) -> exp(x*y)
2147// * pow(sqrt(x),y) -> pow(x,y*0.5)
2148// * pow(pow(x,y),z)-> pow(x,y*z)
2149//
2150// puts:
2151// * puts("") -> putchar("\n")
2152//
2153// round, roundf, roundl:
2154// * round(cnst) -> cnst'
2155//
2156// signbit:
2157// * signbit(cnst) -> cnst'
2158// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2159//
2160// sqrt, sqrtf, sqrtl:
2161// * sqrt(expN(x)) -> expN(x*0.5)
2162// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2163// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2164//
2165// stpcpy:
2166// * stpcpy(str, "literal") ->
2167// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2168// strrchr:
2169// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2170// (if c is a constant integer and s is a constant string)
2171// * strrchr(s1,0) -> strchr(s1,0)
2172//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002173// strpbrk:
2174// * strpbrk(s,a) -> offset_in_for(s,a)
2175// (if s and a are both constant strings)
2176// * strpbrk(s,"") -> 0
2177// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2178//
2179// strspn, strcspn:
2180// * strspn(s,a) -> const_int (if both args are constant)
2181// * strspn("",a) -> 0
2182// * strspn(s,"") -> 0
2183// * strcspn(s,a) -> const_int (if both args are constant)
2184// * strcspn("",a) -> 0
2185// * strcspn(s,"") -> strlen(a)
2186//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002187// tan, tanf, tanl:
2188// * tan(atan(x)) -> x
2189//
2190// trunc, truncf, truncl:
2191// * trunc(cnst) -> cnst'
2192//
2193//