blob: 058cd3c561f3482fd0362bbc11e79b1a9c4d3ffb [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;
Evan Cheng0289b412010-03-23 15:48:04 +000052 bool OptChkCall; // True if it's optimizing a *_chk libcall.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000053public:
Evan Cheng0289b412010-03-23 15:48:04 +000054 LibCallOptimization() : OptChkCall(false) { }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000055 virtual ~LibCallOptimization() {}
56
Evan Cheng0289b412010-03-23 15:48:04 +000057 void setOptChkCall(bool c) { OptChkCall = c; }
58
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000059 /// CallOptimizer - This pure virtual method is implemented by base classes to
60 /// do various optimizations. If this returns null then no transformation was
61 /// performed. If it returns CI, then it transformed the call and CI is to be
62 /// deleted. If it returns something else, replace CI with the new value and
63 /// delete CI.
Eric Christopher37c8b862009-10-07 21:14:25 +000064 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
Eric Christopher7a61d702008-08-08 19:39:37 +000065 =0;
Eric Christopher37c8b862009-10-07 21:14:25 +000066
Dan Gohmanf14d9192009-08-18 00:48:13 +000067 Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000068 Caller = CI->getParent()->getParent();
Dan Gohmanf14d9192009-08-18 00:48:13 +000069 this->TD = TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000070 if (CI->getCalledFunction())
Owen Andersone922c022009-07-22 00:24:57 +000071 Context = &CI->getCalledFunction()->getContext();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000072 return CallOptimizer(CI->getCalledFunction(), CI, B);
73 }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000074};
75} // End anonymous namespace.
76
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000077
78//===----------------------------------------------------------------------===//
79// Helper Functions
80//===----------------------------------------------------------------------===//
81
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000082/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +000083/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000084static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
85 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
86 UI != E; ++UI) {
87 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
88 if (IC->isEquality())
89 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
90 if (C->isNullValue())
91 continue;
92 // Unknown instruction.
93 return false;
94 }
95 return true;
96}
97
98//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000099// String and Memory LibCall Optimizations
100//===----------------------------------------------------------------------===//
101
102//===---------------------------------------===//
103// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000104namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000105struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000106 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000107 // Verify the "strcat" function prototype.
108 const FunctionType *FT = Callee->getFunctionType();
109 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000110 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000111 FT->getParamType(0) != FT->getReturnType() ||
112 FT->getParamType(1) != FT->getReturnType())
113 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000114
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000115 // Extract some information from the instruction
116 Value *Dst = CI->getOperand(1);
117 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000118
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000119 // See if we can get the length of the input string.
120 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000121 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000122 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000123
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000124 // Handle the simple, do-nothing case: strcat(x, "") -> x
125 if (Len == 0)
126 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000127
128 // These optimizations require TargetData.
129 if (!TD) return 0;
130
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000131 EmitStrLenMemCpy(Src, Dst, Len, B);
132 return Dst;
133 }
134
135 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000136 // We need to find the end of the destination string. That's where the
137 // memory is to be moved to. We just generate a call to strlen.
Eric Christopherb6174e32010-03-05 22:25:30 +0000138 Value *DstLen = EmitStrLen(Dst, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000139
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000140 // Now that we have the destination's length, we must index into the
141 // destination's pointer to get the actual memcpy destination (end of
142 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000143 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000144
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000145 // We have enough information to now generate the memcpy call to do the
146 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000147 EmitMemCpy(CpyDst, Src,
Eric Christopherb6174e32010-03-05 22:25:30 +0000148 ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000149 }
150};
151
152//===---------------------------------------===//
153// 'strncat' Optimizations
154
Chris Lattner3e8b6632009-09-02 06:11:42 +0000155struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000156 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
157 // Verify the "strncat" function prototype.
158 const FunctionType *FT = Callee->getFunctionType();
159 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000160 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000161 FT->getParamType(0) != FT->getReturnType() ||
162 FT->getParamType(1) != FT->getReturnType() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000163 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000164 return 0;
165
166 // Extract some information from the instruction
167 Value *Dst = CI->getOperand(1);
168 Value *Src = CI->getOperand(2);
169 uint64_t Len;
170
171 // We don't do anything if length is not constant
172 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
173 Len = LengthArg->getZExtValue();
174 else
175 return 0;
176
177 // See if we can get the length of the input string.
178 uint64_t SrcLen = GetStringLength(Src);
179 if (SrcLen == 0) return 0;
180 --SrcLen; // Unbias length.
181
182 // Handle the simple, do-nothing cases:
183 // strncat(x, "", c) -> x
184 // strncat(x, c, 0) -> x
185 if (SrcLen == 0 || Len == 0) return Dst;
186
Dan Gohmanf14d9192009-08-18 00:48:13 +0000187 // These optimizations require TargetData.
188 if (!TD) return 0;
189
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000190 // We don't optimize this case
191 if (Len < SrcLen) return 0;
192
193 // strncat(x, s, c) -> strcat(x, s)
194 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000195 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000196 return Dst;
197 }
198};
199
200//===---------------------------------------===//
201// 'strchr' Optimizations
202
Chris Lattner3e8b6632009-09-02 06:11:42 +0000203struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000204 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000205 // Verify the "strchr" function prototype.
206 const FunctionType *FT = Callee->getFunctionType();
207 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000208 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000209 FT->getParamType(0) != FT->getReturnType())
210 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000211
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000212 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000213
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000214 // If the second operand is non-constant, see if we can compute the length
215 // of the input string and turn this into memchr.
216 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
217 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000218 // These optimizations require TargetData.
219 if (!TD) return 0;
220
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000221 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000222 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000223 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000224
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000225 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000226 ConstantInt::get(TD->getIntPtrType(*Context), Len),
227 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000228 }
229
230 // Otherwise, the character is a constant, see if the first argument is
231 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000232 std::string Str;
233 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000234 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000235
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000236 // strchr can find the nul character.
237 Str += '\0';
238 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000239
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000240 // Compute the offset.
241 uint64_t i = 0;
242 while (1) {
243 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000244 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000245 // Did we find our match?
246 if (Str[i] == CharValue)
247 break;
248 ++i;
249 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000250
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000251 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000252 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000253 return B.CreateGEP(SrcStr, Idx, "strchr");
254 }
255};
256
257//===---------------------------------------===//
258// 'strcmp' Optimizations
259
Chris Lattner3e8b6632009-09-02 06:11:42 +0000260struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000261 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000262 // Verify the "strcmp" function prototype.
263 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000264 if (FT->getNumParams() != 2 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000265 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000266 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000267 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000268 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000269
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000270 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
271 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000272 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000273
Bill Wendling0582ae92009-03-13 04:39:26 +0000274 std::string Str1, Str2;
275 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
276 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000277
Bill Wendling0582ae92009-03-13 04:39:26 +0000278 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000279 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000280
Bill Wendling0582ae92009-03-13 04:39:26 +0000281 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000282 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000283
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000284 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000285 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000286 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000287 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000288
289 // strcmp(P, "x") -> memcmp(P, "x", 2)
290 uint64_t Len1 = GetStringLength(Str1P);
291 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000292 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000293 // These optimizations require TargetData.
294 if (!TD) return 0;
295
Nick Lewycky13a09e22008-12-21 00:19:21 +0000296 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000297 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000298 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000299 }
300
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000301 return 0;
302 }
303};
304
305//===---------------------------------------===//
306// 'strncmp' Optimizations
307
Chris Lattner3e8b6632009-09-02 06:11:42 +0000308struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000309 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000310 // Verify the "strncmp" function prototype.
311 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000312 if (FT->getNumParams() != 3 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000313 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000314 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000315 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000316 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000317 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000318
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000319 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
320 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000321 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000322
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000323 // Get the length argument if it is constant.
324 uint64_t Length;
325 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
326 Length = LengthArg->getZExtValue();
327 else
328 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000329
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000330 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000331 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000332
Bill Wendling0582ae92009-03-13 04:39:26 +0000333 std::string Str1, Str2;
334 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
335 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000336
Bill Wendling0582ae92009-03-13 04:39:26 +0000337 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000338 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000339
Bill Wendling0582ae92009-03-13 04:39:26 +0000340 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000341 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000342
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000343 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000344 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000345 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000346 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000347 return 0;
348 }
349};
350
351
352//===---------------------------------------===//
353// 'strcpy' Optimizations
354
Chris Lattner3e8b6632009-09-02 06:11:42 +0000355struct StrCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000356 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000357 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000358 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000359 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000360 if (FT->getNumParams() != NumParams ||
361 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000362 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000363 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000364 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000365
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000366 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
367 if (Dst == Src) // strcpy(x,x) -> x
368 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000369
Dan Gohmanf14d9192009-08-18 00:48:13 +0000370 // These optimizations require TargetData.
371 if (!TD) return 0;
372
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000373 // See if we can get the length of the input string.
374 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000375 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000376
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000377 // We have enough information to now generate the memcpy call to do the
378 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000379 if (OptChkCall)
380 EmitMemCpyChk(Dst, Src,
381 ConstantInt::get(TD->getIntPtrType(*Context), Len),
382 CI->getOperand(3), B, TD);
383 else
384 EmitMemCpy(Dst, Src,
385 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000386 return Dst;
387 }
388};
389
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000390//===---------------------------------------===//
391// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000392
Chris Lattner3e8b6632009-09-02 06:11:42 +0000393struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000394 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
395 const FunctionType *FT = Callee->getFunctionType();
396 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
397 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000398 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000399 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000400 return 0;
401
402 Value *Dst = CI->getOperand(1);
403 Value *Src = CI->getOperand(2);
404 Value *LenOp = CI->getOperand(3);
405
406 // See if we can get the length of the input string.
407 uint64_t SrcLen = GetStringLength(Src);
408 if (SrcLen == 0) return 0;
409 --SrcLen;
410
411 if (SrcLen == 0) {
412 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000413 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
Eric Christopherb6174e32010-03-05 22:25:30 +0000414 B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000415 return Dst;
416 }
417
418 uint64_t Len;
419 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
420 Len = LengthArg->getZExtValue();
421 else
422 return 0;
423
424 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
425
Dan Gohmanf14d9192009-08-18 00:48:13 +0000426 // These optimizations require TargetData.
427 if (!TD) return 0;
428
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000429 // Let strncpy handle the zero padding
430 if (Len > SrcLen+1) return 0;
431
432 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000433 EmitMemCpy(Dst, Src,
Eric Christopherb6174e32010-03-05 22:25:30 +0000434 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000435
436 return Dst;
437 }
438};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000439
440//===---------------------------------------===//
441// 'strlen' Optimizations
442
Chris Lattner3e8b6632009-09-02 06:11:42 +0000443struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000444 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000445 const FunctionType *FT = Callee->getFunctionType();
446 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000447 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000448 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000449 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000450
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000451 Value *Src = CI->getOperand(1);
452
453 // Constant folding: strlen("xyz") -> 3
454 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000455 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000456
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000457 // strlen(x) != 0 --> *x != 0
458 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000459 if (IsOnlyUsedInZeroEqualityComparison(CI))
460 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
461 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000462 }
463};
464
465//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000466// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000467
Chris Lattner3e8b6632009-09-02 06:11:42 +0000468struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000469 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
470 const FunctionType *FT = Callee->getFunctionType();
471 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000472 !FT->getParamType(0)->isPointerTy() ||
473 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000474 return 0;
475
476 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000477 if (isa<ConstantPointerNull>(EndPtr)) {
478 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000479 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000480 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000481
482 return 0;
483 }
484};
485
Chris Lattner24604112009-12-16 09:32:05 +0000486//===---------------------------------------===//
487// 'strstr' Optimizations
488
489struct StrStrOpt : public LibCallOptimization {
490 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
491 const FunctionType *FT = Callee->getFunctionType();
492 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000493 !FT->getParamType(0)->isPointerTy() ||
494 !FT->getParamType(1)->isPointerTy() ||
495 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000496 return 0;
497
498 // fold strstr(x, x) -> x.
499 if (CI->getOperand(1) == CI->getOperand(2))
500 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000501
Chris Lattner24604112009-12-16 09:32:05 +0000502 // See if either input string is a constant string.
503 std::string SearchStr, ToFindStr;
504 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
505 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000506
Chris Lattner24604112009-12-16 09:32:05 +0000507 // fold strstr(x, "") -> x.
508 if (HasStr2 && ToFindStr.empty())
509 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000510
Chris Lattner24604112009-12-16 09:32:05 +0000511 // If both strings are known, constant fold it.
512 if (HasStr1 && HasStr2) {
513 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000514
Chris Lattner24604112009-12-16 09:32:05 +0000515 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
516 return Constant::getNullValue(CI->getType());
517
518 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
519 Value *Result = CastToCStr(CI->getOperand(1), B);
520 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
521 return B.CreateBitCast(Result, CI->getType());
522 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000523
Chris Lattner24604112009-12-16 09:32:05 +0000524 // fold strstr(x, "y") -> strchr(x, 'y').
525 if (HasStr2 && ToFindStr.size() == 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000526 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B, TD),
Chris Lattner24604112009-12-16 09:32:05 +0000527 CI->getType());
528 return 0;
529 }
530};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000531
Nick Lewycky4c498412009-02-13 15:31:46 +0000532
533//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000534// 'memcmp' Optimizations
535
Chris Lattner3e8b6632009-09-02 06:11:42 +0000536struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000537 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000538 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000539 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
540 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000541 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000542 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000543
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000544 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000545
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000546 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000547 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000548
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000549 // Make sure we have a constant length.
550 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000551 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000552 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000553
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000554 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000555 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000556
557 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
558 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
559 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +0000560 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000561 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000562
Benjamin Kramer992a6372009-11-05 17:44:22 +0000563 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
564 std::string LHSStr, RHSStr;
565 if (GetConstantStringInfo(LHS, LHSStr) &&
566 GetConstantStringInfo(RHS, RHSStr)) {
567 // Make sure we're not reading out-of-bounds memory.
568 if (Len > LHSStr.length() || Len > RHSStr.length())
569 return 0;
570 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
571 return ConstantInt::get(CI->getType(), Ret);
572 }
573
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000574 return 0;
575 }
576};
577
578//===---------------------------------------===//
579// 'memcpy' Optimizations
580
Chris Lattner3e8b6632009-09-02 06:11:42 +0000581struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000582 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000583 // These optimizations require TargetData.
584 if (!TD) return 0;
585
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000586 const FunctionType *FT = Callee->getFunctionType();
587 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000588 !FT->getParamType(0)->isPointerTy() ||
589 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000590 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000591 return 0;
592
593 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000594 EmitMemCpy(CI->getOperand(1), CI->getOperand(2),
595 CI->getOperand(3), 1, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000596 return CI->getOperand(1);
597 }
598};
599
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000600//===---------------------------------------===//
601// 'memmove' Optimizations
602
Chris Lattner3e8b6632009-09-02 06:11:42 +0000603struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000604 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000605 // These optimizations require TargetData.
606 if (!TD) return 0;
607
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000608 const FunctionType *FT = Callee->getFunctionType();
609 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000610 !FT->getParamType(0)->isPointerTy() ||
611 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000612 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000613 return 0;
614
615 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000616 EmitMemMove(CI->getOperand(1), CI->getOperand(2),
617 CI->getOperand(3), 1, B, TD);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000618 return CI->getOperand(1);
619 }
620};
621
622//===---------------------------------------===//
623// 'memset' Optimizations
624
Chris Lattner3e8b6632009-09-02 06:11:42 +0000625struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000626 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000627 // These optimizations require TargetData.
628 if (!TD) return 0;
629
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000630 const FunctionType *FT = Callee->getFunctionType();
631 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000632 !FT->getParamType(0)->isPointerTy() ||
633 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000634 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000635 return 0;
636
637 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000638 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
639 false);
Eric Christopherb6174e32010-03-05 22:25:30 +0000640 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B, TD);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000641 return CI->getOperand(1);
642 }
643};
644
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000645//===----------------------------------------------------------------------===//
646// Math Library Optimizations
647//===----------------------------------------------------------------------===//
648
649//===---------------------------------------===//
650// 'pow*' Optimizations
651
Chris Lattner3e8b6632009-09-02 06:11:42 +0000652struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000653 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000654 const FunctionType *FT = Callee->getFunctionType();
655 // Just make sure this has 2 arguments of the same FP type, which match the
656 // result type.
657 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
658 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000659 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000660 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000661
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000662 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
663 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
664 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
665 return Op1C;
666 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000667 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000668 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000669
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000670 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
671 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000672
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000673 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000674 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000675
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000676 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000677 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
678 // This is faster than calling pow, and still handles negative zero
679 // and negative infinite correctly.
680 // TODO: In fast-math mode, this could be just sqrt(x).
681 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000682 Value *Inf = ConstantFP::getInfinity(CI->getType());
683 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000684 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
685 Callee->getAttributes());
686 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
687 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000688 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
689 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
690 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000691 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000692
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000693 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
694 return Op1;
695 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000696 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000697 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000698 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000699 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000700 return 0;
701 }
702};
703
704//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000705// 'exp2' Optimizations
706
Chris Lattner3e8b6632009-09-02 06:11:42 +0000707struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000708 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000709 const FunctionType *FT = Callee->getFunctionType();
710 // Just make sure this has 1 argument of FP type, which matches the
711 // result type.
712 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000713 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000714 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000715
Chris Lattnere818f772008-05-02 18:43:35 +0000716 Value *Op = CI->getOperand(1);
717 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
718 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
719 Value *LdExpArg = 0;
720 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
721 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000722 LdExpArg = B.CreateSExt(OpC->getOperand(0),
723 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000724 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
725 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000726 LdExpArg = B.CreateZExt(OpC->getOperand(0),
727 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000728 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000729
Chris Lattnere818f772008-05-02 18:43:35 +0000730 if (LdExpArg) {
731 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000732 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000733 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000734 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000735 Name = "ldexp";
736 else
737 Name = "ldexpl";
738
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000739 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000740 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000741 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000742
743 Module *M = Caller->getParent();
744 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000745 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000746 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000747 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
748 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
749 CI->setCallingConv(F->getCallingConv());
750
751 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000752 }
753 return 0;
754 }
755};
Chris Lattnere818f772008-05-02 18:43:35 +0000756
757//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000758// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
759
Chris Lattner3e8b6632009-09-02 06:11:42 +0000760struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000761 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000762 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000763 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
764 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000765 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000766
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000767 // If this is something like 'floor((double)floatval)', convert to floorf.
768 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000769 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000770 return 0;
771
772 // floor((double)floatval) -> (double)floorf(floatval)
773 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000774 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
775 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000776 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000777 }
778};
779
780//===----------------------------------------------------------------------===//
781// Integer Optimizations
782//===----------------------------------------------------------------------===//
783
784//===---------------------------------------===//
785// 'ffs*' Optimizations
786
Chris Lattner3e8b6632009-09-02 06:11:42 +0000787struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000788 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000789 const FunctionType *FT = Callee->getFunctionType();
790 // Just make sure this has 2 arguments of the same FP type, which match the
791 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000792 if (FT->getNumParams() != 1 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000793 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000794 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000795 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000796
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000797 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000798
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000799 // Constant fold.
800 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
801 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000802 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000803 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000804 CI->getValue().countTrailingZeros()+1);
805 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000806
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000807 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
808 const Type *ArgType = Op->getType();
809 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
810 Intrinsic::cttz, &ArgType, 1);
811 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000812 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000813 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000814
Owen Andersona7235ea2009-07-31 20:28:14 +0000815 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000816 return B.CreateSelect(Cond, V,
817 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000818 }
819};
820
821//===---------------------------------------===//
822// 'isdigit' Optimizations
823
Chris Lattner3e8b6632009-09-02 06:11:42 +0000824struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000825 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000826 const FunctionType *FT = Callee->getFunctionType();
827 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000828 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000829 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000830 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000831
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000832 // isdigit(c) -> (c-'0') <u 10
833 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000834 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000835 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000836 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000837 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000838 return B.CreateZExt(Op, CI->getType());
839 }
840};
841
842//===---------------------------------------===//
843// 'isascii' Optimizations
844
Chris Lattner3e8b6632009-09-02 06:11:42 +0000845struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000846 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000847 const FunctionType *FT = Callee->getFunctionType();
848 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000849 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000850 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000851 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000852
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000853 // isascii(c) -> c <u 128
854 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000855 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000856 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000857 return B.CreateZExt(Op, CI->getType());
858 }
859};
Eric Christopher37c8b862009-10-07 21:14:25 +0000860
Chris Lattner313f0e62008-06-09 08:26:51 +0000861//===---------------------------------------===//
862// 'abs', 'labs', 'llabs' Optimizations
863
Chris Lattner3e8b6632009-09-02 06:11:42 +0000864struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000865 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +0000866 const FunctionType *FT = Callee->getFunctionType();
867 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +0000868 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +0000869 FT->getParamType(0) != FT->getReturnType())
870 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000871
Chris Lattner313f0e62008-06-09 08:26:51 +0000872 // abs(x) -> x >s -1 ? x : -x
873 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000874 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +0000875 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +0000876 "ispos");
877 Value *Neg = B.CreateNeg(Op, "neg");
878 return B.CreateSelect(Pos, Op, Neg);
879 }
880};
Eric Christopher37c8b862009-10-07 21:14:25 +0000881
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000882
883//===---------------------------------------===//
884// 'toascii' Optimizations
885
Chris Lattner3e8b6632009-09-02 06:11:42 +0000886struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000887 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000888 const FunctionType *FT = Callee->getFunctionType();
889 // We require i32(i32)
890 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000891 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000892 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000893
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000894 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000895 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +0000896 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000897 }
898};
899
900//===----------------------------------------------------------------------===//
901// Formatting and IO Optimizations
902//===----------------------------------------------------------------------===//
903
904//===---------------------------------------===//
905// 'printf' Optimizations
906
Chris Lattner3e8b6632009-09-02 06:11:42 +0000907struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000908 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000909 // Require one fixed pointer argument and an integer/void result.
910 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000911 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
912 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000913 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000914 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000915
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000916 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000917 std::string FormatStr;
918 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
919 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000920
921 // Empty format string -> noop.
922 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +0000923 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000924 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000925
Chris Lattner74965f22009-11-09 04:57:04 +0000926 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
927 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000928 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +0000929 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000930 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +0000931 if (CI->use_empty()) return CI;
932 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000933 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000934
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000935 // printf("foo\n") --> puts("foo")
936 if (FormatStr[FormatStr.size()-1] == '\n' &&
937 FormatStr.find('%') == std::string::npos) { // no format characters.
938 // Create a string literal with no \n on it. We expect the constant merge
939 // pass to be run after this pass, to merge duplicate strings.
940 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000941 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +0000942 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
943 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +0000944 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000945 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000946 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000947 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000948
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000949 // Optimize specific format strings.
950 // printf("%c", chr) --> putchar(*(i8*)dst)
951 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
Duncan Sands1df98592010-02-16 11:11:14 +0000952 CI->getOperand(2)->getType()->isIntegerTy()) {
Eric Christopherb6174e32010-03-05 22:25:30 +0000953 Value *Res = EmitPutChar(CI->getOperand(2), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000954
Chris Lattner74965f22009-11-09 04:57:04 +0000955 if (CI->use_empty()) return CI;
956 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000957 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000958
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000959 // printf("%s\n", str) --> puts(str)
960 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
Duncan Sands1df98592010-02-16 11:11:14 +0000961 CI->getOperand(2)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000962 CI->use_empty()) {
Eric Christopherb6174e32010-03-05 22:25:30 +0000963 EmitPutS(CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000964 return CI;
965 }
966 return 0;
967 }
968};
969
970//===---------------------------------------===//
971// 'sprintf' Optimizations
972
Chris Lattner3e8b6632009-09-02 06:11:42 +0000973struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000974 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000975 // Require two fixed pointer arguments and an integer result.
976 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000977 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
978 !FT->getParamType(1)->isPointerTy() ||
979 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000980 return 0;
981
982 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000983 std::string FormatStr;
984 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
985 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000986
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000987 // If we just have a format string (nothing else crazy) transform it.
988 if (CI->getNumOperands() == 3) {
989 // Make sure there's no % in the constant array. We could try to handle
990 // %% -> % in the future if we cared.
991 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
992 if (FormatStr[i] == '%')
993 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +0000994
995 // These optimizations require TargetData.
996 if (!TD) return 0;
997
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000998 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
999 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Eric Christopherb6174e32010-03-05 22:25:30 +00001000 ConstantInt::get(TD->getIntPtrType(*Context),
1001 FormatStr.size()+1), 1, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001002 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001003 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001004
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001005 // The remaining optimizations require the format string to be "%s" or "%c"
1006 // and have an extra operand.
1007 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1008 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001009
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001010 // Decode the second character of the format string.
1011 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001012 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Duncan Sands1df98592010-02-16 11:11:14 +00001013 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001014 Value *V = B.CreateTrunc(CI->getOperand(3),
1015 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001016 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1017 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001018 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1019 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001020 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001021
Owen Andersoneed707b2009-07-24 23:12:02 +00001022 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001023 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001024
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001025 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001026 // These optimizations require TargetData.
1027 if (!TD) return 0;
1028
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001029 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Duncan Sands1df98592010-02-16 11:11:14 +00001030 if (!CI->getOperand(3)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001031
Eric Christopherb6174e32010-03-05 22:25:30 +00001032 Value *Len = EmitStrLen(CI->getOperand(3), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001033 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001034 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001035 "leninc");
Eric Christopherb6174e32010-03-05 22:25:30 +00001036 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001037
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001038 // The sprintf result is the unincremented number of bytes in the string.
1039 return B.CreateIntCast(Len, CI->getType(), false);
1040 }
1041 return 0;
1042 }
1043};
1044
1045//===---------------------------------------===//
1046// 'fwrite' Optimizations
1047
Chris Lattner3e8b6632009-09-02 06:11:42 +00001048struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001049 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001050 // Require a pointer, an integer, an integer, a pointer, returning integer.
1051 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001052 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1053 !FT->getParamType(1)->isIntegerTy() ||
1054 !FT->getParamType(2)->isIntegerTy() ||
1055 !FT->getParamType(3)->isPointerTy() ||
1056 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001057 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001058
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001059 // Get the element size and count.
1060 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1061 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1062 if (!SizeC || !CountC) return 0;
1063 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001064
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001065 // If this is writing zero records, remove the call (it's a noop).
1066 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001067 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001068
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001069 // If this is writing one byte, turn it into fputc.
1070 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1071 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
Eric Christopherb6174e32010-03-05 22:25:30 +00001072 EmitFPutC(Char, CI->getOperand(4), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001073 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001074 }
1075
1076 return 0;
1077 }
1078};
1079
1080//===---------------------------------------===//
1081// 'fputs' Optimizations
1082
Chris Lattner3e8b6632009-09-02 06:11:42 +00001083struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001084 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001085 // These optimizations require TargetData.
1086 if (!TD) return 0;
1087
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001088 // Require two pointers. Also, we can't optimize if return value is used.
1089 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001090 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1091 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001092 !CI->use_empty())
1093 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001094
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001095 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1096 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001097 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001098 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001099 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Eric Christopherb6174e32010-03-05 22:25:30 +00001100 CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001101 return CI; // Known to have no uses (see above).
1102 }
1103};
1104
1105//===---------------------------------------===//
1106// 'fprintf' Optimizations
1107
Chris Lattner3e8b6632009-09-02 06:11:42 +00001108struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001109 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001110 // Require two fixed paramters as pointers and integer result.
1111 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001112 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1113 !FT->getParamType(1)->isPointerTy() ||
1114 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001115 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001116
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001117 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001118 std::string FormatStr;
1119 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1120 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001121
1122 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1123 if (CI->getNumOperands() == 3) {
1124 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1125 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001126 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001127
1128 // These optimizations require TargetData.
1129 if (!TD) return 0;
1130
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001131 EmitFWrite(CI->getOperand(2),
1132 ConstantInt::get(TD->getIntPtrType(*Context),
1133 FormatStr.size()),
Eric Christopherb6174e32010-03-05 22:25:30 +00001134 CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001135 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001136 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001137
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001138 // The remaining optimizations require the format string to be "%s" or "%c"
1139 // and have an extra operand.
1140 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1141 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001142
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001143 // Decode the second character of the format string.
1144 if (FormatStr[1] == 'c') {
1145 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
Duncan Sands1df98592010-02-16 11:11:14 +00001146 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
Eric Christopherb6174e32010-03-05 22:25:30 +00001147 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001148 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001149 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001150
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001151 if (FormatStr[1] == 's') {
1152 // fprintf(F, "%s", str) -> fputs(str, F)
Duncan Sands1df98592010-02-16 11:11:14 +00001153 if (!CI->getOperand(3)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001154 return 0;
Eric Christopherb6174e32010-03-05 22:25:30 +00001155 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001156 return CI;
1157 }
1158 return 0;
1159 }
1160};
1161
Bill Wendlingac178222008-05-05 21:37:59 +00001162} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001163
1164//===----------------------------------------------------------------------===//
1165// SimplifyLibCalls Pass Implementation
1166//===----------------------------------------------------------------------===//
1167
1168namespace {
1169 /// This pass optimizes well known library functions from libc and libm.
1170 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001171 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001172 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001173 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001174 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
Evan Cheng0289b412010-03-23 15:48:04 +00001175 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
1176 StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001177 StrToOpt StrTo; StrStrOpt StrStr;
1178 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001179 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001180 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001181 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001182 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1183 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001184 // Formatting and IO Optimizations
1185 SPrintFOpt SPrintF; PrintFOpt PrintF;
1186 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001187
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001188 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001189 public:
1190 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001191 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001192
1193 void InitOptimizations();
1194 bool runOnFunction(Function &F);
1195
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001196 void setDoesNotAccessMemory(Function &F);
1197 void setOnlyReadsMemory(Function &F);
1198 void setDoesNotThrow(Function &F);
1199 void setDoesNotCapture(Function &F, unsigned n);
1200 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001201 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001202
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001203 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001204 }
1205 };
1206 char SimplifyLibCalls::ID = 0;
1207} // end anonymous namespace.
1208
1209static RegisterPass<SimplifyLibCalls>
1210X("simplify-libcalls", "Simplify well-known library calls");
1211
1212// Public interface to the Simplify LibCalls pass.
1213FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001214 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001215}
1216
1217/// Optimizations - Populate the Optimizations map with all the optimizations
1218/// we know.
1219void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001220 // String and Memory LibCall Optimizations
1221 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001222 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001223 Optimizations["strchr"] = &StrChr;
1224 Optimizations["strcmp"] = &StrCmp;
1225 Optimizations["strncmp"] = &StrNCmp;
1226 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001227 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001228 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001229 Optimizations["strtol"] = &StrTo;
1230 Optimizations["strtod"] = &StrTo;
1231 Optimizations["strtof"] = &StrTo;
1232 Optimizations["strtoul"] = &StrTo;
1233 Optimizations["strtoll"] = &StrTo;
1234 Optimizations["strtold"] = &StrTo;
1235 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001236 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001237 Optimizations["memcmp"] = &MemCmp;
1238 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001239 Optimizations["memmove"] = &MemMove;
1240 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001241
Evan Cheng0289b412010-03-23 15:48:04 +00001242 // _chk variants of String and Memory LibCall Optimizations.
1243 StrCpyChk.setOptChkCall(true);
1244 Optimizations["__strcpy_chk"] = &StrCpyChk;
1245
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001246 // Math Library Optimizations
1247 Optimizations["powf"] = &Pow;
1248 Optimizations["pow"] = &Pow;
1249 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001250 Optimizations["llvm.pow.f32"] = &Pow;
1251 Optimizations["llvm.pow.f64"] = &Pow;
1252 Optimizations["llvm.pow.f80"] = &Pow;
1253 Optimizations["llvm.pow.f128"] = &Pow;
1254 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001255 Optimizations["exp2l"] = &Exp2;
1256 Optimizations["exp2"] = &Exp2;
1257 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001258 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1259 Optimizations["llvm.exp2.f128"] = &Exp2;
1260 Optimizations["llvm.exp2.f80"] = &Exp2;
1261 Optimizations["llvm.exp2.f64"] = &Exp2;
1262 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001263
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001264#ifdef HAVE_FLOORF
1265 Optimizations["floor"] = &UnaryDoubleFP;
1266#endif
1267#ifdef HAVE_CEILF
1268 Optimizations["ceil"] = &UnaryDoubleFP;
1269#endif
1270#ifdef HAVE_ROUNDF
1271 Optimizations["round"] = &UnaryDoubleFP;
1272#endif
1273#ifdef HAVE_RINTF
1274 Optimizations["rint"] = &UnaryDoubleFP;
1275#endif
1276#ifdef HAVE_NEARBYINTF
1277 Optimizations["nearbyint"] = &UnaryDoubleFP;
1278#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001279
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001280 // Integer Optimizations
1281 Optimizations["ffs"] = &FFS;
1282 Optimizations["ffsl"] = &FFS;
1283 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001284 Optimizations["abs"] = &Abs;
1285 Optimizations["labs"] = &Abs;
1286 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001287 Optimizations["isdigit"] = &IsDigit;
1288 Optimizations["isascii"] = &IsAscii;
1289 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001290
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001291 // Formatting and IO Optimizations
1292 Optimizations["sprintf"] = &SPrintF;
1293 Optimizations["printf"] = &PrintF;
1294 Optimizations["fwrite"] = &FWrite;
1295 Optimizations["fputs"] = &FPuts;
1296 Optimizations["fprintf"] = &FPrintF;
1297}
1298
1299
1300/// runOnFunction - Top level algorithm.
1301///
1302bool SimplifyLibCalls::runOnFunction(Function &F) {
1303 if (Optimizations.empty())
1304 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001305
Dan Gohmanf14d9192009-08-18 00:48:13 +00001306 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001307
Owen Andersone922c022009-07-22 00:24:57 +00001308 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001309
1310 bool Changed = false;
1311 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1312 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1313 // Ignore non-calls.
1314 CallInst *CI = dyn_cast<CallInst>(I++);
1315 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001316
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001317 // Ignore indirect calls and calls to non-external functions.
1318 Function *Callee = CI->getCalledFunction();
1319 if (Callee == 0 || !Callee->isDeclaration() ||
1320 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1321 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001322
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001323 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001324 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1325 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001326
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001327 // Set the builder to the instruction after the call.
1328 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001329
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001330 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001331 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001332 if (Result == 0) continue;
1333
David Greene6a6b90e2010-01-05 01:27:21 +00001334 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1335 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001336
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001337 // Something changed!
1338 Changed = true;
1339 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001340
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001341 // Inspect the instruction after the call (which was potentially just
1342 // added) next.
1343 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001344
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001345 if (CI != Result && !CI->use_empty()) {
1346 CI->replaceAllUsesWith(Result);
1347 if (!Result->hasName())
1348 Result->takeName(CI);
1349 }
1350 CI->eraseFromParent();
1351 }
1352 }
1353 return Changed;
1354}
1355
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001356// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001357
1358void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1359 if (!F.doesNotAccessMemory()) {
1360 F.setDoesNotAccessMemory();
1361 ++NumAnnotated;
1362 Modified = true;
1363 }
1364}
1365void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1366 if (!F.onlyReadsMemory()) {
1367 F.setOnlyReadsMemory();
1368 ++NumAnnotated;
1369 Modified = true;
1370 }
1371}
1372void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1373 if (!F.doesNotThrow()) {
1374 F.setDoesNotThrow();
1375 ++NumAnnotated;
1376 Modified = true;
1377 }
1378}
1379void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1380 if (!F.doesNotCapture(n)) {
1381 F.setDoesNotCapture(n);
1382 ++NumAnnotated;
1383 Modified = true;
1384 }
1385}
1386void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1387 if (!F.doesNotAlias(n)) {
1388 F.setDoesNotAlias(n);
1389 ++NumAnnotated;
1390 Modified = true;
1391 }
1392}
1393
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001394/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001395///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001396bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001397 Modified = false;
1398 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1399 Function &F = *I;
1400 if (!F.isDeclaration())
1401 continue;
1402
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001403 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001404 continue;
1405
1406 const FunctionType *FTy = F.getFunctionType();
1407
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001408 StringRef Name = F.getName();
1409 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001410 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001411 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001412 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001413 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001414 continue;
1415 setOnlyReadsMemory(F);
1416 setDoesNotThrow(F);
1417 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001418 } else if (Name == "strchr" ||
1419 Name == "strrchr") {
1420 if (FTy->getNumParams() != 2 ||
1421 !FTy->getParamType(0)->isPointerTy() ||
1422 !FTy->getParamType(1)->isIntegerTy())
1423 continue;
1424 setOnlyReadsMemory(F);
1425 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001426 } else if (Name == "strcpy" ||
1427 Name == "stpcpy" ||
1428 Name == "strcat" ||
1429 Name == "strtol" ||
1430 Name == "strtod" ||
1431 Name == "strtof" ||
1432 Name == "strtoul" ||
1433 Name == "strtoll" ||
1434 Name == "strtold" ||
1435 Name == "strncat" ||
1436 Name == "strncpy" ||
1437 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001438 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001439 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001440 continue;
1441 setDoesNotThrow(F);
1442 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001443 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001444 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001445 !FTy->getParamType(0)->isPointerTy() ||
1446 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001447 continue;
1448 setDoesNotThrow(F);
1449 setDoesNotCapture(F, 1);
1450 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001451 } else if (Name == "strcmp" ||
1452 Name == "strspn" ||
1453 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001454 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001455 Name == "strcoll" ||
1456 Name == "strcasecmp" ||
1457 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001458 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001459 !FTy->getParamType(0)->isPointerTy() ||
1460 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001461 continue;
1462 setOnlyReadsMemory(F);
1463 setDoesNotThrow(F);
1464 setDoesNotCapture(F, 1);
1465 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001466 } else if (Name == "strstr" ||
1467 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001468 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001469 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001470 continue;
1471 setOnlyReadsMemory(F);
1472 setDoesNotThrow(F);
1473 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001474 } else if (Name == "strtok" ||
1475 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001476 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001477 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001478 continue;
1479 setDoesNotThrow(F);
1480 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001481 } else if (Name == "scanf" ||
1482 Name == "setbuf" ||
1483 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001484 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001485 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001486 continue;
1487 setDoesNotThrow(F);
1488 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001489 } else if (Name == "strdup" ||
1490 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001491 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001492 !FTy->getReturnType()->isPointerTy() ||
1493 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001494 continue;
1495 setDoesNotThrow(F);
1496 setDoesNotAlias(F, 0);
1497 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001498 } else if (Name == "stat" ||
1499 Name == "sscanf" ||
1500 Name == "sprintf" ||
1501 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001502 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001503 !FTy->getParamType(0)->isPointerTy() ||
1504 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001505 continue;
1506 setDoesNotThrow(F);
1507 setDoesNotCapture(F, 1);
1508 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001509 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001510 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001511 !FTy->getParamType(0)->isPointerTy() ||
1512 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001513 continue;
1514 setDoesNotThrow(F);
1515 setDoesNotCapture(F, 1);
1516 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001517 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001518 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001519 !FTy->getParamType(1)->isPointerTy() ||
1520 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001521 continue;
1522 setDoesNotThrow(F);
1523 setDoesNotCapture(F, 2);
1524 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001525 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001526 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001527 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001528 continue;
1529 // May throw; "system" is a valid pthread cancellation point.
1530 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001531 }
1532 break;
1533 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001534 if (Name == "malloc") {
1535 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001536 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001537 continue;
1538 setDoesNotThrow(F);
1539 setDoesNotAlias(F, 0);
1540 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001541 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001542 !FTy->getParamType(0)->isPointerTy() ||
1543 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001544 continue;
1545 setOnlyReadsMemory(F);
1546 setDoesNotThrow(F);
1547 setDoesNotCapture(F, 1);
1548 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001549 } else if (Name == "memchr" ||
1550 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001551 if (FTy->getNumParams() != 3)
1552 continue;
1553 setOnlyReadsMemory(F);
1554 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001555 } else if (Name == "modf" ||
1556 Name == "modff" ||
1557 Name == "modfl" ||
1558 Name == "memcpy" ||
1559 Name == "memccpy" ||
1560 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001561 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001562 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001563 continue;
1564 setDoesNotThrow(F);
1565 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001566 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001567 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001568 continue;
1569 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001570 } else if (Name == "mkdir" ||
1571 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001572 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001573 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001574 continue;
1575 setDoesNotThrow(F);
1576 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001577 }
1578 break;
1579 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001580 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001581 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001582 !FTy->getParamType(0)->isPointerTy() ||
1583 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001584 continue;
1585 setDoesNotThrow(F);
1586 setDoesNotAlias(F, 0);
1587 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001588 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001589 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001590 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001591 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001592 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001593 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001594 } else if (Name == "rmdir" ||
1595 Name == "rewind" ||
1596 Name == "remove" ||
1597 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001598 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001599 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001600 continue;
1601 setDoesNotThrow(F);
1602 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001603 } else if (Name == "rename" ||
1604 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001605 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001606 !FTy->getParamType(0)->isPointerTy() ||
1607 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001608 continue;
1609 setDoesNotThrow(F);
1610 setDoesNotCapture(F, 1);
1611 setDoesNotCapture(F, 2);
1612 }
1613 break;
1614 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001615 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001616 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001617 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001618 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001619 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001620 setDoesNotCapture(F, 2);
1621 }
1622 break;
1623 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001624 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001625 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001626 !FTy->getParamType(0)->isPointerTy() ||
1627 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001628 continue;
1629 setDoesNotThrow(F);
1630 setDoesNotCapture(F, 1);
1631 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001632 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001633 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001634 !FTy->getParamType(0)->isPointerTy() ||
1635 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001636 continue;
1637 setDoesNotThrow(F);
1638 setOnlyReadsMemory(F);
1639 setDoesNotCapture(F, 1);
1640 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001641 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001642 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001643 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001644 continue;
1645 setDoesNotThrow(F);
1646 setDoesNotCapture(F, 1);
1647 }
1648 break;
1649 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001650 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001651 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001652 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001653 continue;
1654 setDoesNotThrow(F);
1655 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001656 } else if (Name == "chmod" ||
1657 Name == "chown" ||
1658 Name == "ctermid" ||
1659 Name == "clearerr" ||
1660 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001661 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001662 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001663 continue;
1664 setDoesNotThrow(F);
1665 setDoesNotCapture(F, 1);
1666 }
1667 break;
1668 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001669 if (Name == "atoi" ||
1670 Name == "atol" ||
1671 Name == "atof" ||
1672 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001673 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001674 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001675 continue;
1676 setDoesNotThrow(F);
1677 setOnlyReadsMemory(F);
1678 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001679 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001680 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001681 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001682 continue;
1683 setDoesNotThrow(F);
1684 setDoesNotCapture(F, 1);
1685 }
1686 break;
1687 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001688 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001689 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001690 !FTy->getReturnType()->isPointerTy() ||
1691 !FTy->getParamType(0)->isPointerTy() ||
1692 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001693 continue;
1694 setDoesNotThrow(F);
1695 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001696 setDoesNotCapture(F, 1);
1697 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001698 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001699 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001700 !FTy->getReturnType()->isPointerTy() ||
1701 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001702 continue;
1703 setDoesNotThrow(F);
1704 setDoesNotAlias(F, 0);
1705 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001706 } else if (Name == "feof" ||
1707 Name == "free" ||
1708 Name == "fseek" ||
1709 Name == "ftell" ||
1710 Name == "fgetc" ||
1711 Name == "fseeko" ||
1712 Name == "ftello" ||
1713 Name == "fileno" ||
1714 Name == "fflush" ||
1715 Name == "fclose" ||
1716 Name == "fsetpos" ||
1717 Name == "flockfile" ||
1718 Name == "funlockfile" ||
1719 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001720 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001721 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001722 continue;
1723 setDoesNotThrow(F);
1724 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001725 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001726 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001727 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001728 continue;
1729 setDoesNotThrow(F);
1730 setDoesNotCapture(F, 1);
1731 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001732 } else if (Name == "fputc" ||
1733 Name == "fstat" ||
1734 Name == "frexp" ||
1735 Name == "frexpf" ||
1736 Name == "frexpl" ||
1737 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001738 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001739 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001740 continue;
1741 setDoesNotThrow(F);
1742 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001743 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001744 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001745 !FTy->getParamType(0)->isPointerTy() ||
1746 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001747 continue;
1748 setDoesNotThrow(F);
1749 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001750 } else if (Name == "fread" ||
1751 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001752 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001753 !FTy->getParamType(0)->isPointerTy() ||
1754 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001755 continue;
1756 setDoesNotThrow(F);
1757 setDoesNotCapture(F, 1);
1758 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001759 } else if (Name == "fputs" ||
1760 Name == "fscanf" ||
1761 Name == "fprintf" ||
1762 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001763 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001764 !FTy->getParamType(0)->isPointerTy() ||
1765 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001766 continue;
1767 setDoesNotThrow(F);
1768 setDoesNotCapture(F, 1);
1769 setDoesNotCapture(F, 2);
1770 }
1771 break;
1772 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001773 if (Name == "getc" ||
1774 Name == "getlogin_r" ||
1775 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001776 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001777 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001778 continue;
1779 setDoesNotThrow(F);
1780 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001781 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001782 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001783 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001784 continue;
1785 setDoesNotThrow(F);
1786 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001787 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001788 } else if (Name == "gets" ||
1789 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001790 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001791 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001792 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001793 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001794 continue;
1795 setDoesNotThrow(F);
1796 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001797 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001798 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001799 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001800 continue;
1801 setDoesNotThrow(F);
1802 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001803 }
1804 break;
1805 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001806 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001807 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001808 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001809 continue;
1810 setDoesNotThrow(F);
1811 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001812 } else if (Name == "uname" ||
1813 Name == "unlink" ||
1814 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001815 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001816 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001817 continue;
1818 setDoesNotThrow(F);
1819 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001820 } else if (Name == "utime" ||
1821 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001822 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001823 !FTy->getParamType(0)->isPointerTy() ||
1824 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001825 continue;
1826 setDoesNotThrow(F);
1827 setDoesNotCapture(F, 1);
1828 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001829 }
1830 break;
1831 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001832 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001833 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001834 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001835 continue;
1836 setDoesNotThrow(F);
1837 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001838 } else if (Name == "puts" ||
1839 Name == "printf" ||
1840 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001841 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001842 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001843 continue;
1844 setDoesNotThrow(F);
1845 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001846 } else if (Name == "pread" ||
1847 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001848 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001849 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001850 continue;
1851 // May throw; these are valid pthread cancellation points.
1852 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001853 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001854 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001855 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001856 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001857 !FTy->getReturnType()->isPointerTy() ||
1858 !FTy->getParamType(0)->isPointerTy() ||
1859 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001860 continue;
1861 setDoesNotThrow(F);
1862 setDoesNotAlias(F, 0);
1863 setDoesNotCapture(F, 1);
1864 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001865 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001866 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001867 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001868 continue;
1869 setDoesNotThrow(F);
1870 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001871 }
1872 break;
1873 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001874 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001875 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001876 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001877 continue;
1878 setDoesNotThrow(F);
1879 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001880 } else if (Name == "vsscanf" ||
1881 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001882 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001883 !FTy->getParamType(1)->isPointerTy() ||
1884 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001885 continue;
1886 setDoesNotThrow(F);
1887 setDoesNotCapture(F, 1);
1888 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001889 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00001890 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001891 continue;
1892 setDoesNotThrow(F);
1893 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001894 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001895 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001896 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001897 continue;
1898 setDoesNotThrow(F);
1899 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001900 } else if (Name == "vfprintf" ||
1901 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001902 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001903 !FTy->getParamType(0)->isPointerTy() ||
1904 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001905 continue;
1906 setDoesNotThrow(F);
1907 setDoesNotCapture(F, 1);
1908 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001909 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001910 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001911 !FTy->getParamType(0)->isPointerTy() ||
1912 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001913 continue;
1914 setDoesNotThrow(F);
1915 setDoesNotCapture(F, 1);
1916 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001917 }
1918 break;
1919 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001920 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001921 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001922 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001923 continue;
1924 // May throw; "open" is a valid pthread cancellation point.
1925 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001926 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001927 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001928 !FTy->getReturnType()->isPointerTy() ||
1929 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001930 continue;
1931 setDoesNotThrow(F);
1932 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00001933 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001934 }
1935 break;
1936 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001937 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00001938 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001939 continue;
1940 setDoesNotThrow(F);
1941 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001942 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001943 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001944 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001945 continue;
1946 setDoesNotThrow(F);
1947 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001948 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001949 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001950 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001951 if (Name == "htonl" ||
1952 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001953 setDoesNotThrow(F);
1954 setDoesNotAccessMemory(F);
1955 }
1956 break;
1957 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001958 if (Name == "ntohl" ||
1959 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001960 setDoesNotThrow(F);
1961 setDoesNotAccessMemory(F);
1962 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001963 break;
1964 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001965 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001966 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001967 !FTy->getParamType(0)->isPointerTy() ||
1968 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001969 continue;
1970 setDoesNotThrow(F);
1971 setDoesNotCapture(F, 1);
1972 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001973 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001974 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001975 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001976 continue;
1977 setDoesNotThrow(F);
1978 setDoesNotCapture(F, 1);
1979 }
1980 break;
1981 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001982 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001983 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001984 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001985 continue;
1986 // May throw; places call through function pointer.
1987 setDoesNotCapture(F, 4);
1988 }
1989 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001990 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001991 if (Name == "__strdup" ||
1992 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001993 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001994 !FTy->getReturnType()->isPointerTy() ||
1995 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001996 continue;
1997 setDoesNotThrow(F);
1998 setDoesNotAlias(F, 0);
1999 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002000 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002001 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002002 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002003 continue;
2004 setDoesNotThrow(F);
2005 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002006 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002007 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002008 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002009 continue;
2010 setDoesNotThrow(F);
2011 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002012 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002013 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002014 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002015 continue;
2016 setDoesNotThrow(F);
2017 setDoesNotCapture(F, 2);
2018 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002019 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002020 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002021 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002022 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002023 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002024 continue;
2025 setDoesNotThrow(F);
2026 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002027 } else if (Name == "\1stat64" ||
2028 Name == "\1lstat64" ||
2029 Name == "\1statvfs64" ||
2030 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002031 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002032 !FTy->getParamType(0)->isPointerTy() ||
2033 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002034 continue;
2035 setDoesNotThrow(F);
2036 setDoesNotCapture(F, 1);
2037 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002038 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002039 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002040 !FTy->getReturnType()->isPointerTy() ||
2041 !FTy->getParamType(0)->isPointerTy() ||
2042 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002043 continue;
2044 setDoesNotThrow(F);
2045 setDoesNotAlias(F, 0);
2046 setDoesNotCapture(F, 1);
2047 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002048 } else if (Name == "\1fseeko64" ||
2049 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002050 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002051 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002052 continue;
2053 setDoesNotThrow(F);
2054 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002055 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002056 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002057 continue;
2058 setDoesNotThrow(F);
2059 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002060 } else if (Name == "\1fstat64" ||
2061 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002062 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002063 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002064 continue;
2065 setDoesNotThrow(F);
2066 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002067 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002068 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002069 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002070 continue;
2071 // May throw; "open" is a valid pthread cancellation point.
2072 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002073 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002074 break;
2075 }
2076 }
2077 return Modified;
2078}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002079
2080// TODO:
2081// Additional cases that we need to add to this file:
2082//
2083// cbrt:
2084// * cbrt(expN(X)) -> expN(x/3)
2085// * cbrt(sqrt(x)) -> pow(x,1/6)
2086// * cbrt(sqrt(x)) -> pow(x,1/9)
2087//
2088// cos, cosf, cosl:
2089// * cos(-x) -> cos(x)
2090//
2091// exp, expf, expl:
2092// * exp(log(x)) -> x
2093//
2094// log, logf, logl:
2095// * log(exp(x)) -> x
2096// * log(x**y) -> y*log(x)
2097// * log(exp(y)) -> y*log(e)
2098// * log(exp2(y)) -> y*log(2)
2099// * log(exp10(y)) -> y*log(10)
2100// * log(sqrt(x)) -> 0.5*log(x)
2101// * log(pow(x,y)) -> y*log(x)
2102//
2103// lround, lroundf, lroundl:
2104// * lround(cnst) -> cnst'
2105//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002106// pow, powf, powl:
2107// * pow(exp(x),y) -> exp(x*y)
2108// * pow(sqrt(x),y) -> pow(x,y*0.5)
2109// * pow(pow(x,y),z)-> pow(x,y*z)
2110//
2111// puts:
2112// * puts("") -> putchar("\n")
2113//
2114// round, roundf, roundl:
2115// * round(cnst) -> cnst'
2116//
2117// signbit:
2118// * signbit(cnst) -> cnst'
2119// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2120//
2121// sqrt, sqrtf, sqrtl:
2122// * sqrt(expN(x)) -> expN(x*0.5)
2123// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2124// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2125//
2126// stpcpy:
2127// * stpcpy(str, "literal") ->
2128// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2129// strrchr:
2130// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2131// (if c is a constant integer and s is a constant string)
2132// * strrchr(s1,0) -> strchr(s1,0)
2133//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002134// strpbrk:
2135// * strpbrk(s,a) -> offset_in_for(s,a)
2136// (if s and a are both constant strings)
2137// * strpbrk(s,"") -> 0
2138// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2139//
2140// strspn, strcspn:
2141// * strspn(s,a) -> const_int (if both args are constant)
2142// * strspn("",a) -> 0
2143// * strspn(s,"") -> 0
2144// * strcspn(s,a) -> const_int (if both args are constant)
2145// * strcspn("",a) -> 0
2146// * strcspn(s,"") -> strlen(a)
2147//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002148// tan, tanf, tanl:
2149// * tan(atan(x)) -> x
2150//
2151// trunc, truncf, truncl:
2152// * trunc(cnst) -> cnst'
2153//
2154//