blob: 05027ae528e3c741f55fef8efbe20094a0a2fcc9 [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);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001403 } else if (Name == "strcpy" ||
1404 Name == "stpcpy" ||
1405 Name == "strcat" ||
1406 Name == "strtol" ||
1407 Name == "strtod" ||
1408 Name == "strtof" ||
1409 Name == "strtoul" ||
1410 Name == "strtoll" ||
1411 Name == "strtold" ||
1412 Name == "strncat" ||
1413 Name == "strncpy" ||
1414 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001415 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001416 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001417 continue;
1418 setDoesNotThrow(F);
1419 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001420 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001421 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001422 !FTy->getParamType(0)->isPointerTy() ||
1423 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001424 continue;
1425 setDoesNotThrow(F);
1426 setDoesNotCapture(F, 1);
1427 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001428 } else if (Name == "strcmp" ||
1429 Name == "strspn" ||
1430 Name == "strncmp" ||
1431 Name ==" strcspn" ||
1432 Name == "strcoll" ||
1433 Name == "strcasecmp" ||
1434 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001435 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001436 !FTy->getParamType(0)->isPointerTy() ||
1437 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001438 continue;
1439 setOnlyReadsMemory(F);
1440 setDoesNotThrow(F);
1441 setDoesNotCapture(F, 1);
1442 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001443 } else if (Name == "strstr" ||
1444 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001445 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001446 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001447 continue;
1448 setOnlyReadsMemory(F);
1449 setDoesNotThrow(F);
1450 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001451 } else if (Name == "strtok" ||
1452 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001453 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001454 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001455 continue;
1456 setDoesNotThrow(F);
1457 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001458 } else if (Name == "scanf" ||
1459 Name == "setbuf" ||
1460 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001461 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001462 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001463 continue;
1464 setDoesNotThrow(F);
1465 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001466 } else if (Name == "strdup" ||
1467 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001468 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001469 !FTy->getReturnType()->isPointerTy() ||
1470 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001471 continue;
1472 setDoesNotThrow(F);
1473 setDoesNotAlias(F, 0);
1474 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001475 } else if (Name == "stat" ||
1476 Name == "sscanf" ||
1477 Name == "sprintf" ||
1478 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001479 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001480 !FTy->getParamType(0)->isPointerTy() ||
1481 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001482 continue;
1483 setDoesNotThrow(F);
1484 setDoesNotCapture(F, 1);
1485 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001486 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001487 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001488 !FTy->getParamType(0)->isPointerTy() ||
1489 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001490 continue;
1491 setDoesNotThrow(F);
1492 setDoesNotCapture(F, 1);
1493 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001494 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001495 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001496 !FTy->getParamType(1)->isPointerTy() ||
1497 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001498 continue;
1499 setDoesNotThrow(F);
1500 setDoesNotCapture(F, 2);
1501 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001502 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001503 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001504 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001505 continue;
1506 // May throw; "system" is a valid pthread cancellation point.
1507 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001508 }
1509 break;
1510 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001511 if (Name == "malloc") {
1512 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001513 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001514 continue;
1515 setDoesNotThrow(F);
1516 setDoesNotAlias(F, 0);
1517 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001518 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001519 !FTy->getParamType(0)->isPointerTy() ||
1520 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001521 continue;
1522 setOnlyReadsMemory(F);
1523 setDoesNotThrow(F);
1524 setDoesNotCapture(F, 1);
1525 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001526 } else if (Name == "memchr" ||
1527 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001528 if (FTy->getNumParams() != 3)
1529 continue;
1530 setOnlyReadsMemory(F);
1531 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001532 } else if (Name == "modf" ||
1533 Name == "modff" ||
1534 Name == "modfl" ||
1535 Name == "memcpy" ||
1536 Name == "memccpy" ||
1537 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001538 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001539 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001540 continue;
1541 setDoesNotThrow(F);
1542 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001543 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001544 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001545 continue;
1546 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001547 } else if (Name == "mkdir" ||
1548 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001549 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001550 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001551 continue;
1552 setDoesNotThrow(F);
1553 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001554 }
1555 break;
1556 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001557 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001558 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001559 !FTy->getParamType(0)->isPointerTy() ||
1560 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001561 continue;
1562 setDoesNotThrow(F);
1563 setDoesNotAlias(F, 0);
1564 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001565 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001566 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001567 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001568 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001569 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001570 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001571 } else if (Name == "rmdir" ||
1572 Name == "rewind" ||
1573 Name == "remove" ||
1574 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001575 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001576 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001577 continue;
1578 setDoesNotThrow(F);
1579 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001580 } else if (Name == "rename" ||
1581 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001582 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001583 !FTy->getParamType(0)->isPointerTy() ||
1584 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001585 continue;
1586 setDoesNotThrow(F);
1587 setDoesNotCapture(F, 1);
1588 setDoesNotCapture(F, 2);
1589 }
1590 break;
1591 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001592 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001593 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001594 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001595 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001596 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001597 setDoesNotCapture(F, 2);
1598 }
1599 break;
1600 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001601 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001602 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001603 !FTy->getParamType(0)->isPointerTy() ||
1604 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001605 continue;
1606 setDoesNotThrow(F);
1607 setDoesNotCapture(F, 1);
1608 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001609 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001610 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001611 !FTy->getParamType(0)->isPointerTy() ||
1612 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001613 continue;
1614 setDoesNotThrow(F);
1615 setOnlyReadsMemory(F);
1616 setDoesNotCapture(F, 1);
1617 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001618 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001619 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001620 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001621 continue;
1622 setDoesNotThrow(F);
1623 setDoesNotCapture(F, 1);
1624 }
1625 break;
1626 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001627 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001628 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001629 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001630 continue;
1631 setDoesNotThrow(F);
1632 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001633 } else if (Name == "chmod" ||
1634 Name == "chown" ||
1635 Name == "ctermid" ||
1636 Name == "clearerr" ||
1637 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001638 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001639 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001640 continue;
1641 setDoesNotThrow(F);
1642 setDoesNotCapture(F, 1);
1643 }
1644 break;
1645 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001646 if (Name == "atoi" ||
1647 Name == "atol" ||
1648 Name == "atof" ||
1649 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001650 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001651 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001652 continue;
1653 setDoesNotThrow(F);
1654 setOnlyReadsMemory(F);
1655 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001656 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001657 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001658 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001659 continue;
1660 setDoesNotThrow(F);
1661 setDoesNotCapture(F, 1);
1662 }
1663 break;
1664 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001665 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001666 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001667 !FTy->getReturnType()->isPointerTy() ||
1668 !FTy->getParamType(0)->isPointerTy() ||
1669 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001670 continue;
1671 setDoesNotThrow(F);
1672 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001673 setDoesNotCapture(F, 1);
1674 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001675 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001676 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001677 !FTy->getReturnType()->isPointerTy() ||
1678 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001679 continue;
1680 setDoesNotThrow(F);
1681 setDoesNotAlias(F, 0);
1682 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001683 } else if (Name == "feof" ||
1684 Name == "free" ||
1685 Name == "fseek" ||
1686 Name == "ftell" ||
1687 Name == "fgetc" ||
1688 Name == "fseeko" ||
1689 Name == "ftello" ||
1690 Name == "fileno" ||
1691 Name == "fflush" ||
1692 Name == "fclose" ||
1693 Name == "fsetpos" ||
1694 Name == "flockfile" ||
1695 Name == "funlockfile" ||
1696 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001697 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001698 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001699 continue;
1700 setDoesNotThrow(F);
1701 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001702 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001703 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001704 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001705 continue;
1706 setDoesNotThrow(F);
1707 setDoesNotCapture(F, 1);
1708 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001709 } else if (Name == "fputc" ||
1710 Name == "fstat" ||
1711 Name == "frexp" ||
1712 Name == "frexpf" ||
1713 Name == "frexpl" ||
1714 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001715 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001716 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001717 continue;
1718 setDoesNotThrow(F);
1719 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001720 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001721 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001722 !FTy->getParamType(0)->isPointerTy() ||
1723 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001724 continue;
1725 setDoesNotThrow(F);
1726 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001727 } else if (Name == "fread" ||
1728 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001729 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001730 !FTy->getParamType(0)->isPointerTy() ||
1731 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001732 continue;
1733 setDoesNotThrow(F);
1734 setDoesNotCapture(F, 1);
1735 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001736 } else if (Name == "fputs" ||
1737 Name == "fscanf" ||
1738 Name == "fprintf" ||
1739 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001740 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001741 !FTy->getParamType(0)->isPointerTy() ||
1742 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001743 continue;
1744 setDoesNotThrow(F);
1745 setDoesNotCapture(F, 1);
1746 setDoesNotCapture(F, 2);
1747 }
1748 break;
1749 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001750 if (Name == "getc" ||
1751 Name == "getlogin_r" ||
1752 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001753 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001754 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001755 continue;
1756 setDoesNotThrow(F);
1757 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001758 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001759 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001760 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001761 continue;
1762 setDoesNotThrow(F);
1763 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001764 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001765 } else if (Name == "gets" ||
1766 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001767 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001768 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001769 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001770 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001771 continue;
1772 setDoesNotThrow(F);
1773 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001774 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001775 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001776 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001777 continue;
1778 setDoesNotThrow(F);
1779 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001780 }
1781 break;
1782 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001783 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001784 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001785 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001786 continue;
1787 setDoesNotThrow(F);
1788 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001789 } else if (Name == "uname" ||
1790 Name == "unlink" ||
1791 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001792 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001793 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001794 continue;
1795 setDoesNotThrow(F);
1796 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001797 } else if (Name == "utime" ||
1798 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001799 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001800 !FTy->getParamType(0)->isPointerTy() ||
1801 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001802 continue;
1803 setDoesNotThrow(F);
1804 setDoesNotCapture(F, 1);
1805 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001806 }
1807 break;
1808 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001809 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001810 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001811 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001812 continue;
1813 setDoesNotThrow(F);
1814 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001815 } else if (Name == "puts" ||
1816 Name == "printf" ||
1817 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001818 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001819 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001820 continue;
1821 setDoesNotThrow(F);
1822 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001823 } else if (Name == "pread" ||
1824 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001825 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001826 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001827 continue;
1828 // May throw; these are valid pthread cancellation points.
1829 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001830 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001831 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001832 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001833 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001834 !FTy->getReturnType()->isPointerTy() ||
1835 !FTy->getParamType(0)->isPointerTy() ||
1836 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001837 continue;
1838 setDoesNotThrow(F);
1839 setDoesNotAlias(F, 0);
1840 setDoesNotCapture(F, 1);
1841 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001842 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001843 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001844 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001845 continue;
1846 setDoesNotThrow(F);
1847 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001848 }
1849 break;
1850 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001851 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001852 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001853 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001854 continue;
1855 setDoesNotThrow(F);
1856 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001857 } else if (Name == "vsscanf" ||
1858 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001859 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001860 !FTy->getParamType(1)->isPointerTy() ||
1861 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001862 continue;
1863 setDoesNotThrow(F);
1864 setDoesNotCapture(F, 1);
1865 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001866 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00001867 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001868 continue;
1869 setDoesNotThrow(F);
1870 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001871 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001872 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001873 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001874 continue;
1875 setDoesNotThrow(F);
1876 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001877 } else if (Name == "vfprintf" ||
1878 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001879 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001880 !FTy->getParamType(0)->isPointerTy() ||
1881 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001882 continue;
1883 setDoesNotThrow(F);
1884 setDoesNotCapture(F, 1);
1885 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001886 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001887 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001888 !FTy->getParamType(0)->isPointerTy() ||
1889 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001890 continue;
1891 setDoesNotThrow(F);
1892 setDoesNotCapture(F, 1);
1893 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001894 }
1895 break;
1896 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001897 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001898 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001899 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001900 continue;
1901 // May throw; "open" is a valid pthread cancellation point.
1902 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001903 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001904 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001905 !FTy->getReturnType()->isPointerTy() ||
1906 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001907 continue;
1908 setDoesNotThrow(F);
1909 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00001910 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001911 }
1912 break;
1913 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001914 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00001915 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001916 continue;
1917 setDoesNotThrow(F);
1918 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001919 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001920 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001921 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001922 continue;
1923 setDoesNotThrow(F);
1924 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001925 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001926 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001927 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001928 if (Name == "htonl" ||
1929 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001930 setDoesNotThrow(F);
1931 setDoesNotAccessMemory(F);
1932 }
1933 break;
1934 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001935 if (Name == "ntohl" ||
1936 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001937 setDoesNotThrow(F);
1938 setDoesNotAccessMemory(F);
1939 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001940 break;
1941 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001942 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001943 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001944 !FTy->getParamType(0)->isPointerTy() ||
1945 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001946 continue;
1947 setDoesNotThrow(F);
1948 setDoesNotCapture(F, 1);
1949 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001950 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001951 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001952 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001953 continue;
1954 setDoesNotThrow(F);
1955 setDoesNotCapture(F, 1);
1956 }
1957 break;
1958 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001959 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001960 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001961 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001962 continue;
1963 // May throw; places call through function pointer.
1964 setDoesNotCapture(F, 4);
1965 }
1966 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001967 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001968 if (Name == "__strdup" ||
1969 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001970 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001971 !FTy->getReturnType()->isPointerTy() ||
1972 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001973 continue;
1974 setDoesNotThrow(F);
1975 setDoesNotAlias(F, 0);
1976 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001977 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001978 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001979 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001980 continue;
1981 setDoesNotThrow(F);
1982 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001983 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001984 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001985 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001986 continue;
1987 setDoesNotThrow(F);
1988 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001989 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001990 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001991 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001992 continue;
1993 setDoesNotThrow(F);
1994 setDoesNotCapture(F, 2);
1995 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001996 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001997 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001998 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001999 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002000 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002001 continue;
2002 setDoesNotThrow(F);
2003 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002004 } else if (Name == "\1stat64" ||
2005 Name == "\1lstat64" ||
2006 Name == "\1statvfs64" ||
2007 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002008 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002009 !FTy->getParamType(0)->isPointerTy() ||
2010 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002011 continue;
2012 setDoesNotThrow(F);
2013 setDoesNotCapture(F, 1);
2014 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002015 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002016 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002017 !FTy->getReturnType()->isPointerTy() ||
2018 !FTy->getParamType(0)->isPointerTy() ||
2019 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002020 continue;
2021 setDoesNotThrow(F);
2022 setDoesNotAlias(F, 0);
2023 setDoesNotCapture(F, 1);
2024 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002025 } else if (Name == "\1fseeko64" ||
2026 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002027 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002028 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002029 continue;
2030 setDoesNotThrow(F);
2031 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002032 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002033 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002034 continue;
2035 setDoesNotThrow(F);
2036 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002037 } else if (Name == "\1fstat64" ||
2038 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002039 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002040 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002041 continue;
2042 setDoesNotThrow(F);
2043 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002044 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002045 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002046 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002047 continue;
2048 // May throw; "open" is a valid pthread cancellation point.
2049 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002050 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002051 break;
2052 }
2053 }
2054 return Modified;
2055}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002056
2057// TODO:
2058// Additional cases that we need to add to this file:
2059//
2060// cbrt:
2061// * cbrt(expN(X)) -> expN(x/3)
2062// * cbrt(sqrt(x)) -> pow(x,1/6)
2063// * cbrt(sqrt(x)) -> pow(x,1/9)
2064//
2065// cos, cosf, cosl:
2066// * cos(-x) -> cos(x)
2067//
2068// exp, expf, expl:
2069// * exp(log(x)) -> x
2070//
2071// log, logf, logl:
2072// * log(exp(x)) -> x
2073// * log(x**y) -> y*log(x)
2074// * log(exp(y)) -> y*log(e)
2075// * log(exp2(y)) -> y*log(2)
2076// * log(exp10(y)) -> y*log(10)
2077// * log(sqrt(x)) -> 0.5*log(x)
2078// * log(pow(x,y)) -> y*log(x)
2079//
2080// lround, lroundf, lroundl:
2081// * lround(cnst) -> cnst'
2082//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002083// pow, powf, powl:
2084// * pow(exp(x),y) -> exp(x*y)
2085// * pow(sqrt(x),y) -> pow(x,y*0.5)
2086// * pow(pow(x,y),z)-> pow(x,y*z)
2087//
2088// puts:
2089// * puts("") -> putchar("\n")
2090//
2091// round, roundf, roundl:
2092// * round(cnst) -> cnst'
2093//
2094// signbit:
2095// * signbit(cnst) -> cnst'
2096// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2097//
2098// sqrt, sqrtf, sqrtl:
2099// * sqrt(expN(x)) -> expN(x*0.5)
2100// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2101// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2102//
2103// stpcpy:
2104// * stpcpy(str, "literal") ->
2105// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2106// strrchr:
2107// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2108// (if c is a constant integer and s is a constant string)
2109// * strrchr(s1,0) -> strchr(s1,0)
2110//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002111// strpbrk:
2112// * strpbrk(s,a) -> offset_in_for(s,a)
2113// (if s and a are both constant strings)
2114// * strpbrk(s,"") -> 0
2115// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2116//
2117// strspn, strcspn:
2118// * strspn(s,a) -> const_int (if both args are constant)
2119// * strspn("",a) -> 0
2120// * strspn(s,"") -> 0
2121// * strcspn(s,a) -> const_int (if both args are constant)
2122// * strcspn("",a) -> 0
2123// * strcspn(s,"") -> strlen(a)
2124//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002125// tan, tanf, tanl:
2126// * tan(atan(x)) -> x
2127//
2128// trunc, truncf, truncl:
2129// * trunc(cnst) -> cnst'
2130//
2131//