blob: 9e263ddf91bae3b4592b4e32aa58b6abd1c22742 [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:
53 LibCallOptimization() { }
54 virtual ~LibCallOptimization() {}
55
56 /// CallOptimizer - This pure virtual method is implemented by base classes to
57 /// do various optimizations. If this returns null then no transformation was
58 /// performed. If it returns CI, then it transformed the call and CI is to be
59 /// deleted. If it returns something else, replace CI with the new value and
60 /// delete CI.
Eric 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
95//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000096// String and Memory LibCall Optimizations
97//===----------------------------------------------------------------------===//
98
99//===---------------------------------------===//
100// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000101namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000102struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000103 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000104 // Verify the "strcat" function prototype.
105 const FunctionType *FT = Callee->getFunctionType();
106 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000107 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000108 FT->getParamType(0) != FT->getReturnType() ||
109 FT->getParamType(1) != FT->getReturnType())
110 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000111
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000112 // Extract some information from the instruction
113 Value *Dst = CI->getOperand(1);
114 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000115
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000116 // See if we can get the length of the input string.
117 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000118 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000119 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000120
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000121 // Handle the simple, do-nothing case: strcat(x, "") -> x
122 if (Len == 0)
123 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000124
125 // These optimizations require TargetData.
126 if (!TD) return 0;
127
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000128 EmitStrLenMemCpy(Src, Dst, Len, B);
129 return Dst;
130 }
131
132 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000133 // We need to find the end of the destination string. That's where the
134 // memory is to be moved to. We just generate a call to strlen.
Eric Christopherb6174e32010-03-05 22:25:30 +0000135 Value *DstLen = EmitStrLen(Dst, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000136
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000137 // Now that we have the destination's length, we must index into the
138 // destination's pointer to get the actual memcpy destination (end of
139 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000140 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000141
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000142 // We have enough information to now generate the memcpy call to do the
143 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000144 EmitMemCpy(CpyDst, Src,
Eric Christopherb6174e32010-03-05 22:25:30 +0000145 ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000146 }
147};
148
149//===---------------------------------------===//
150// 'strncat' Optimizations
151
Chris Lattner3e8b6632009-09-02 06:11:42 +0000152struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000153 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
154 // Verify the "strncat" function prototype.
155 const FunctionType *FT = Callee->getFunctionType();
156 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000157 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000158 FT->getParamType(0) != FT->getReturnType() ||
159 FT->getParamType(1) != FT->getReturnType() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000160 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000161 return 0;
162
163 // Extract some information from the instruction
164 Value *Dst = CI->getOperand(1);
165 Value *Src = CI->getOperand(2);
166 uint64_t Len;
167
168 // We don't do anything if length is not constant
169 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
170 Len = LengthArg->getZExtValue();
171 else
172 return 0;
173
174 // See if we can get the length of the input string.
175 uint64_t SrcLen = GetStringLength(Src);
176 if (SrcLen == 0) return 0;
177 --SrcLen; // Unbias length.
178
179 // Handle the simple, do-nothing cases:
180 // strncat(x, "", c) -> x
181 // strncat(x, c, 0) -> x
182 if (SrcLen == 0 || Len == 0) return Dst;
183
Dan Gohmanf14d9192009-08-18 00:48:13 +0000184 // These optimizations require TargetData.
185 if (!TD) return 0;
186
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000187 // We don't optimize this case
188 if (Len < SrcLen) return 0;
189
190 // strncat(x, s, c) -> strcat(x, s)
191 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000192 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000193 return Dst;
194 }
195};
196
197//===---------------------------------------===//
198// 'strchr' Optimizations
199
Chris Lattner3e8b6632009-09-02 06:11:42 +0000200struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000201 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000202 // Verify the "strchr" function prototype.
203 const FunctionType *FT = Callee->getFunctionType();
204 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000205 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000206 FT->getParamType(0) != FT->getReturnType())
207 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000208
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000209 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000210
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000211 // If the second operand is non-constant, see if we can compute the length
212 // of the input string and turn this into memchr.
213 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
214 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000215 // These optimizations require TargetData.
216 if (!TD) return 0;
217
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000218 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000219 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000220 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000221
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000222 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000223 ConstantInt::get(TD->getIntPtrType(*Context), Len),
224 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000225 }
226
227 // Otherwise, the character is a constant, see if the first argument is
228 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000229 std::string Str;
230 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000231 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000232
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000233 // strchr can find the nul character.
234 Str += '\0';
235 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000236
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000237 // Compute the offset.
238 uint64_t i = 0;
239 while (1) {
240 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000241 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000242 // Did we find our match?
243 if (Str[i] == CharValue)
244 break;
245 ++i;
246 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000247
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000248 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000249 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000250 return B.CreateGEP(SrcStr, Idx, "strchr");
251 }
252};
253
254//===---------------------------------------===//
255// 'strcmp' Optimizations
256
Chris Lattner3e8b6632009-09-02 06:11:42 +0000257struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000258 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000259 // Verify the "strcmp" function prototype.
260 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000261 if (FT->getNumParams() != 2 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000262 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000263 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000264 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000265 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000266
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000267 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
268 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000269 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000270
Bill Wendling0582ae92009-03-13 04:39:26 +0000271 std::string Str1, Str2;
272 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
273 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000274
Bill Wendling0582ae92009-03-13 04:39:26 +0000275 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000276 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000277
Bill Wendling0582ae92009-03-13 04:39:26 +0000278 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000279 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000280
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000281 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000282 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000283 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000284 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000285
286 // strcmp(P, "x") -> memcmp(P, "x", 2)
287 uint64_t Len1 = GetStringLength(Str1P);
288 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000289 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000290 // These optimizations require TargetData.
291 if (!TD) return 0;
292
Nick Lewycky13a09e22008-12-21 00:19:21 +0000293 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000294 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000295 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000296 }
297
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000298 return 0;
299 }
300};
301
302//===---------------------------------------===//
303// 'strncmp' Optimizations
304
Chris Lattner3e8b6632009-09-02 06:11:42 +0000305struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000306 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000307 // Verify the "strncmp" function prototype.
308 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000309 if (FT->getNumParams() != 3 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000310 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000311 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000312 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000313 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000314 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000315
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000316 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
317 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000318 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000319
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000320 // Get the length argument if it is constant.
321 uint64_t Length;
322 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
323 Length = LengthArg->getZExtValue();
324 else
325 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000326
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000327 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000328 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000329
Bill Wendling0582ae92009-03-13 04:39:26 +0000330 std::string Str1, Str2;
331 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
332 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000333
Bill Wendling0582ae92009-03-13 04:39:26 +0000334 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000335 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000336
Bill Wendling0582ae92009-03-13 04:39:26 +0000337 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000338 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000339
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000340 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000341 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000342 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000343 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000344 return 0;
345 }
346};
347
348
349//===---------------------------------------===//
350// 'strcpy' Optimizations
351
Chris Lattner3e8b6632009-09-02 06:11:42 +0000352struct StrCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000353 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000354 // Verify the "strcpy" function prototype.
355 const FunctionType *FT = Callee->getFunctionType();
356 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
357 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000358 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000359 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000360
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000361 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
362 if (Dst == Src) // strcpy(x,x) -> x
363 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000364
Dan Gohmanf14d9192009-08-18 00:48:13 +0000365 // These optimizations require TargetData.
366 if (!TD) return 0;
367
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000368 // See if we can get the length of the input string.
369 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000370 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000371
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000372 // We have enough information to now generate the memcpy call to do the
373 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000374 EmitMemCpy(Dst, Src,
Eric Christopherb6174e32010-03-05 22:25:30 +0000375 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000376 return Dst;
377 }
378};
379
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000380//===---------------------------------------===//
381// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000382
Chris Lattner3e8b6632009-09-02 06:11:42 +0000383struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000384 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
385 const FunctionType *FT = Callee->getFunctionType();
386 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
387 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000388 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000389 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000390 return 0;
391
392 Value *Dst = CI->getOperand(1);
393 Value *Src = CI->getOperand(2);
394 Value *LenOp = CI->getOperand(3);
395
396 // See if we can get the length of the input string.
397 uint64_t SrcLen = GetStringLength(Src);
398 if (SrcLen == 0) return 0;
399 --SrcLen;
400
401 if (SrcLen == 0) {
402 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000403 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
Eric Christopherb6174e32010-03-05 22:25:30 +0000404 B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000405 return Dst;
406 }
407
408 uint64_t Len;
409 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
410 Len = LengthArg->getZExtValue();
411 else
412 return 0;
413
414 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
415
Dan Gohmanf14d9192009-08-18 00:48:13 +0000416 // These optimizations require TargetData.
417 if (!TD) return 0;
418
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000419 // Let strncpy handle the zero padding
420 if (Len > SrcLen+1) return 0;
421
422 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000423 EmitMemCpy(Dst, Src,
Eric Christopherb6174e32010-03-05 22:25:30 +0000424 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000425
426 return Dst;
427 }
428};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000429
430//===---------------------------------------===//
431// 'strlen' Optimizations
432
Chris Lattner3e8b6632009-09-02 06:11:42 +0000433struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000434 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000435 const FunctionType *FT = Callee->getFunctionType();
436 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000437 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000438 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000439 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000440
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000441 Value *Src = CI->getOperand(1);
442
443 // Constant folding: strlen("xyz") -> 3
444 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000445 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000446
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000447 // strlen(x) != 0 --> *x != 0
448 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000449 if (IsOnlyUsedInZeroEqualityComparison(CI))
450 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
451 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000452 }
453};
454
455//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000456// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000457
Chris Lattner3e8b6632009-09-02 06:11:42 +0000458struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000459 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
460 const FunctionType *FT = Callee->getFunctionType();
461 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000462 !FT->getParamType(0)->isPointerTy() ||
463 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000464 return 0;
465
466 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000467 if (isa<ConstantPointerNull>(EndPtr)) {
468 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000469 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000470 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000471
472 return 0;
473 }
474};
475
Chris Lattner24604112009-12-16 09:32:05 +0000476//===---------------------------------------===//
477// 'strstr' Optimizations
478
479struct StrStrOpt : public LibCallOptimization {
480 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
481 const FunctionType *FT = Callee->getFunctionType();
482 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000483 !FT->getParamType(0)->isPointerTy() ||
484 !FT->getParamType(1)->isPointerTy() ||
485 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000486 return 0;
487
488 // fold strstr(x, x) -> x.
489 if (CI->getOperand(1) == CI->getOperand(2))
490 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000491
Chris Lattner24604112009-12-16 09:32:05 +0000492 // See if either input string is a constant string.
493 std::string SearchStr, ToFindStr;
494 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
495 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000496
Chris Lattner24604112009-12-16 09:32:05 +0000497 // fold strstr(x, "") -> x.
498 if (HasStr2 && ToFindStr.empty())
499 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000500
Chris Lattner24604112009-12-16 09:32:05 +0000501 // If both strings are known, constant fold it.
502 if (HasStr1 && HasStr2) {
503 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000504
Chris Lattner24604112009-12-16 09:32:05 +0000505 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
506 return Constant::getNullValue(CI->getType());
507
508 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
509 Value *Result = CastToCStr(CI->getOperand(1), B);
510 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
511 return B.CreateBitCast(Result, CI->getType());
512 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000513
Chris Lattner24604112009-12-16 09:32:05 +0000514 // fold strstr(x, "y") -> strchr(x, 'y').
515 if (HasStr2 && ToFindStr.size() == 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000516 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B, TD),
Chris Lattner24604112009-12-16 09:32:05 +0000517 CI->getType());
518 return 0;
519 }
520};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000521
Nick Lewycky4c498412009-02-13 15:31:46 +0000522
523//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000524// 'memcmp' Optimizations
525
Chris Lattner3e8b6632009-09-02 06:11:42 +0000526struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000527 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000528 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000529 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
530 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000531 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000532 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000533
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000534 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000535
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000536 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000537 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000538
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000539 // Make sure we have a constant length.
540 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000541 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000542 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000543
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000544 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000545 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000546
547 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
548 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
549 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +0000550 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000551 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000552
Benjamin Kramer992a6372009-11-05 17:44:22 +0000553 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
554 std::string LHSStr, RHSStr;
555 if (GetConstantStringInfo(LHS, LHSStr) &&
556 GetConstantStringInfo(RHS, RHSStr)) {
557 // Make sure we're not reading out-of-bounds memory.
558 if (Len > LHSStr.length() || Len > RHSStr.length())
559 return 0;
560 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
561 return ConstantInt::get(CI->getType(), Ret);
562 }
563
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000564 return 0;
565 }
566};
567
568//===---------------------------------------===//
569// 'memcpy' Optimizations
570
Chris Lattner3e8b6632009-09-02 06:11:42 +0000571struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000572 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000573 // These optimizations require TargetData.
574 if (!TD) return 0;
575
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000576 const FunctionType *FT = Callee->getFunctionType();
577 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000578 !FT->getParamType(0)->isPointerTy() ||
579 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000580 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000581 return 0;
582
583 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000584 EmitMemCpy(CI->getOperand(1), CI->getOperand(2),
585 CI->getOperand(3), 1, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000586 return CI->getOperand(1);
587 }
588};
589
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000590//===---------------------------------------===//
591// 'memmove' Optimizations
592
Chris Lattner3e8b6632009-09-02 06:11:42 +0000593struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000594 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000595 // These optimizations require TargetData.
596 if (!TD) return 0;
597
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000598 const FunctionType *FT = Callee->getFunctionType();
599 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000600 !FT->getParamType(0)->isPointerTy() ||
601 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000602 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000603 return 0;
604
605 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000606 EmitMemMove(CI->getOperand(1), CI->getOperand(2),
607 CI->getOperand(3), 1, B, TD);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000608 return CI->getOperand(1);
609 }
610};
611
612//===---------------------------------------===//
613// 'memset' Optimizations
614
Chris Lattner3e8b6632009-09-02 06:11:42 +0000615struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000616 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000617 // These optimizations require TargetData.
618 if (!TD) return 0;
619
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000620 const FunctionType *FT = Callee->getFunctionType();
621 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000622 !FT->getParamType(0)->isPointerTy() ||
623 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000624 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000625 return 0;
626
627 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000628 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
629 false);
Eric Christopherb6174e32010-03-05 22:25:30 +0000630 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B, TD);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000631 return CI->getOperand(1);
632 }
633};
634
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000635//===----------------------------------------------------------------------===//
636// Math Library Optimizations
637//===----------------------------------------------------------------------===//
638
639//===---------------------------------------===//
640// 'pow*' Optimizations
641
Chris Lattner3e8b6632009-09-02 06:11:42 +0000642struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000643 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000644 const FunctionType *FT = Callee->getFunctionType();
645 // Just make sure this has 2 arguments of the same FP type, which match the
646 // result type.
647 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
648 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000649 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000650 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000651
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000652 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
653 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
654 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
655 return Op1C;
656 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000657 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000658 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000659
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000660 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
661 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000662
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000663 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000664 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000665
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000666 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000667 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
668 // This is faster than calling pow, and still handles negative zero
669 // and negative infinite correctly.
670 // TODO: In fast-math mode, this could be just sqrt(x).
671 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000672 Value *Inf = ConstantFP::getInfinity(CI->getType());
673 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000674 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
675 Callee->getAttributes());
676 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
677 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000678 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
679 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
680 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000681 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000682
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000683 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
684 return Op1;
685 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000686 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000687 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000688 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000689 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000690 return 0;
691 }
692};
693
694//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000695// 'exp2' Optimizations
696
Chris Lattner3e8b6632009-09-02 06:11:42 +0000697struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000698 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000699 const FunctionType *FT = Callee->getFunctionType();
700 // Just make sure this has 1 argument of FP type, which matches the
701 // result type.
702 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000703 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000704 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000705
Chris Lattnere818f772008-05-02 18:43:35 +0000706 Value *Op = CI->getOperand(1);
707 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
708 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
709 Value *LdExpArg = 0;
710 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
711 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000712 LdExpArg = B.CreateSExt(OpC->getOperand(0),
713 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000714 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
715 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000716 LdExpArg = B.CreateZExt(OpC->getOperand(0),
717 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000718 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000719
Chris Lattnere818f772008-05-02 18:43:35 +0000720 if (LdExpArg) {
721 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000722 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000723 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000724 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000725 Name = "ldexp";
726 else
727 Name = "ldexpl";
728
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000729 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000730 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000731 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000732
733 Module *M = Caller->getParent();
734 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000735 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000736 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000737 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
738 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
739 CI->setCallingConv(F->getCallingConv());
740
741 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000742 }
743 return 0;
744 }
745};
Chris Lattnere818f772008-05-02 18:43:35 +0000746
747//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000748// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
749
Chris Lattner3e8b6632009-09-02 06:11:42 +0000750struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000751 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000752 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000753 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
754 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000755 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000756
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000757 // If this is something like 'floor((double)floatval)', convert to floorf.
758 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000759 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000760 return 0;
761
762 // floor((double)floatval) -> (double)floorf(floatval)
763 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000764 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
765 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000766 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000767 }
768};
769
770//===----------------------------------------------------------------------===//
771// Integer Optimizations
772//===----------------------------------------------------------------------===//
773
774//===---------------------------------------===//
775// 'ffs*' Optimizations
776
Chris Lattner3e8b6632009-09-02 06:11:42 +0000777struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000778 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000779 const FunctionType *FT = Callee->getFunctionType();
780 // Just make sure this has 2 arguments of the same FP type, which match the
781 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000782 if (FT->getNumParams() != 1 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000783 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000784 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000785 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000786
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000787 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000788
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000789 // Constant fold.
790 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
791 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000792 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000793 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000794 CI->getValue().countTrailingZeros()+1);
795 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000796
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000797 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
798 const Type *ArgType = Op->getType();
799 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
800 Intrinsic::cttz, &ArgType, 1);
801 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000802 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000803 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000804
Owen Andersona7235ea2009-07-31 20:28:14 +0000805 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000806 return B.CreateSelect(Cond, V,
807 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000808 }
809};
810
811//===---------------------------------------===//
812// 'isdigit' Optimizations
813
Chris Lattner3e8b6632009-09-02 06:11:42 +0000814struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000815 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000816 const FunctionType *FT = Callee->getFunctionType();
817 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000818 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000819 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000820 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000821
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000822 // isdigit(c) -> (c-'0') <u 10
823 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000824 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000825 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000826 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000827 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000828 return B.CreateZExt(Op, CI->getType());
829 }
830};
831
832//===---------------------------------------===//
833// 'isascii' Optimizations
834
Chris Lattner3e8b6632009-09-02 06:11:42 +0000835struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000836 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000837 const FunctionType *FT = Callee->getFunctionType();
838 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000839 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000840 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000841 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000842
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000843 // isascii(c) -> c <u 128
844 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000845 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000846 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000847 return B.CreateZExt(Op, CI->getType());
848 }
849};
Eric Christopher37c8b862009-10-07 21:14:25 +0000850
Chris Lattner313f0e62008-06-09 08:26:51 +0000851//===---------------------------------------===//
852// 'abs', 'labs', 'llabs' Optimizations
853
Chris Lattner3e8b6632009-09-02 06:11:42 +0000854struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000855 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +0000856 const FunctionType *FT = Callee->getFunctionType();
857 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +0000858 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +0000859 FT->getParamType(0) != FT->getReturnType())
860 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000861
Chris Lattner313f0e62008-06-09 08:26:51 +0000862 // abs(x) -> x >s -1 ? x : -x
863 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000864 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +0000865 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +0000866 "ispos");
867 Value *Neg = B.CreateNeg(Op, "neg");
868 return B.CreateSelect(Pos, Op, Neg);
869 }
870};
Eric Christopher37c8b862009-10-07 21:14:25 +0000871
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000872
873//===---------------------------------------===//
874// 'toascii' Optimizations
875
Chris Lattner3e8b6632009-09-02 06:11:42 +0000876struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000877 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000878 const FunctionType *FT = Callee->getFunctionType();
879 // We require i32(i32)
880 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000881 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000882 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000883
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000884 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000885 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +0000886 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000887 }
888};
889
890//===----------------------------------------------------------------------===//
891// Formatting and IO Optimizations
892//===----------------------------------------------------------------------===//
893
894//===---------------------------------------===//
895// 'printf' Optimizations
896
Chris Lattner3e8b6632009-09-02 06:11:42 +0000897struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000898 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000899 // Require one fixed pointer argument and an integer/void result.
900 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000901 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
902 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000903 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000904 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000905
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000906 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000907 std::string FormatStr;
908 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
909 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000910
911 // Empty format string -> noop.
912 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +0000913 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000914 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000915
Chris Lattner74965f22009-11-09 04:57:04 +0000916 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
917 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000918 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +0000919 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000920 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +0000921 if (CI->use_empty()) return CI;
922 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000923 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000924
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000925 // printf("foo\n") --> puts("foo")
926 if (FormatStr[FormatStr.size()-1] == '\n' &&
927 FormatStr.find('%') == std::string::npos) { // no format characters.
928 // Create a string literal with no \n on it. We expect the constant merge
929 // pass to be run after this pass, to merge duplicate strings.
930 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000931 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +0000932 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
933 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +0000934 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000935 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000936 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000937 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000938
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000939 // Optimize specific format strings.
940 // printf("%c", chr) --> putchar(*(i8*)dst)
941 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
Duncan Sands1df98592010-02-16 11:11:14 +0000942 CI->getOperand(2)->getType()->isIntegerTy()) {
Eric Christopherb6174e32010-03-05 22:25:30 +0000943 Value *Res = EmitPutChar(CI->getOperand(2), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000944
Chris Lattner74965f22009-11-09 04:57:04 +0000945 if (CI->use_empty()) return CI;
946 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000947 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000948
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000949 // printf("%s\n", str) --> puts(str)
950 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
Duncan Sands1df98592010-02-16 11:11:14 +0000951 CI->getOperand(2)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000952 CI->use_empty()) {
Eric Christopherb6174e32010-03-05 22:25:30 +0000953 EmitPutS(CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000954 return CI;
955 }
956 return 0;
957 }
958};
959
960//===---------------------------------------===//
961// 'sprintf' Optimizations
962
Chris Lattner3e8b6632009-09-02 06:11:42 +0000963struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000964 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000965 // Require two fixed pointer arguments and an integer result.
966 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000967 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
968 !FT->getParamType(1)->isPointerTy() ||
969 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000970 return 0;
971
972 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000973 std::string FormatStr;
974 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
975 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000976
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000977 // If we just have a format string (nothing else crazy) transform it.
978 if (CI->getNumOperands() == 3) {
979 // Make sure there's no % in the constant array. We could try to handle
980 // %% -> % in the future if we cared.
981 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
982 if (FormatStr[i] == '%')
983 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +0000984
985 // These optimizations require TargetData.
986 if (!TD) return 0;
987
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000988 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
989 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Eric Christopherb6174e32010-03-05 22:25:30 +0000990 ConstantInt::get(TD->getIntPtrType(*Context),
991 FormatStr.size()+1), 1, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +0000992 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000993 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000994
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000995 // The remaining optimizations require the format string to be "%s" or "%c"
996 // and have an extra operand.
997 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
998 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000999
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001000 // Decode the second character of the format string.
1001 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001002 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Duncan Sands1df98592010-02-16 11:11:14 +00001003 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001004 Value *V = B.CreateTrunc(CI->getOperand(3),
1005 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001006 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1007 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001008 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1009 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001010 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001011
Owen Andersoneed707b2009-07-24 23:12:02 +00001012 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001013 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001014
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001015 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001016 // These optimizations require TargetData.
1017 if (!TD) return 0;
1018
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001019 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Duncan Sands1df98592010-02-16 11:11:14 +00001020 if (!CI->getOperand(3)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001021
Eric Christopherb6174e32010-03-05 22:25:30 +00001022 Value *Len = EmitStrLen(CI->getOperand(3), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001023 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001024 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001025 "leninc");
Eric Christopherb6174e32010-03-05 22:25:30 +00001026 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001027
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001028 // The sprintf result is the unincremented number of bytes in the string.
1029 return B.CreateIntCast(Len, CI->getType(), false);
1030 }
1031 return 0;
1032 }
1033};
1034
1035//===---------------------------------------===//
1036// 'fwrite' Optimizations
1037
Chris Lattner3e8b6632009-09-02 06:11:42 +00001038struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001039 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001040 // Require a pointer, an integer, an integer, a pointer, returning integer.
1041 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001042 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1043 !FT->getParamType(1)->isIntegerTy() ||
1044 !FT->getParamType(2)->isIntegerTy() ||
1045 !FT->getParamType(3)->isPointerTy() ||
1046 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001047 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001048
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001049 // Get the element size and count.
1050 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1051 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1052 if (!SizeC || !CountC) return 0;
1053 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001054
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001055 // If this is writing zero records, remove the call (it's a noop).
1056 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001057 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001058
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001059 // If this is writing one byte, turn it into fputc.
1060 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1061 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
Eric Christopherb6174e32010-03-05 22:25:30 +00001062 EmitFPutC(Char, CI->getOperand(4), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001063 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001064 }
1065
1066 return 0;
1067 }
1068};
1069
1070//===---------------------------------------===//
1071// 'fputs' Optimizations
1072
Chris Lattner3e8b6632009-09-02 06:11:42 +00001073struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001074 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001075 // These optimizations require TargetData.
1076 if (!TD) return 0;
1077
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001078 // Require two pointers. Also, we can't optimize if return value is used.
1079 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001080 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1081 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001082 !CI->use_empty())
1083 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001084
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001085 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1086 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001087 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001088 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001089 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Eric Christopherb6174e32010-03-05 22:25:30 +00001090 CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001091 return CI; // Known to have no uses (see above).
1092 }
1093};
1094
1095//===---------------------------------------===//
1096// 'fprintf' Optimizations
1097
Chris Lattner3e8b6632009-09-02 06:11:42 +00001098struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001099 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001100 // Require two fixed paramters as pointers and integer result.
1101 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001102 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1103 !FT->getParamType(1)->isPointerTy() ||
1104 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001105 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001106
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001107 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001108 std::string FormatStr;
1109 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1110 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001111
1112 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1113 if (CI->getNumOperands() == 3) {
1114 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1115 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001116 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001117
1118 // These optimizations require TargetData.
1119 if (!TD) return 0;
1120
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001121 EmitFWrite(CI->getOperand(2),
1122 ConstantInt::get(TD->getIntPtrType(*Context),
1123 FormatStr.size()),
Eric Christopherb6174e32010-03-05 22:25:30 +00001124 CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001125 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001126 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001127
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001128 // The remaining optimizations require the format string to be "%s" or "%c"
1129 // and have an extra operand.
1130 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1131 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001132
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001133 // Decode the second character of the format string.
1134 if (FormatStr[1] == 'c') {
1135 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
Duncan Sands1df98592010-02-16 11:11:14 +00001136 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
Eric Christopherb6174e32010-03-05 22:25:30 +00001137 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001138 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001139 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001140
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001141 if (FormatStr[1] == 's') {
1142 // fprintf(F, "%s", str) -> fputs(str, F)
Duncan Sands1df98592010-02-16 11:11:14 +00001143 if (!CI->getOperand(3)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001144 return 0;
Eric Christopherb6174e32010-03-05 22:25:30 +00001145 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001146 return CI;
1147 }
1148 return 0;
1149 }
1150};
1151
Bill Wendlingac178222008-05-05 21:37:59 +00001152} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001153
1154//===----------------------------------------------------------------------===//
1155// SimplifyLibCalls Pass Implementation
1156//===----------------------------------------------------------------------===//
1157
1158namespace {
1159 /// This pass optimizes well known library functions from libc and libm.
1160 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001161 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001162 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001163 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001164 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1165 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001166 StrToOpt StrTo; StrStrOpt StrStr;
1167 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001168 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001169 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001170 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001171 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1172 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001173 // Formatting and IO Optimizations
1174 SPrintFOpt SPrintF; PrintFOpt PrintF;
1175 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001176
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001177 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001178 public:
1179 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001180 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001181
1182 void InitOptimizations();
1183 bool runOnFunction(Function &F);
1184
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001185 void setDoesNotAccessMemory(Function &F);
1186 void setOnlyReadsMemory(Function &F);
1187 void setDoesNotThrow(Function &F);
1188 void setDoesNotCapture(Function &F, unsigned n);
1189 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001190 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001191
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001192 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001193 }
1194 };
1195 char SimplifyLibCalls::ID = 0;
1196} // end anonymous namespace.
1197
1198static RegisterPass<SimplifyLibCalls>
1199X("simplify-libcalls", "Simplify well-known library calls");
1200
1201// Public interface to the Simplify LibCalls pass.
1202FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001203 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001204}
1205
1206/// Optimizations - Populate the Optimizations map with all the optimizations
1207/// we know.
1208void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001209 // String and Memory LibCall Optimizations
1210 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001211 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001212 Optimizations["strchr"] = &StrChr;
1213 Optimizations["strcmp"] = &StrCmp;
1214 Optimizations["strncmp"] = &StrNCmp;
1215 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001216 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001217 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001218 Optimizations["strtol"] = &StrTo;
1219 Optimizations["strtod"] = &StrTo;
1220 Optimizations["strtof"] = &StrTo;
1221 Optimizations["strtoul"] = &StrTo;
1222 Optimizations["strtoll"] = &StrTo;
1223 Optimizations["strtold"] = &StrTo;
1224 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001225 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001226 Optimizations["memcmp"] = &MemCmp;
1227 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001228 Optimizations["memmove"] = &MemMove;
1229 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001230
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001231 // Math Library Optimizations
1232 Optimizations["powf"] = &Pow;
1233 Optimizations["pow"] = &Pow;
1234 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001235 Optimizations["llvm.pow.f32"] = &Pow;
1236 Optimizations["llvm.pow.f64"] = &Pow;
1237 Optimizations["llvm.pow.f80"] = &Pow;
1238 Optimizations["llvm.pow.f128"] = &Pow;
1239 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001240 Optimizations["exp2l"] = &Exp2;
1241 Optimizations["exp2"] = &Exp2;
1242 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001243 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1244 Optimizations["llvm.exp2.f128"] = &Exp2;
1245 Optimizations["llvm.exp2.f80"] = &Exp2;
1246 Optimizations["llvm.exp2.f64"] = &Exp2;
1247 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001248
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001249#ifdef HAVE_FLOORF
1250 Optimizations["floor"] = &UnaryDoubleFP;
1251#endif
1252#ifdef HAVE_CEILF
1253 Optimizations["ceil"] = &UnaryDoubleFP;
1254#endif
1255#ifdef HAVE_ROUNDF
1256 Optimizations["round"] = &UnaryDoubleFP;
1257#endif
1258#ifdef HAVE_RINTF
1259 Optimizations["rint"] = &UnaryDoubleFP;
1260#endif
1261#ifdef HAVE_NEARBYINTF
1262 Optimizations["nearbyint"] = &UnaryDoubleFP;
1263#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001264
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001265 // Integer Optimizations
1266 Optimizations["ffs"] = &FFS;
1267 Optimizations["ffsl"] = &FFS;
1268 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001269 Optimizations["abs"] = &Abs;
1270 Optimizations["labs"] = &Abs;
1271 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001272 Optimizations["isdigit"] = &IsDigit;
1273 Optimizations["isascii"] = &IsAscii;
1274 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001275
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001276 // Formatting and IO Optimizations
1277 Optimizations["sprintf"] = &SPrintF;
1278 Optimizations["printf"] = &PrintF;
1279 Optimizations["fwrite"] = &FWrite;
1280 Optimizations["fputs"] = &FPuts;
1281 Optimizations["fprintf"] = &FPrintF;
1282}
1283
1284
1285/// runOnFunction - Top level algorithm.
1286///
1287bool SimplifyLibCalls::runOnFunction(Function &F) {
1288 if (Optimizations.empty())
1289 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001290
Dan Gohmanf14d9192009-08-18 00:48:13 +00001291 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001292
Owen Andersone922c022009-07-22 00:24:57 +00001293 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001294
1295 bool Changed = false;
1296 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1297 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1298 // Ignore non-calls.
1299 CallInst *CI = dyn_cast<CallInst>(I++);
1300 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001301
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001302 // Ignore indirect calls and calls to non-external functions.
1303 Function *Callee = CI->getCalledFunction();
1304 if (Callee == 0 || !Callee->isDeclaration() ||
1305 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1306 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001307
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001308 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001309 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1310 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001311
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001312 // Set the builder to the instruction after the call.
1313 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001314
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001315 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001316 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001317 if (Result == 0) continue;
1318
David Greene6a6b90e2010-01-05 01:27:21 +00001319 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1320 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001321
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001322 // Something changed!
1323 Changed = true;
1324 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001325
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001326 // Inspect the instruction after the call (which was potentially just
1327 // added) next.
1328 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001329
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001330 if (CI != Result && !CI->use_empty()) {
1331 CI->replaceAllUsesWith(Result);
1332 if (!Result->hasName())
1333 Result->takeName(CI);
1334 }
1335 CI->eraseFromParent();
1336 }
1337 }
1338 return Changed;
1339}
1340
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001341// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001342
1343void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1344 if (!F.doesNotAccessMemory()) {
1345 F.setDoesNotAccessMemory();
1346 ++NumAnnotated;
1347 Modified = true;
1348 }
1349}
1350void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1351 if (!F.onlyReadsMemory()) {
1352 F.setOnlyReadsMemory();
1353 ++NumAnnotated;
1354 Modified = true;
1355 }
1356}
1357void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1358 if (!F.doesNotThrow()) {
1359 F.setDoesNotThrow();
1360 ++NumAnnotated;
1361 Modified = true;
1362 }
1363}
1364void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1365 if (!F.doesNotCapture(n)) {
1366 F.setDoesNotCapture(n);
1367 ++NumAnnotated;
1368 Modified = true;
1369 }
1370}
1371void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1372 if (!F.doesNotAlias(n)) {
1373 F.setDoesNotAlias(n);
1374 ++NumAnnotated;
1375 Modified = true;
1376 }
1377}
1378
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001379/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001380///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001381bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001382 Modified = false;
1383 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1384 Function &F = *I;
1385 if (!F.isDeclaration())
1386 continue;
1387
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001388 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001389 continue;
1390
1391 const FunctionType *FTy = F.getFunctionType();
1392
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001393 StringRef Name = F.getName();
1394 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001395 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001396 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001397 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001398 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001399 continue;
1400 setOnlyReadsMemory(F);
1401 setDoesNotThrow(F);
1402 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001403 } else if (Name == "strchr" ||
1404 Name == "strrchr") {
1405 if (FTy->getNumParams() != 2 ||
1406 !FTy->getParamType(0)->isPointerTy() ||
1407 !FTy->getParamType(1)->isIntegerTy())
1408 continue;
1409 setOnlyReadsMemory(F);
1410 setDoesNotThrow(F);
1411 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001412 } else if (Name == "strcpy" ||
1413 Name == "stpcpy" ||
1414 Name == "strcat" ||
1415 Name == "strtol" ||
1416 Name == "strtod" ||
1417 Name == "strtof" ||
1418 Name == "strtoul" ||
1419 Name == "strtoll" ||
1420 Name == "strtold" ||
1421 Name == "strncat" ||
1422 Name == "strncpy" ||
1423 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001424 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001425 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001426 continue;
1427 setDoesNotThrow(F);
1428 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001429 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001430 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001431 !FTy->getParamType(0)->isPointerTy() ||
1432 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001433 continue;
1434 setDoesNotThrow(F);
1435 setDoesNotCapture(F, 1);
1436 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001437 } else if (Name == "strcmp" ||
1438 Name == "strspn" ||
1439 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001440 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001441 Name == "strcoll" ||
1442 Name == "strcasecmp" ||
1443 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001444 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001445 !FTy->getParamType(0)->isPointerTy() ||
1446 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001447 continue;
1448 setOnlyReadsMemory(F);
1449 setDoesNotThrow(F);
1450 setDoesNotCapture(F, 1);
1451 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001452 } else if (Name == "strstr" ||
1453 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001454 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001455 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001456 continue;
1457 setOnlyReadsMemory(F);
1458 setDoesNotThrow(F);
1459 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001460 } else if (Name == "strtok" ||
1461 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001462 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001463 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001464 continue;
1465 setDoesNotThrow(F);
1466 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001467 } else if (Name == "scanf" ||
1468 Name == "setbuf" ||
1469 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001470 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001471 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001472 continue;
1473 setDoesNotThrow(F);
1474 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001475 } else if (Name == "strdup" ||
1476 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001477 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001478 !FTy->getReturnType()->isPointerTy() ||
1479 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001480 continue;
1481 setDoesNotThrow(F);
1482 setDoesNotAlias(F, 0);
1483 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001484 } else if (Name == "stat" ||
1485 Name == "sscanf" ||
1486 Name == "sprintf" ||
1487 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001488 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001489 !FTy->getParamType(0)->isPointerTy() ||
1490 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001491 continue;
1492 setDoesNotThrow(F);
1493 setDoesNotCapture(F, 1);
1494 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001495 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001496 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001497 !FTy->getParamType(0)->isPointerTy() ||
1498 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001499 continue;
1500 setDoesNotThrow(F);
1501 setDoesNotCapture(F, 1);
1502 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001503 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001504 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001505 !FTy->getParamType(1)->isPointerTy() ||
1506 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001507 continue;
1508 setDoesNotThrow(F);
1509 setDoesNotCapture(F, 2);
1510 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001511 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001512 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001513 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001514 continue;
1515 // May throw; "system" is a valid pthread cancellation point.
1516 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001517 }
1518 break;
1519 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001520 if (Name == "malloc") {
1521 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001522 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001523 continue;
1524 setDoesNotThrow(F);
1525 setDoesNotAlias(F, 0);
1526 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001527 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001528 !FTy->getParamType(0)->isPointerTy() ||
1529 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001530 continue;
1531 setOnlyReadsMemory(F);
1532 setDoesNotThrow(F);
1533 setDoesNotCapture(F, 1);
1534 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001535 } else if (Name == "memchr" ||
1536 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001537 if (FTy->getNumParams() != 3)
1538 continue;
1539 setOnlyReadsMemory(F);
1540 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001541 } else if (Name == "modf" ||
1542 Name == "modff" ||
1543 Name == "modfl" ||
1544 Name == "memcpy" ||
1545 Name == "memccpy" ||
1546 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001547 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001548 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001549 continue;
1550 setDoesNotThrow(F);
1551 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001552 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001553 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001554 continue;
1555 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001556 } else if (Name == "mkdir" ||
1557 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001558 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001559 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001560 continue;
1561 setDoesNotThrow(F);
1562 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001563 }
1564 break;
1565 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001566 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001567 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001568 !FTy->getParamType(0)->isPointerTy() ||
1569 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001570 continue;
1571 setDoesNotThrow(F);
1572 setDoesNotAlias(F, 0);
1573 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001574 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001575 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001576 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001577 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001578 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001579 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001580 } else if (Name == "rmdir" ||
1581 Name == "rewind" ||
1582 Name == "remove" ||
1583 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001584 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001585 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001586 continue;
1587 setDoesNotThrow(F);
1588 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001589 } else if (Name == "rename" ||
1590 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001591 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001592 !FTy->getParamType(0)->isPointerTy() ||
1593 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001594 continue;
1595 setDoesNotThrow(F);
1596 setDoesNotCapture(F, 1);
1597 setDoesNotCapture(F, 2);
1598 }
1599 break;
1600 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001601 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001602 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001603 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001604 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001605 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001606 setDoesNotCapture(F, 2);
1607 }
1608 break;
1609 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001610 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001611 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001612 !FTy->getParamType(0)->isPointerTy() ||
1613 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001614 continue;
1615 setDoesNotThrow(F);
1616 setDoesNotCapture(F, 1);
1617 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001618 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001619 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001620 !FTy->getParamType(0)->isPointerTy() ||
1621 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001622 continue;
1623 setDoesNotThrow(F);
1624 setOnlyReadsMemory(F);
1625 setDoesNotCapture(F, 1);
1626 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001627 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001628 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001629 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001630 continue;
1631 setDoesNotThrow(F);
1632 setDoesNotCapture(F, 1);
1633 }
1634 break;
1635 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001636 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001637 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001638 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001639 continue;
1640 setDoesNotThrow(F);
1641 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001642 } else if (Name == "chmod" ||
1643 Name == "chown" ||
1644 Name == "ctermid" ||
1645 Name == "clearerr" ||
1646 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001647 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001648 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001649 continue;
1650 setDoesNotThrow(F);
1651 setDoesNotCapture(F, 1);
1652 }
1653 break;
1654 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001655 if (Name == "atoi" ||
1656 Name == "atol" ||
1657 Name == "atof" ||
1658 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001659 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001660 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001661 continue;
1662 setDoesNotThrow(F);
1663 setOnlyReadsMemory(F);
1664 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001665 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001666 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001667 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001668 continue;
1669 setDoesNotThrow(F);
1670 setDoesNotCapture(F, 1);
1671 }
1672 break;
1673 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001674 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001675 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001676 !FTy->getReturnType()->isPointerTy() ||
1677 !FTy->getParamType(0)->isPointerTy() ||
1678 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001679 continue;
1680 setDoesNotThrow(F);
1681 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001682 setDoesNotCapture(F, 1);
1683 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001684 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001685 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001686 !FTy->getReturnType()->isPointerTy() ||
1687 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001688 continue;
1689 setDoesNotThrow(F);
1690 setDoesNotAlias(F, 0);
1691 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001692 } else if (Name == "feof" ||
1693 Name == "free" ||
1694 Name == "fseek" ||
1695 Name == "ftell" ||
1696 Name == "fgetc" ||
1697 Name == "fseeko" ||
1698 Name == "ftello" ||
1699 Name == "fileno" ||
1700 Name == "fflush" ||
1701 Name == "fclose" ||
1702 Name == "fsetpos" ||
1703 Name == "flockfile" ||
1704 Name == "funlockfile" ||
1705 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001706 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001707 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001708 continue;
1709 setDoesNotThrow(F);
1710 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001711 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001712 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001713 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001714 continue;
1715 setDoesNotThrow(F);
1716 setDoesNotCapture(F, 1);
1717 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001718 } else if (Name == "fputc" ||
1719 Name == "fstat" ||
1720 Name == "frexp" ||
1721 Name == "frexpf" ||
1722 Name == "frexpl" ||
1723 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001724 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001725 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001726 continue;
1727 setDoesNotThrow(F);
1728 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001729 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001730 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001731 !FTy->getParamType(0)->isPointerTy() ||
1732 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001733 continue;
1734 setDoesNotThrow(F);
1735 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001736 } else if (Name == "fread" ||
1737 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001738 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001739 !FTy->getParamType(0)->isPointerTy() ||
1740 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001741 continue;
1742 setDoesNotThrow(F);
1743 setDoesNotCapture(F, 1);
1744 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001745 } else if (Name == "fputs" ||
1746 Name == "fscanf" ||
1747 Name == "fprintf" ||
1748 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001749 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001750 !FTy->getParamType(0)->isPointerTy() ||
1751 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001752 continue;
1753 setDoesNotThrow(F);
1754 setDoesNotCapture(F, 1);
1755 setDoesNotCapture(F, 2);
1756 }
1757 break;
1758 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001759 if (Name == "getc" ||
1760 Name == "getlogin_r" ||
1761 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001762 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001763 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001764 continue;
1765 setDoesNotThrow(F);
1766 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001767 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001768 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001769 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001770 continue;
1771 setDoesNotThrow(F);
1772 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001773 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001774 } else if (Name == "gets" ||
1775 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001776 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001777 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001778 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001779 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001780 continue;
1781 setDoesNotThrow(F);
1782 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001783 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001784 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001785 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001786 continue;
1787 setDoesNotThrow(F);
1788 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001789 }
1790 break;
1791 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001792 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001793 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001794 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001795 continue;
1796 setDoesNotThrow(F);
1797 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001798 } else if (Name == "uname" ||
1799 Name == "unlink" ||
1800 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001801 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001802 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001803 continue;
1804 setDoesNotThrow(F);
1805 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001806 } else if (Name == "utime" ||
1807 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001808 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001809 !FTy->getParamType(0)->isPointerTy() ||
1810 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001811 continue;
1812 setDoesNotThrow(F);
1813 setDoesNotCapture(F, 1);
1814 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001815 }
1816 break;
1817 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001818 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001819 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001820 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001821 continue;
1822 setDoesNotThrow(F);
1823 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001824 } else if (Name == "puts" ||
1825 Name == "printf" ||
1826 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001827 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001828 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001829 continue;
1830 setDoesNotThrow(F);
1831 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001832 } else if (Name == "pread" ||
1833 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001834 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001835 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001836 continue;
1837 // May throw; these are valid pthread cancellation points.
1838 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001839 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001840 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001841 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001842 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001843 !FTy->getReturnType()->isPointerTy() ||
1844 !FTy->getParamType(0)->isPointerTy() ||
1845 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001846 continue;
1847 setDoesNotThrow(F);
1848 setDoesNotAlias(F, 0);
1849 setDoesNotCapture(F, 1);
1850 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001851 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001852 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001853 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001854 continue;
1855 setDoesNotThrow(F);
1856 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001857 }
1858 break;
1859 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001860 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001861 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001862 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001863 continue;
1864 setDoesNotThrow(F);
1865 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001866 } else if (Name == "vsscanf" ||
1867 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001868 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001869 !FTy->getParamType(1)->isPointerTy() ||
1870 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001871 continue;
1872 setDoesNotThrow(F);
1873 setDoesNotCapture(F, 1);
1874 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001875 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00001876 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001877 continue;
1878 setDoesNotThrow(F);
1879 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001880 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001881 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001882 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001883 continue;
1884 setDoesNotThrow(F);
1885 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001886 } else if (Name == "vfprintf" ||
1887 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001888 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001889 !FTy->getParamType(0)->isPointerTy() ||
1890 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001891 continue;
1892 setDoesNotThrow(F);
1893 setDoesNotCapture(F, 1);
1894 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001895 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001896 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001897 !FTy->getParamType(0)->isPointerTy() ||
1898 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001899 continue;
1900 setDoesNotThrow(F);
1901 setDoesNotCapture(F, 1);
1902 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001903 }
1904 break;
1905 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001906 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001907 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001908 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001909 continue;
1910 // May throw; "open" is a valid pthread cancellation point.
1911 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001912 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001913 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001914 !FTy->getReturnType()->isPointerTy() ||
1915 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001916 continue;
1917 setDoesNotThrow(F);
1918 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00001919 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001920 }
1921 break;
1922 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001923 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00001924 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001925 continue;
1926 setDoesNotThrow(F);
1927 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001928 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001929 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001930 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001931 continue;
1932 setDoesNotThrow(F);
1933 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001934 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001935 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001936 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001937 if (Name == "htonl" ||
1938 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001939 setDoesNotThrow(F);
1940 setDoesNotAccessMemory(F);
1941 }
1942 break;
1943 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001944 if (Name == "ntohl" ||
1945 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001946 setDoesNotThrow(F);
1947 setDoesNotAccessMemory(F);
1948 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001949 break;
1950 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001951 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001952 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001953 !FTy->getParamType(0)->isPointerTy() ||
1954 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001955 continue;
1956 setDoesNotThrow(F);
1957 setDoesNotCapture(F, 1);
1958 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001959 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001960 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001961 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001962 continue;
1963 setDoesNotThrow(F);
1964 setDoesNotCapture(F, 1);
1965 }
1966 break;
1967 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001968 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001969 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001970 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001971 continue;
1972 // May throw; places call through function pointer.
1973 setDoesNotCapture(F, 4);
1974 }
1975 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001976 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001977 if (Name == "__strdup" ||
1978 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001979 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001980 !FTy->getReturnType()->isPointerTy() ||
1981 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001982 continue;
1983 setDoesNotThrow(F);
1984 setDoesNotAlias(F, 0);
1985 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001986 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001987 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001988 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001989 continue;
1990 setDoesNotThrow(F);
1991 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001992 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001993 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001994 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001995 continue;
1996 setDoesNotThrow(F);
1997 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001998 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001999 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002000 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002001 continue;
2002 setDoesNotThrow(F);
2003 setDoesNotCapture(F, 2);
2004 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002005 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002006 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002007 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002008 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002009 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002010 continue;
2011 setDoesNotThrow(F);
2012 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002013 } else if (Name == "\1stat64" ||
2014 Name == "\1lstat64" ||
2015 Name == "\1statvfs64" ||
2016 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002017 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002018 !FTy->getParamType(0)->isPointerTy() ||
2019 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002020 continue;
2021 setDoesNotThrow(F);
2022 setDoesNotCapture(F, 1);
2023 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002024 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002025 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002026 !FTy->getReturnType()->isPointerTy() ||
2027 !FTy->getParamType(0)->isPointerTy() ||
2028 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002029 continue;
2030 setDoesNotThrow(F);
2031 setDoesNotAlias(F, 0);
2032 setDoesNotCapture(F, 1);
2033 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002034 } else if (Name == "\1fseeko64" ||
2035 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002036 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002037 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002038 continue;
2039 setDoesNotThrow(F);
2040 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002041 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002042 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002043 continue;
2044 setDoesNotThrow(F);
2045 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002046 } else if (Name == "\1fstat64" ||
2047 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002048 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002049 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002050 continue;
2051 setDoesNotThrow(F);
2052 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002053 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002054 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002055 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002056 continue;
2057 // May throw; "open" is a valid pthread cancellation point.
2058 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002059 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002060 break;
2061 }
2062 }
2063 return Modified;
2064}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002065
2066// TODO:
2067// Additional cases that we need to add to this file:
2068//
2069// cbrt:
2070// * cbrt(expN(X)) -> expN(x/3)
2071// * cbrt(sqrt(x)) -> pow(x,1/6)
2072// * cbrt(sqrt(x)) -> pow(x,1/9)
2073//
2074// cos, cosf, cosl:
2075// * cos(-x) -> cos(x)
2076//
2077// exp, expf, expl:
2078// * exp(log(x)) -> x
2079//
2080// log, logf, logl:
2081// * log(exp(x)) -> x
2082// * log(x**y) -> y*log(x)
2083// * log(exp(y)) -> y*log(e)
2084// * log(exp2(y)) -> y*log(2)
2085// * log(exp10(y)) -> y*log(10)
2086// * log(sqrt(x)) -> 0.5*log(x)
2087// * log(pow(x,y)) -> y*log(x)
2088//
2089// lround, lroundf, lroundl:
2090// * lround(cnst) -> cnst'
2091//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002092// pow, powf, powl:
2093// * pow(exp(x),y) -> exp(x*y)
2094// * pow(sqrt(x),y) -> pow(x,y*0.5)
2095// * pow(pow(x,y),z)-> pow(x,y*z)
2096//
2097// puts:
2098// * puts("") -> putchar("\n")
2099//
2100// round, roundf, roundl:
2101// * round(cnst) -> cnst'
2102//
2103// signbit:
2104// * signbit(cnst) -> cnst'
2105// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2106//
2107// sqrt, sqrtf, sqrtl:
2108// * sqrt(expN(x)) -> expN(x*0.5)
2109// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2110// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2111//
2112// stpcpy:
2113// * stpcpy(str, "literal") ->
2114// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2115// strrchr:
2116// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2117// (if c is a constant integer and s is a constant string)
2118// * strrchr(s1,0) -> strchr(s1,0)
2119//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002120// strpbrk:
2121// * strpbrk(s,a) -> offset_in_for(s,a)
2122// (if s and a are both constant strings)
2123// * strpbrk(s,"") -> 0
2124// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2125//
2126// strspn, strcspn:
2127// * strspn(s,a) -> const_int (if both args are constant)
2128// * strspn("",a) -> 0
2129// * strspn(s,"") -> 0
2130// * strcspn(s,a) -> const_int (if both args are constant)
2131// * strcspn("",a) -> 0
2132// * strcspn(s,"") -> strlen(a)
2133//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002134// tan, tanf, tanl:
2135// * tan(atan(x)) -> x
2136//
2137// trunc, truncf, truncl:
2138// * trunc(cnst) -> cnst'
2139//
2140//