blob: b053cfc3b46d992f4bec31ddd8130031d33ea6e0 [file] [log] [blame]
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001//===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a simple pass that applies a variety of small
11// optimizations for calls to specific well-known function calls (e.g. runtime
Chris Lattnere9f9a7e2009-09-03 05:19:59 +000012// library functions). Any optimization that takes the very simple form
13// "replace call to library function with simpler code that provides the same
14// result" belongs in this file.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000015//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "simplify-libcalls"
19#include "llvm/Transforms/Scalar.h"
Eric Christopherb6174e32010-03-05 22:25:30 +000020#include "llvm/Transforms/Utils/BuildLibCalls.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000021#include "llvm/Intrinsics.h"
Owen Andersonfa5cbd62009-07-03 19:42:02 +000022#include "llvm/LLVMContext.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000023#include "llvm/Module.h"
24#include "llvm/Pass.h"
25#include "llvm/Support/IRBuilder.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000026#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000027#include "llvm/Target/TargetData.h"
28#include "llvm/ADT/SmallPtrSet.h"
29#include "llvm/ADT/StringMap.h"
30#include "llvm/ADT/Statistic.h"
Daniel Dunbar473955f2009-07-29 22:00:43 +000031#include "llvm/ADT/STLExtras.h"
Chris Lattner56b4f2b2008-05-01 06:39:12 +000032#include "llvm/Support/Debug.h"
Daniel Dunbarf0443c12009-07-26 08:34:35 +000033#include "llvm/Support/raw_ostream.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000034#include "llvm/Config/config.h"
35using namespace llvm;
36
37STATISTIC(NumSimplified, "Number of library calls simplified");
Nick Lewycky0f8df9a2009-01-04 20:27:34 +000038STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000039
40//===----------------------------------------------------------------------===//
41// Optimizer Base Class
42//===----------------------------------------------------------------------===//
43
44/// This class is the abstract base class for the set of optimizations that
45/// corresponds to one library call.
46namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +000047class LibCallOptimization {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000048protected:
49 Function *Caller;
50 const TargetData *TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000051 LLVMContext* Context;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000052public:
Evan Chengeb8c6452010-03-24 20:19:04 +000053 LibCallOptimization() { }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000054 virtual ~LibCallOptimization() {}
55
56 /// CallOptimizer - This pure virtual method is implemented by base classes to
57 /// do various optimizations. If this returns null then no transformation was
58 /// performed. If it returns CI, then it transformed the call and CI is to be
59 /// deleted. If it returns something else, replace CI with the new value and
60 /// delete CI.
Eric Christopher37c8b862009-10-07 21:14:25 +000061 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
Eric Christopher7a61d702008-08-08 19:39:37 +000062 =0;
Eric Christopher37c8b862009-10-07 21:14:25 +000063
Dan Gohmanf14d9192009-08-18 00:48:13 +000064 Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000065 Caller = CI->getParent()->getParent();
Dan Gohmanf14d9192009-08-18 00:48:13 +000066 this->TD = TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000067 if (CI->getCalledFunction())
Owen Andersone922c022009-07-22 00:24:57 +000068 Context = &CI->getCalledFunction()->getContext();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000069 return CallOptimizer(CI->getCalledFunction(), CI, B);
70 }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000071};
72} // End anonymous namespace.
73
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000074
75//===----------------------------------------------------------------------===//
76// Helper Functions
77//===----------------------------------------------------------------------===//
78
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000079/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +000080/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000081static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
82 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
83 UI != E; ++UI) {
84 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
85 if (IC->isEquality())
86 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
87 if (C->isNullValue())
88 continue;
89 // Unknown instruction.
90 return false;
91 }
92 return true;
93}
94
95//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000096// String and Memory LibCall Optimizations
97//===----------------------------------------------------------------------===//
98
99//===---------------------------------------===//
100// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000101namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000102struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000103 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000104 // Verify the "strcat" function prototype.
105 const FunctionType *FT = Callee->getFunctionType();
106 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000107 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000108 FT->getParamType(0) != FT->getReturnType() ||
109 FT->getParamType(1) != FT->getReturnType())
110 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000111
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000112 // Extract some information from the instruction
113 Value *Dst = CI->getOperand(1);
114 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000115
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000116 // See if we can get the length of the input string.
117 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000118 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000119 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000120
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000121 // Handle the simple, do-nothing case: strcat(x, "") -> x
122 if (Len == 0)
123 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000124
125 // These optimizations require TargetData.
126 if (!TD) return 0;
127
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000128 EmitStrLenMemCpy(Src, Dst, Len, B);
129 return Dst;
130 }
131
132 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000133 // We need to find the end of the destination string. That's where the
134 // memory is to be moved to. We just generate a call to strlen.
Eric Christopherb6174e32010-03-05 22:25:30 +0000135 Value *DstLen = EmitStrLen(Dst, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000136
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000137 // Now that we have the destination's length, we must index into the
138 // destination's pointer to get the actual memcpy destination (end of
139 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000140 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000141
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000142 // We have enough information to now generate the memcpy call to do the
143 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000144 EmitMemCpy(CpyDst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000145 ConstantInt::get(TD->getIntPtrType(*Context), Len+1),
146 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000147 }
148};
149
150//===---------------------------------------===//
151// 'strncat' Optimizations
152
Chris Lattner3e8b6632009-09-02 06:11:42 +0000153struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000154 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
155 // Verify the "strncat" function prototype.
156 const FunctionType *FT = Callee->getFunctionType();
157 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000158 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000159 FT->getParamType(0) != FT->getReturnType() ||
160 FT->getParamType(1) != FT->getReturnType() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000161 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000162 return 0;
163
164 // Extract some information from the instruction
165 Value *Dst = CI->getOperand(1);
166 Value *Src = CI->getOperand(2);
167 uint64_t Len;
168
169 // We don't do anything if length is not constant
170 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
171 Len = LengthArg->getZExtValue();
172 else
173 return 0;
174
175 // See if we can get the length of the input string.
176 uint64_t SrcLen = GetStringLength(Src);
177 if (SrcLen == 0) return 0;
178 --SrcLen; // Unbias length.
179
180 // Handle the simple, do-nothing cases:
181 // strncat(x, "", c) -> x
182 // strncat(x, c, 0) -> x
183 if (SrcLen == 0 || Len == 0) return Dst;
184
Dan Gohmanf14d9192009-08-18 00:48:13 +0000185 // These optimizations require TargetData.
186 if (!TD) return 0;
187
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000188 // We don't optimize this case
189 if (Len < SrcLen) return 0;
190
191 // strncat(x, s, c) -> strcat(x, s)
192 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000193 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000194 return Dst;
195 }
196};
197
198//===---------------------------------------===//
199// 'strchr' Optimizations
200
Chris Lattner3e8b6632009-09-02 06:11:42 +0000201struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000202 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000203 // Verify the "strchr" function prototype.
204 const FunctionType *FT = Callee->getFunctionType();
205 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000206 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000207 FT->getParamType(0) != FT->getReturnType())
208 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000209
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000210 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000211
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000212 // If the second operand is non-constant, see if we can compute the length
213 // of the input string and turn this into memchr.
214 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
215 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000216 // These optimizations require TargetData.
217 if (!TD) return 0;
218
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000219 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000220 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000221 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000222
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000223 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000224 ConstantInt::get(TD->getIntPtrType(*Context), Len),
225 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000226 }
227
228 // Otherwise, the character is a constant, see if the first argument is
229 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000230 std::string Str;
231 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000232 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000233
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000234 // strchr can find the nul character.
235 Str += '\0';
236 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000237
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000238 // Compute the offset.
239 uint64_t i = 0;
240 while (1) {
241 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000242 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000243 // Did we find our match?
244 if (Str[i] == CharValue)
245 break;
246 ++i;
247 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000248
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000249 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000250 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000251 return B.CreateGEP(SrcStr, Idx, "strchr");
252 }
253};
254
255//===---------------------------------------===//
256// 'strcmp' Optimizations
257
Chris Lattner3e8b6632009-09-02 06:11:42 +0000258struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000259 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000260 // Verify the "strcmp" function prototype.
261 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000262 if (FT->getNumParams() != 2 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000263 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000264 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000265 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000266 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000267
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000268 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
269 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000270 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000271
Bill Wendling0582ae92009-03-13 04:39:26 +0000272 std::string Str1, Str2;
273 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
274 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000275
Bill Wendling0582ae92009-03-13 04:39:26 +0000276 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000277 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000278
Bill Wendling0582ae92009-03-13 04:39:26 +0000279 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000280 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000281
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000282 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000283 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000284 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000285 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000286
287 // strcmp(P, "x") -> memcmp(P, "x", 2)
288 uint64_t Len1 = GetStringLength(Str1P);
289 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000290 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000291 // These optimizations require TargetData.
292 if (!TD) return 0;
293
Nick Lewycky13a09e22008-12-21 00:19:21 +0000294 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000295 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000296 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000297 }
298
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000299 return 0;
300 }
301};
302
303//===---------------------------------------===//
304// 'strncmp' Optimizations
305
Chris Lattner3e8b6632009-09-02 06:11:42 +0000306struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000307 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000308 // Verify the "strncmp" function prototype.
309 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000310 if (FT->getNumParams() != 3 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000311 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000312 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000313 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000314 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000315 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000316
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000317 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
318 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000319 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000320
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000321 // Get the length argument if it is constant.
322 uint64_t Length;
323 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
324 Length = LengthArg->getZExtValue();
325 else
326 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000327
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000328 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000329 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000330
Bill Wendling0582ae92009-03-13 04:39:26 +0000331 std::string Str1, Str2;
332 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
333 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000334
Bill Wendling0582ae92009-03-13 04:39:26 +0000335 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000336 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000337
Bill Wendling0582ae92009-03-13 04:39:26 +0000338 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000339 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000340
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000341 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000342 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000343 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000344 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000345 return 0;
346 }
347};
348
349
350//===---------------------------------------===//
351// 'strcpy' Optimizations
352
Chris Lattner3e8b6632009-09-02 06:11:42 +0000353struct StrCpyOpt : public LibCallOptimization {
Evan Chengeb8c6452010-03-24 20:19:04 +0000354 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
355
356 StrCpyOpt(bool c) : OptChkCall(c) {}
357
Eric Christopher7a61d702008-08-08 19:39:37 +0000358 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000359 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000360 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000361 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000362 if (FT->getNumParams() != NumParams ||
363 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000364 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000365 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000366 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000367
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000368 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
369 if (Dst == Src) // strcpy(x,x) -> x
370 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000371
Dan Gohmanf14d9192009-08-18 00:48:13 +0000372 // These optimizations require TargetData.
373 if (!TD) return 0;
374
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000375 // See if we can get the length of the input string.
376 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000377 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000378
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000379 // We have enough information to now generate the memcpy call to do the
380 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000381 if (OptChkCall)
382 EmitMemCpyChk(Dst, Src,
383 ConstantInt::get(TD->getIntPtrType(*Context), Len),
384 CI->getOperand(3), B, TD);
385 else
386 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000387 ConstantInt::get(TD->getIntPtrType(*Context), Len),
388 1, false, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000389 return Dst;
390 }
391};
392
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000393//===---------------------------------------===//
394// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000395
Chris Lattner3e8b6632009-09-02 06:11:42 +0000396struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000397 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
398 const FunctionType *FT = Callee->getFunctionType();
399 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
400 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000401 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000402 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000403 return 0;
404
405 Value *Dst = CI->getOperand(1);
406 Value *Src = CI->getOperand(2);
407 Value *LenOp = CI->getOperand(3);
408
409 // See if we can get the length of the input string.
410 uint64_t SrcLen = GetStringLength(Src);
411 if (SrcLen == 0) return 0;
412 --SrcLen;
413
414 if (SrcLen == 0) {
415 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Mon P Wang20adc9d2010-04-04 03:10:48 +0000416 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'),
417 LenOp, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000418 return Dst;
419 }
420
421 uint64_t Len;
422 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
423 Len = LengthArg->getZExtValue();
424 else
425 return 0;
426
427 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
428
Dan Gohmanf14d9192009-08-18 00:48:13 +0000429 // These optimizations require TargetData.
430 if (!TD) return 0;
431
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000432 // Let strncpy handle the zero padding
433 if (Len > SrcLen+1) return 0;
434
435 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000436 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000437 ConstantInt::get(TD->getIntPtrType(*Context), Len),
438 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000439
440 return Dst;
441 }
442};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000443
444//===---------------------------------------===//
445// 'strlen' Optimizations
446
Chris Lattner3e8b6632009-09-02 06:11:42 +0000447struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000448 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000449 const FunctionType *FT = Callee->getFunctionType();
450 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000451 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000452 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000453 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000454
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000455 Value *Src = CI->getOperand(1);
456
457 // Constant folding: strlen("xyz") -> 3
458 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000459 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000460
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000461 // strlen(x) != 0 --> *x != 0
462 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000463 if (IsOnlyUsedInZeroEqualityComparison(CI))
464 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
465 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000466 }
467};
468
469//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000470// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000471
Chris Lattner3e8b6632009-09-02 06:11:42 +0000472struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000473 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
474 const FunctionType *FT = Callee->getFunctionType();
475 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000476 !FT->getParamType(0)->isPointerTy() ||
477 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000478 return 0;
479
480 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000481 if (isa<ConstantPointerNull>(EndPtr)) {
482 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000483 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000484 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000485
486 return 0;
487 }
488};
489
Chris Lattner24604112009-12-16 09:32:05 +0000490//===---------------------------------------===//
491// 'strstr' Optimizations
492
493struct StrStrOpt : public LibCallOptimization {
494 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
495 const FunctionType *FT = Callee->getFunctionType();
496 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000497 !FT->getParamType(0)->isPointerTy() ||
498 !FT->getParamType(1)->isPointerTy() ||
499 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000500 return 0;
501
502 // fold strstr(x, x) -> x.
503 if (CI->getOperand(1) == CI->getOperand(2))
504 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000505
Chris Lattner24604112009-12-16 09:32:05 +0000506 // See if either input string is a constant string.
507 std::string SearchStr, ToFindStr;
508 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
509 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000510
Chris Lattner24604112009-12-16 09:32:05 +0000511 // fold strstr(x, "") -> x.
512 if (HasStr2 && ToFindStr.empty())
513 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000514
Chris Lattner24604112009-12-16 09:32:05 +0000515 // If both strings are known, constant fold it.
516 if (HasStr1 && HasStr2) {
517 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000518
Chris Lattner24604112009-12-16 09:32:05 +0000519 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
520 return Constant::getNullValue(CI->getType());
521
522 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
523 Value *Result = CastToCStr(CI->getOperand(1), B);
524 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
525 return B.CreateBitCast(Result, CI->getType());
526 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000527
Chris Lattner24604112009-12-16 09:32:05 +0000528 // fold strstr(x, "y") -> strchr(x, 'y').
529 if (HasStr2 && ToFindStr.size() == 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000530 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B, TD),
Chris Lattner24604112009-12-16 09:32:05 +0000531 CI->getType());
532 return 0;
533 }
534};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000535
Nick Lewycky4c498412009-02-13 15:31:46 +0000536
537//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000538// 'memcmp' Optimizations
539
Chris Lattner3e8b6632009-09-02 06:11:42 +0000540struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000541 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000542 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000543 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
544 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000545 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000546 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000547
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000548 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000549
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000550 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000551 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000552
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000553 // Make sure we have a constant length.
554 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000555 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000556 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000557
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000558 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000559 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000560
561 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
562 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
563 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +0000564 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000565 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000566
Benjamin Kramer992a6372009-11-05 17:44:22 +0000567 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
568 std::string LHSStr, RHSStr;
569 if (GetConstantStringInfo(LHS, LHSStr) &&
570 GetConstantStringInfo(RHS, RHSStr)) {
571 // Make sure we're not reading out-of-bounds memory.
572 if (Len > LHSStr.length() || Len > RHSStr.length())
573 return 0;
574 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
575 return ConstantInt::get(CI->getType(), Ret);
576 }
577
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000578 return 0;
579 }
580};
581
582//===---------------------------------------===//
583// 'memcpy' Optimizations
584
Chris Lattner3e8b6632009-09-02 06:11:42 +0000585struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000586 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000587 // These optimizations require TargetData.
588 if (!TD) return 0;
589
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000590 const FunctionType *FT = Callee->getFunctionType();
591 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000592 !FT->getParamType(0)->isPointerTy() ||
593 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000594 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000595 return 0;
596
597 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000598 EmitMemCpy(CI->getOperand(1), CI->getOperand(2),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000599 CI->getOperand(3), 1, false, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000600 return CI->getOperand(1);
601 }
602};
603
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000604//===---------------------------------------===//
605// 'memmove' Optimizations
606
Chris Lattner3e8b6632009-09-02 06:11:42 +0000607struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000608 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000609 // These optimizations require TargetData.
610 if (!TD) return 0;
611
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000612 const FunctionType *FT = Callee->getFunctionType();
613 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000614 !FT->getParamType(0)->isPointerTy() ||
615 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000616 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000617 return 0;
618
619 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000620 EmitMemMove(CI->getOperand(1), CI->getOperand(2),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000621 CI->getOperand(3), 1, false, B, TD);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000622 return CI->getOperand(1);
623 }
624};
625
626//===---------------------------------------===//
627// 'memset' Optimizations
628
Chris Lattner3e8b6632009-09-02 06:11:42 +0000629struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000630 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000631 // These optimizations require TargetData.
632 if (!TD) return 0;
633
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000634 const FunctionType *FT = Callee->getFunctionType();
635 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000636 !FT->getParamType(0)->isPointerTy() ||
637 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000638 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000639 return 0;
640
641 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000642 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000643 false);
644 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), false, B, TD);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000645 return CI->getOperand(1);
646 }
647};
648
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000649//===----------------------------------------------------------------------===//
650// Math Library Optimizations
651//===----------------------------------------------------------------------===//
652
653//===---------------------------------------===//
654// 'pow*' Optimizations
655
Chris Lattner3e8b6632009-09-02 06:11:42 +0000656struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000657 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000658 const FunctionType *FT = Callee->getFunctionType();
659 // Just make sure this has 2 arguments of the same FP type, which match the
660 // result type.
661 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
662 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000663 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000664 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000665
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000666 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
667 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
668 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
669 return Op1C;
670 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000671 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000672 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000673
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000674 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
675 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000676
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000677 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000678 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000679
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000680 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000681 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
682 // This is faster than calling pow, and still handles negative zero
683 // and negative infinite correctly.
684 // TODO: In fast-math mode, this could be just sqrt(x).
685 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000686 Value *Inf = ConstantFP::getInfinity(CI->getType());
687 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000688 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
689 Callee->getAttributes());
690 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
691 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000692 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
693 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
694 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000695 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000696
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000697 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
698 return Op1;
699 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000700 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000701 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000702 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000703 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000704 return 0;
705 }
706};
707
708//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000709// 'exp2' Optimizations
710
Chris Lattner3e8b6632009-09-02 06:11:42 +0000711struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000712 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000713 const FunctionType *FT = Callee->getFunctionType();
714 // Just make sure this has 1 argument of FP type, which matches the
715 // result type.
716 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000717 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000718 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000719
Chris Lattnere818f772008-05-02 18:43:35 +0000720 Value *Op = CI->getOperand(1);
721 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
722 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
723 Value *LdExpArg = 0;
724 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
725 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000726 LdExpArg = B.CreateSExt(OpC->getOperand(0),
727 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000728 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
729 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000730 LdExpArg = B.CreateZExt(OpC->getOperand(0),
731 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000732 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000733
Chris Lattnere818f772008-05-02 18:43:35 +0000734 if (LdExpArg) {
735 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000736 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000737 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000738 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000739 Name = "ldexp";
740 else
741 Name = "ldexpl";
742
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000743 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000744 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000745 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000746
747 Module *M = Caller->getParent();
748 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000749 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000750 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000751 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
752 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
753 CI->setCallingConv(F->getCallingConv());
754
755 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000756 }
757 return 0;
758 }
759};
Chris Lattnere818f772008-05-02 18:43:35 +0000760
761//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000762// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
763
Chris Lattner3e8b6632009-09-02 06:11:42 +0000764struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000765 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000766 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000767 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
768 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000769 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000770
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000771 // If this is something like 'floor((double)floatval)', convert to floorf.
772 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000773 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000774 return 0;
775
776 // floor((double)floatval) -> (double)floorf(floatval)
777 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000778 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
779 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000780 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000781 }
782};
783
784//===----------------------------------------------------------------------===//
785// Integer Optimizations
786//===----------------------------------------------------------------------===//
787
788//===---------------------------------------===//
789// 'ffs*' Optimizations
790
Chris Lattner3e8b6632009-09-02 06:11:42 +0000791struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000792 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000793 const FunctionType *FT = Callee->getFunctionType();
794 // Just make sure this has 2 arguments of the same FP type, which match the
795 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000796 if (FT->getNumParams() != 1 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000797 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000798 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000799 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000800
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000801 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000802
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000803 // Constant fold.
804 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
805 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000806 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000807 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000808 CI->getValue().countTrailingZeros()+1);
809 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000810
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000811 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
812 const Type *ArgType = Op->getType();
813 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
814 Intrinsic::cttz, &ArgType, 1);
815 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000816 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000817 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000818
Owen Andersona7235ea2009-07-31 20:28:14 +0000819 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000820 return B.CreateSelect(Cond, V,
821 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000822 }
823};
824
825//===---------------------------------------===//
826// 'isdigit' Optimizations
827
Chris Lattner3e8b6632009-09-02 06:11:42 +0000828struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000829 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000830 const FunctionType *FT = Callee->getFunctionType();
831 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000832 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000833 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000834 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000835
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000836 // isdigit(c) -> (c-'0') <u 10
837 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000838 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000839 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000840 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000841 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000842 return B.CreateZExt(Op, CI->getType());
843 }
844};
845
846//===---------------------------------------===//
847// 'isascii' Optimizations
848
Chris Lattner3e8b6632009-09-02 06:11:42 +0000849struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000850 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000851 const FunctionType *FT = Callee->getFunctionType();
852 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000853 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000854 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000855 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000856
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000857 // isascii(c) -> c <u 128
858 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000859 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000860 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000861 return B.CreateZExt(Op, CI->getType());
862 }
863};
Eric Christopher37c8b862009-10-07 21:14:25 +0000864
Chris Lattner313f0e62008-06-09 08:26:51 +0000865//===---------------------------------------===//
866// 'abs', 'labs', 'llabs' Optimizations
867
Chris Lattner3e8b6632009-09-02 06:11:42 +0000868struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000869 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +0000870 const FunctionType *FT = Callee->getFunctionType();
871 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +0000872 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +0000873 FT->getParamType(0) != FT->getReturnType())
874 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000875
Chris Lattner313f0e62008-06-09 08:26:51 +0000876 // abs(x) -> x >s -1 ? x : -x
877 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000878 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +0000879 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +0000880 "ispos");
881 Value *Neg = B.CreateNeg(Op, "neg");
882 return B.CreateSelect(Pos, Op, Neg);
883 }
884};
Eric Christopher37c8b862009-10-07 21:14:25 +0000885
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000886
887//===---------------------------------------===//
888// 'toascii' Optimizations
889
Chris Lattner3e8b6632009-09-02 06:11:42 +0000890struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000891 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000892 const FunctionType *FT = Callee->getFunctionType();
893 // We require i32(i32)
894 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000895 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000896 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000897
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000898 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000899 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +0000900 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000901 }
902};
903
904//===----------------------------------------------------------------------===//
905// Formatting and IO Optimizations
906//===----------------------------------------------------------------------===//
907
908//===---------------------------------------===//
909// 'printf' Optimizations
910
Chris Lattner3e8b6632009-09-02 06:11:42 +0000911struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000912 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000913 // Require one fixed pointer argument and an integer/void result.
914 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000915 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
916 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000917 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000918 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000919
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000920 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000921 std::string FormatStr;
922 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
923 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000924
925 // Empty format string -> noop.
926 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +0000927 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000928 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000929
Chris Lattner74965f22009-11-09 04:57:04 +0000930 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
931 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000932 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +0000933 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000934 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +0000935 if (CI->use_empty()) return CI;
936 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000937 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000938
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000939 // printf("foo\n") --> puts("foo")
940 if (FormatStr[FormatStr.size()-1] == '\n' &&
941 FormatStr.find('%') == std::string::npos) { // no format characters.
942 // Create a string literal with no \n on it. We expect the constant merge
943 // pass to be run after this pass, to merge duplicate strings.
944 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000945 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +0000946 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
947 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +0000948 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000949 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000950 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000951 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000952
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000953 // Optimize specific format strings.
954 // printf("%c", chr) --> putchar(*(i8*)dst)
955 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
Duncan Sands1df98592010-02-16 11:11:14 +0000956 CI->getOperand(2)->getType()->isIntegerTy()) {
Eric Christopherb6174e32010-03-05 22:25:30 +0000957 Value *Res = EmitPutChar(CI->getOperand(2), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000958
Chris Lattner74965f22009-11-09 04:57:04 +0000959 if (CI->use_empty()) return CI;
960 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000961 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000962
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000963 // printf("%s\n", str) --> puts(str)
964 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
Duncan Sands1df98592010-02-16 11:11:14 +0000965 CI->getOperand(2)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000966 CI->use_empty()) {
Eric Christopherb6174e32010-03-05 22:25:30 +0000967 EmitPutS(CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000968 return CI;
969 }
970 return 0;
971 }
972};
973
974//===---------------------------------------===//
975// 'sprintf' Optimizations
976
Chris Lattner3e8b6632009-09-02 06:11:42 +0000977struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000978 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000979 // Require two fixed pointer arguments and an integer result.
980 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000981 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
982 !FT->getParamType(1)->isPointerTy() ||
983 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000984 return 0;
985
986 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000987 std::string FormatStr;
988 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
989 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000990
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000991 // If we just have a format string (nothing else crazy) transform it.
992 if (CI->getNumOperands() == 3) {
993 // Make sure there's no % in the constant array. We could try to handle
994 // %% -> % in the future if we cared.
995 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
996 if (FormatStr[i] == '%')
997 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +0000998
999 // These optimizations require TargetData.
1000 if (!TD) return 0;
1001
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001002 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1003 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Eric Christopherb6174e32010-03-05 22:25:30 +00001004 ConstantInt::get(TD->getIntPtrType(*Context),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001005 FormatStr.size()+1), 1, false, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001006 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001007 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001008
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001009 // The remaining optimizations require the format string to be "%s" or "%c"
1010 // and have an extra operand.
1011 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1012 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001013
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001014 // Decode the second character of the format string.
1015 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001016 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Duncan Sands1df98592010-02-16 11:11:14 +00001017 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001018 Value *V = B.CreateTrunc(CI->getOperand(3),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001019 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001020 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1021 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001022 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001023 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001024 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001025
Owen Andersoneed707b2009-07-24 23:12:02 +00001026 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001027 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001028
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001029 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001030 // These optimizations require TargetData.
1031 if (!TD) return 0;
1032
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001033 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Duncan Sands1df98592010-02-16 11:11:14 +00001034 if (!CI->getOperand(3)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001035
Eric Christopherb6174e32010-03-05 22:25:30 +00001036 Value *Len = EmitStrLen(CI->getOperand(3), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001037 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001038 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001039 "leninc");
Mon P Wang20adc9d2010-04-04 03:10:48 +00001040 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, false, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001041
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001042 // The sprintf result is the unincremented number of bytes in the string.
1043 return B.CreateIntCast(Len, CI->getType(), false);
1044 }
1045 return 0;
1046 }
1047};
1048
1049//===---------------------------------------===//
1050// 'fwrite' Optimizations
1051
Chris Lattner3e8b6632009-09-02 06:11:42 +00001052struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001053 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001054 // Require a pointer, an integer, an integer, a pointer, returning integer.
1055 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001056 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1057 !FT->getParamType(1)->isIntegerTy() ||
1058 !FT->getParamType(2)->isIntegerTy() ||
1059 !FT->getParamType(3)->isPointerTy() ||
1060 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001061 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001062
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001063 // Get the element size and count.
1064 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1065 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1066 if (!SizeC || !CountC) return 0;
1067 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001068
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001069 // If this is writing zero records, remove the call (it's a noop).
1070 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001071 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001072
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001073 // If this is writing one byte, turn it into fputc.
1074 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1075 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
Eric Christopherb6174e32010-03-05 22:25:30 +00001076 EmitFPutC(Char, CI->getOperand(4), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001077 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001078 }
1079
1080 return 0;
1081 }
1082};
1083
1084//===---------------------------------------===//
1085// 'fputs' Optimizations
1086
Chris Lattner3e8b6632009-09-02 06:11:42 +00001087struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001088 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001089 // These optimizations require TargetData.
1090 if (!TD) return 0;
1091
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001092 // Require two pointers. Also, we can't optimize if return value is used.
1093 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001094 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1095 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001096 !CI->use_empty())
1097 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001098
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001099 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1100 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001101 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001102 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001103 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Eric Christopherb6174e32010-03-05 22:25:30 +00001104 CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001105 return CI; // Known to have no uses (see above).
1106 }
1107};
1108
1109//===---------------------------------------===//
1110// 'fprintf' Optimizations
1111
Chris Lattner3e8b6632009-09-02 06:11:42 +00001112struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001113 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001114 // Require two fixed paramters as pointers and integer result.
1115 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001116 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1117 !FT->getParamType(1)->isPointerTy() ||
1118 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001119 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001120
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001121 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001122 std::string FormatStr;
1123 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1124 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001125
1126 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1127 if (CI->getNumOperands() == 3) {
1128 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1129 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001130 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001131
1132 // These optimizations require TargetData.
1133 if (!TD) return 0;
1134
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001135 EmitFWrite(CI->getOperand(2),
1136 ConstantInt::get(TD->getIntPtrType(*Context),
1137 FormatStr.size()),
Eric Christopherb6174e32010-03-05 22:25:30 +00001138 CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001139 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001140 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001141
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001142 // The remaining optimizations require the format string to be "%s" or "%c"
1143 // and have an extra operand.
1144 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1145 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001146
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001147 // Decode the second character of the format string.
1148 if (FormatStr[1] == 'c') {
1149 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
Duncan Sands1df98592010-02-16 11:11:14 +00001150 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
Eric Christopherb6174e32010-03-05 22:25:30 +00001151 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001152 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001153 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001154
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001155 if (FormatStr[1] == 's') {
1156 // fprintf(F, "%s", str) -> fputs(str, F)
Duncan Sands1df98592010-02-16 11:11:14 +00001157 if (!CI->getOperand(3)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001158 return 0;
Eric Christopherb6174e32010-03-05 22:25:30 +00001159 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001160 return CI;
1161 }
1162 return 0;
1163 }
1164};
1165
Bill Wendlingac178222008-05-05 21:37:59 +00001166} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001167
1168//===----------------------------------------------------------------------===//
1169// SimplifyLibCalls Pass Implementation
1170//===----------------------------------------------------------------------===//
1171
1172namespace {
1173 /// This pass optimizes well known library functions from libc and libm.
1174 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001175 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001176 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001177 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001178 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
Evan Cheng0289b412010-03-23 15:48:04 +00001179 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
1180 StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001181 StrToOpt StrTo; StrStrOpt StrStr;
1182 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001183 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001184 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001185 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001186 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1187 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001188 // Formatting and IO Optimizations
1189 SPrintFOpt SPrintF; PrintFOpt PrintF;
1190 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001191
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001192 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001193 public:
1194 static char ID; // Pass identification
Evan Chengeb8c6452010-03-24 20:19:04 +00001195 SimplifyLibCalls() : FunctionPass(&ID), StrCpy(false), StrCpyChk(true) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001196 void InitOptimizations();
1197 bool runOnFunction(Function &F);
1198
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001199 void setDoesNotAccessMemory(Function &F);
1200 void setOnlyReadsMemory(Function &F);
1201 void setDoesNotThrow(Function &F);
1202 void setDoesNotCapture(Function &F, unsigned n);
1203 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001204 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001205
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001206 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001207 }
1208 };
1209 char SimplifyLibCalls::ID = 0;
1210} // end anonymous namespace.
1211
1212static RegisterPass<SimplifyLibCalls>
1213X("simplify-libcalls", "Simplify well-known library calls");
1214
1215// Public interface to the Simplify LibCalls pass.
1216FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001217 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001218}
1219
1220/// Optimizations - Populate the Optimizations map with all the optimizations
1221/// we know.
1222void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001223 // String and Memory LibCall Optimizations
1224 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001225 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001226 Optimizations["strchr"] = &StrChr;
1227 Optimizations["strcmp"] = &StrCmp;
1228 Optimizations["strncmp"] = &StrNCmp;
1229 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001230 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001231 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001232 Optimizations["strtol"] = &StrTo;
1233 Optimizations["strtod"] = &StrTo;
1234 Optimizations["strtof"] = &StrTo;
1235 Optimizations["strtoul"] = &StrTo;
1236 Optimizations["strtoll"] = &StrTo;
1237 Optimizations["strtold"] = &StrTo;
1238 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001239 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001240 Optimizations["memcmp"] = &MemCmp;
1241 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001242 Optimizations["memmove"] = &MemMove;
1243 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001244
Evan Cheng0289b412010-03-23 15:48:04 +00001245 // _chk variants of String and Memory LibCall Optimizations.
Evan Cheng0289b412010-03-23 15:48:04 +00001246 Optimizations["__strcpy_chk"] = &StrCpyChk;
1247
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001248 // Math Library Optimizations
1249 Optimizations["powf"] = &Pow;
1250 Optimizations["pow"] = &Pow;
1251 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001252 Optimizations["llvm.pow.f32"] = &Pow;
1253 Optimizations["llvm.pow.f64"] = &Pow;
1254 Optimizations["llvm.pow.f80"] = &Pow;
1255 Optimizations["llvm.pow.f128"] = &Pow;
1256 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001257 Optimizations["exp2l"] = &Exp2;
1258 Optimizations["exp2"] = &Exp2;
1259 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001260 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1261 Optimizations["llvm.exp2.f128"] = &Exp2;
1262 Optimizations["llvm.exp2.f80"] = &Exp2;
1263 Optimizations["llvm.exp2.f64"] = &Exp2;
1264 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001265
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001266#ifdef HAVE_FLOORF
1267 Optimizations["floor"] = &UnaryDoubleFP;
1268#endif
1269#ifdef HAVE_CEILF
1270 Optimizations["ceil"] = &UnaryDoubleFP;
1271#endif
1272#ifdef HAVE_ROUNDF
1273 Optimizations["round"] = &UnaryDoubleFP;
1274#endif
1275#ifdef HAVE_RINTF
1276 Optimizations["rint"] = &UnaryDoubleFP;
1277#endif
1278#ifdef HAVE_NEARBYINTF
1279 Optimizations["nearbyint"] = &UnaryDoubleFP;
1280#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001281
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001282 // Integer Optimizations
1283 Optimizations["ffs"] = &FFS;
1284 Optimizations["ffsl"] = &FFS;
1285 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001286 Optimizations["abs"] = &Abs;
1287 Optimizations["labs"] = &Abs;
1288 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001289 Optimizations["isdigit"] = &IsDigit;
1290 Optimizations["isascii"] = &IsAscii;
1291 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001292
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001293 // Formatting and IO Optimizations
1294 Optimizations["sprintf"] = &SPrintF;
1295 Optimizations["printf"] = &PrintF;
1296 Optimizations["fwrite"] = &FWrite;
1297 Optimizations["fputs"] = &FPuts;
1298 Optimizations["fprintf"] = &FPrintF;
1299}
1300
1301
1302/// runOnFunction - Top level algorithm.
1303///
1304bool SimplifyLibCalls::runOnFunction(Function &F) {
1305 if (Optimizations.empty())
1306 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001307
Dan Gohmanf14d9192009-08-18 00:48:13 +00001308 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001309
Owen Andersone922c022009-07-22 00:24:57 +00001310 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001311
1312 bool Changed = false;
1313 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1314 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1315 // Ignore non-calls.
1316 CallInst *CI = dyn_cast<CallInst>(I++);
1317 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001318
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001319 // Ignore indirect calls and calls to non-external functions.
1320 Function *Callee = CI->getCalledFunction();
1321 if (Callee == 0 || !Callee->isDeclaration() ||
1322 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1323 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001324
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001325 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001326 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1327 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001328
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001329 // Set the builder to the instruction after the call.
1330 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001331
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001332 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001333 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001334 if (Result == 0) continue;
1335
David Greene6a6b90e2010-01-05 01:27:21 +00001336 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1337 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001338
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001339 // Something changed!
1340 Changed = true;
1341 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001342
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001343 // Inspect the instruction after the call (which was potentially just
1344 // added) next.
1345 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001346
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001347 if (CI != Result && !CI->use_empty()) {
1348 CI->replaceAllUsesWith(Result);
1349 if (!Result->hasName())
1350 Result->takeName(CI);
1351 }
1352 CI->eraseFromParent();
1353 }
1354 }
1355 return Changed;
1356}
1357
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001358// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001359
1360void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1361 if (!F.doesNotAccessMemory()) {
1362 F.setDoesNotAccessMemory();
1363 ++NumAnnotated;
1364 Modified = true;
1365 }
1366}
1367void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1368 if (!F.onlyReadsMemory()) {
1369 F.setOnlyReadsMemory();
1370 ++NumAnnotated;
1371 Modified = true;
1372 }
1373}
1374void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1375 if (!F.doesNotThrow()) {
1376 F.setDoesNotThrow();
1377 ++NumAnnotated;
1378 Modified = true;
1379 }
1380}
1381void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1382 if (!F.doesNotCapture(n)) {
1383 F.setDoesNotCapture(n);
1384 ++NumAnnotated;
1385 Modified = true;
1386 }
1387}
1388void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1389 if (!F.doesNotAlias(n)) {
1390 F.setDoesNotAlias(n);
1391 ++NumAnnotated;
1392 Modified = true;
1393 }
1394}
1395
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001396/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001397///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001398bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001399 Modified = false;
1400 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1401 Function &F = *I;
1402 if (!F.isDeclaration())
1403 continue;
1404
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001405 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001406 continue;
1407
1408 const FunctionType *FTy = F.getFunctionType();
1409
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001410 StringRef Name = F.getName();
1411 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001412 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001413 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001414 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001415 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001416 continue;
1417 setOnlyReadsMemory(F);
1418 setDoesNotThrow(F);
1419 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001420 } else if (Name == "strchr" ||
1421 Name == "strrchr") {
1422 if (FTy->getNumParams() != 2 ||
1423 !FTy->getParamType(0)->isPointerTy() ||
1424 !FTy->getParamType(1)->isIntegerTy())
1425 continue;
1426 setOnlyReadsMemory(F);
1427 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001428 } else if (Name == "strcpy" ||
1429 Name == "stpcpy" ||
1430 Name == "strcat" ||
1431 Name == "strtol" ||
1432 Name == "strtod" ||
1433 Name == "strtof" ||
1434 Name == "strtoul" ||
1435 Name == "strtoll" ||
1436 Name == "strtold" ||
1437 Name == "strncat" ||
1438 Name == "strncpy" ||
1439 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001440 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001441 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001442 continue;
1443 setDoesNotThrow(F);
1444 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001445 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001446 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001447 !FTy->getParamType(0)->isPointerTy() ||
1448 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001449 continue;
1450 setDoesNotThrow(F);
1451 setDoesNotCapture(F, 1);
1452 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001453 } else if (Name == "strcmp" ||
1454 Name == "strspn" ||
1455 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001456 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001457 Name == "strcoll" ||
1458 Name == "strcasecmp" ||
1459 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001460 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001461 !FTy->getParamType(0)->isPointerTy() ||
1462 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001463 continue;
1464 setOnlyReadsMemory(F);
1465 setDoesNotThrow(F);
1466 setDoesNotCapture(F, 1);
1467 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001468 } else if (Name == "strstr" ||
1469 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001470 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001471 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001472 continue;
1473 setOnlyReadsMemory(F);
1474 setDoesNotThrow(F);
1475 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001476 } else if (Name == "strtok" ||
1477 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001478 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001479 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001480 continue;
1481 setDoesNotThrow(F);
1482 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001483 } else if (Name == "scanf" ||
1484 Name == "setbuf" ||
1485 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001486 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001487 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001488 continue;
1489 setDoesNotThrow(F);
1490 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001491 } else if (Name == "strdup" ||
1492 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001493 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001494 !FTy->getReturnType()->isPointerTy() ||
1495 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001496 continue;
1497 setDoesNotThrow(F);
1498 setDoesNotAlias(F, 0);
1499 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001500 } else if (Name == "stat" ||
1501 Name == "sscanf" ||
1502 Name == "sprintf" ||
1503 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001504 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001505 !FTy->getParamType(0)->isPointerTy() ||
1506 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001507 continue;
1508 setDoesNotThrow(F);
1509 setDoesNotCapture(F, 1);
1510 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001511 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001512 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001513 !FTy->getParamType(0)->isPointerTy() ||
1514 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001515 continue;
1516 setDoesNotThrow(F);
1517 setDoesNotCapture(F, 1);
1518 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001519 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001520 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001521 !FTy->getParamType(1)->isPointerTy() ||
1522 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001523 continue;
1524 setDoesNotThrow(F);
1525 setDoesNotCapture(F, 2);
1526 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001527 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001528 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001529 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001530 continue;
1531 // May throw; "system" is a valid pthread cancellation point.
1532 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001533 }
1534 break;
1535 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001536 if (Name == "malloc") {
1537 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001538 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001539 continue;
1540 setDoesNotThrow(F);
1541 setDoesNotAlias(F, 0);
1542 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001543 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001544 !FTy->getParamType(0)->isPointerTy() ||
1545 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001546 continue;
1547 setOnlyReadsMemory(F);
1548 setDoesNotThrow(F);
1549 setDoesNotCapture(F, 1);
1550 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001551 } else if (Name == "memchr" ||
1552 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001553 if (FTy->getNumParams() != 3)
1554 continue;
1555 setOnlyReadsMemory(F);
1556 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001557 } else if (Name == "modf" ||
1558 Name == "modff" ||
1559 Name == "modfl" ||
1560 Name == "memcpy" ||
1561 Name == "memccpy" ||
1562 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001563 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001564 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001565 continue;
1566 setDoesNotThrow(F);
1567 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001568 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001569 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001570 continue;
1571 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001572 } else if (Name == "mkdir" ||
1573 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001574 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001575 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001576 continue;
1577 setDoesNotThrow(F);
1578 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001579 }
1580 break;
1581 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001582 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001583 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001584 !FTy->getParamType(0)->isPointerTy() ||
1585 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001586 continue;
1587 setDoesNotThrow(F);
1588 setDoesNotAlias(F, 0);
1589 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001590 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001591 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001592 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001593 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001594 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001595 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001596 } else if (Name == "rmdir" ||
1597 Name == "rewind" ||
1598 Name == "remove" ||
1599 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001600 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001601 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001602 continue;
1603 setDoesNotThrow(F);
1604 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001605 } else if (Name == "rename" ||
1606 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001607 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001608 !FTy->getParamType(0)->isPointerTy() ||
1609 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001610 continue;
1611 setDoesNotThrow(F);
1612 setDoesNotCapture(F, 1);
1613 setDoesNotCapture(F, 2);
1614 }
1615 break;
1616 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001617 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001618 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001619 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001620 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001621 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001622 setDoesNotCapture(F, 2);
1623 }
1624 break;
1625 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001626 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001627 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001628 !FTy->getParamType(0)->isPointerTy() ||
1629 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001630 continue;
1631 setDoesNotThrow(F);
1632 setDoesNotCapture(F, 1);
1633 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001634 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001635 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001636 !FTy->getParamType(0)->isPointerTy() ||
1637 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001638 continue;
1639 setDoesNotThrow(F);
1640 setOnlyReadsMemory(F);
1641 setDoesNotCapture(F, 1);
1642 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001643 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001644 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001645 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001646 continue;
1647 setDoesNotThrow(F);
1648 setDoesNotCapture(F, 1);
1649 }
1650 break;
1651 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001652 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001653 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001654 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001655 continue;
1656 setDoesNotThrow(F);
1657 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001658 } else if (Name == "chmod" ||
1659 Name == "chown" ||
1660 Name == "ctermid" ||
1661 Name == "clearerr" ||
1662 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001663 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001664 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001665 continue;
1666 setDoesNotThrow(F);
1667 setDoesNotCapture(F, 1);
1668 }
1669 break;
1670 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001671 if (Name == "atoi" ||
1672 Name == "atol" ||
1673 Name == "atof" ||
1674 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001675 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001676 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001677 continue;
1678 setDoesNotThrow(F);
1679 setOnlyReadsMemory(F);
1680 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001681 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001682 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001683 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001684 continue;
1685 setDoesNotThrow(F);
1686 setDoesNotCapture(F, 1);
1687 }
1688 break;
1689 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001690 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001691 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001692 !FTy->getReturnType()->isPointerTy() ||
1693 !FTy->getParamType(0)->isPointerTy() ||
1694 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001695 continue;
1696 setDoesNotThrow(F);
1697 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001698 setDoesNotCapture(F, 1);
1699 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001700 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001701 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001702 !FTy->getReturnType()->isPointerTy() ||
1703 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001704 continue;
1705 setDoesNotThrow(F);
1706 setDoesNotAlias(F, 0);
1707 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001708 } else if (Name == "feof" ||
1709 Name == "free" ||
1710 Name == "fseek" ||
1711 Name == "ftell" ||
1712 Name == "fgetc" ||
1713 Name == "fseeko" ||
1714 Name == "ftello" ||
1715 Name == "fileno" ||
1716 Name == "fflush" ||
1717 Name == "fclose" ||
1718 Name == "fsetpos" ||
1719 Name == "flockfile" ||
1720 Name == "funlockfile" ||
1721 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001722 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001723 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001724 continue;
1725 setDoesNotThrow(F);
1726 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001727 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001728 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001729 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001730 continue;
1731 setDoesNotThrow(F);
1732 setDoesNotCapture(F, 1);
1733 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001734 } else if (Name == "fputc" ||
1735 Name == "fstat" ||
1736 Name == "frexp" ||
1737 Name == "frexpf" ||
1738 Name == "frexpl" ||
1739 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001740 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001741 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001742 continue;
1743 setDoesNotThrow(F);
1744 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001745 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001746 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001747 !FTy->getParamType(0)->isPointerTy() ||
1748 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001749 continue;
1750 setDoesNotThrow(F);
1751 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001752 } else if (Name == "fread" ||
1753 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001754 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001755 !FTy->getParamType(0)->isPointerTy() ||
1756 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001757 continue;
1758 setDoesNotThrow(F);
1759 setDoesNotCapture(F, 1);
1760 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001761 } else if (Name == "fputs" ||
1762 Name == "fscanf" ||
1763 Name == "fprintf" ||
1764 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001765 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001766 !FTy->getParamType(0)->isPointerTy() ||
1767 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001768 continue;
1769 setDoesNotThrow(F);
1770 setDoesNotCapture(F, 1);
1771 setDoesNotCapture(F, 2);
1772 }
1773 break;
1774 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001775 if (Name == "getc" ||
1776 Name == "getlogin_r" ||
1777 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001778 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001779 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001780 continue;
1781 setDoesNotThrow(F);
1782 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001783 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001784 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001785 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001786 continue;
1787 setDoesNotThrow(F);
1788 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001789 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001790 } else if (Name == "gets" ||
1791 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001792 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001793 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001794 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001795 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001796 continue;
1797 setDoesNotThrow(F);
1798 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001799 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001800 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001801 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001802 continue;
1803 setDoesNotThrow(F);
1804 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001805 }
1806 break;
1807 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001808 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001809 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001810 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001811 continue;
1812 setDoesNotThrow(F);
1813 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001814 } else if (Name == "uname" ||
1815 Name == "unlink" ||
1816 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001817 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001818 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001819 continue;
1820 setDoesNotThrow(F);
1821 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001822 } else if (Name == "utime" ||
1823 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001824 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001825 !FTy->getParamType(0)->isPointerTy() ||
1826 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001827 continue;
1828 setDoesNotThrow(F);
1829 setDoesNotCapture(F, 1);
1830 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001831 }
1832 break;
1833 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001834 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001835 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001836 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001837 continue;
1838 setDoesNotThrow(F);
1839 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001840 } else if (Name == "puts" ||
1841 Name == "printf" ||
1842 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001843 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001844 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001845 continue;
1846 setDoesNotThrow(F);
1847 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001848 } else if (Name == "pread" ||
1849 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001850 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001851 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001852 continue;
1853 // May throw; these are valid pthread cancellation points.
1854 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001855 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001856 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001857 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001858 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001859 !FTy->getReturnType()->isPointerTy() ||
1860 !FTy->getParamType(0)->isPointerTy() ||
1861 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001862 continue;
1863 setDoesNotThrow(F);
1864 setDoesNotAlias(F, 0);
1865 setDoesNotCapture(F, 1);
1866 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001867 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001868 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001869 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001870 continue;
1871 setDoesNotThrow(F);
1872 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001873 }
1874 break;
1875 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001876 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001877 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001878 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001879 continue;
1880 setDoesNotThrow(F);
1881 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001882 } else if (Name == "vsscanf" ||
1883 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001884 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001885 !FTy->getParamType(1)->isPointerTy() ||
1886 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001887 continue;
1888 setDoesNotThrow(F);
1889 setDoesNotCapture(F, 1);
1890 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001891 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00001892 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001893 continue;
1894 setDoesNotThrow(F);
1895 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001896 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001897 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001898 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001899 continue;
1900 setDoesNotThrow(F);
1901 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001902 } else if (Name == "vfprintf" ||
1903 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001904 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001905 !FTy->getParamType(0)->isPointerTy() ||
1906 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001907 continue;
1908 setDoesNotThrow(F);
1909 setDoesNotCapture(F, 1);
1910 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001911 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001912 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001913 !FTy->getParamType(0)->isPointerTy() ||
1914 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001915 continue;
1916 setDoesNotThrow(F);
1917 setDoesNotCapture(F, 1);
1918 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001919 }
1920 break;
1921 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001922 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001923 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001924 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001925 continue;
1926 // May throw; "open" is a valid pthread cancellation point.
1927 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001928 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001929 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001930 !FTy->getReturnType()->isPointerTy() ||
1931 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001932 continue;
1933 setDoesNotThrow(F);
1934 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00001935 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001936 }
1937 break;
1938 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001939 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00001940 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001941 continue;
1942 setDoesNotThrow(F);
1943 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001944 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001945 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001946 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001947 continue;
1948 setDoesNotThrow(F);
1949 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001950 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001951 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001952 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001953 if (Name == "htonl" ||
1954 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001955 setDoesNotThrow(F);
1956 setDoesNotAccessMemory(F);
1957 }
1958 break;
1959 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001960 if (Name == "ntohl" ||
1961 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001962 setDoesNotThrow(F);
1963 setDoesNotAccessMemory(F);
1964 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001965 break;
1966 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001967 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001968 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001969 !FTy->getParamType(0)->isPointerTy() ||
1970 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001971 continue;
1972 setDoesNotThrow(F);
1973 setDoesNotCapture(F, 1);
1974 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001975 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001976 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001977 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001978 continue;
1979 setDoesNotThrow(F);
1980 setDoesNotCapture(F, 1);
1981 }
1982 break;
1983 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001984 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001985 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001986 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001987 continue;
1988 // May throw; places call through function pointer.
1989 setDoesNotCapture(F, 4);
1990 }
1991 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001992 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001993 if (Name == "__strdup" ||
1994 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001995 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001996 !FTy->getReturnType()->isPointerTy() ||
1997 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001998 continue;
1999 setDoesNotThrow(F);
2000 setDoesNotAlias(F, 0);
2001 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002002 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002003 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002004 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002005 continue;
2006 setDoesNotThrow(F);
2007 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002008 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002009 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002010 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002011 continue;
2012 setDoesNotThrow(F);
2013 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002014 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002015 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002016 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002017 continue;
2018 setDoesNotThrow(F);
2019 setDoesNotCapture(F, 2);
2020 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002021 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002022 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002023 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002024 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002025 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002026 continue;
2027 setDoesNotThrow(F);
2028 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002029 } else if (Name == "\1stat64" ||
2030 Name == "\1lstat64" ||
2031 Name == "\1statvfs64" ||
2032 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002033 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002034 !FTy->getParamType(0)->isPointerTy() ||
2035 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002036 continue;
2037 setDoesNotThrow(F);
2038 setDoesNotCapture(F, 1);
2039 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002040 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002041 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002042 !FTy->getReturnType()->isPointerTy() ||
2043 !FTy->getParamType(0)->isPointerTy() ||
2044 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002045 continue;
2046 setDoesNotThrow(F);
2047 setDoesNotAlias(F, 0);
2048 setDoesNotCapture(F, 1);
2049 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002050 } else if (Name == "\1fseeko64" ||
2051 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002052 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002053 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002054 continue;
2055 setDoesNotThrow(F);
2056 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002057 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002058 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002059 continue;
2060 setDoesNotThrow(F);
2061 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002062 } else if (Name == "\1fstat64" ||
2063 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002064 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002065 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002066 continue;
2067 setDoesNotThrow(F);
2068 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002069 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002070 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002071 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002072 continue;
2073 // May throw; "open" is a valid pthread cancellation point.
2074 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002075 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002076 break;
2077 }
2078 }
2079 return Modified;
2080}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002081
2082// TODO:
2083// Additional cases that we need to add to this file:
2084//
2085// cbrt:
2086// * cbrt(expN(X)) -> expN(x/3)
2087// * cbrt(sqrt(x)) -> pow(x,1/6)
2088// * cbrt(sqrt(x)) -> pow(x,1/9)
2089//
2090// cos, cosf, cosl:
2091// * cos(-x) -> cos(x)
2092//
2093// exp, expf, expl:
2094// * exp(log(x)) -> x
2095//
2096// log, logf, logl:
2097// * log(exp(x)) -> x
2098// * log(x**y) -> y*log(x)
2099// * log(exp(y)) -> y*log(e)
2100// * log(exp2(y)) -> y*log(2)
2101// * log(exp10(y)) -> y*log(10)
2102// * log(sqrt(x)) -> 0.5*log(x)
2103// * log(pow(x,y)) -> y*log(x)
2104//
2105// lround, lroundf, lroundl:
2106// * lround(cnst) -> cnst'
2107//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002108// pow, powf, powl:
2109// * pow(exp(x),y) -> exp(x*y)
2110// * pow(sqrt(x),y) -> pow(x,y*0.5)
2111// * pow(pow(x,y),z)-> pow(x,y*z)
2112//
2113// puts:
2114// * puts("") -> putchar("\n")
2115//
2116// round, roundf, roundl:
2117// * round(cnst) -> cnst'
2118//
2119// signbit:
2120// * signbit(cnst) -> cnst'
2121// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2122//
2123// sqrt, sqrtf, sqrtl:
2124// * sqrt(expN(x)) -> expN(x*0.5)
2125// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2126// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2127//
2128// stpcpy:
2129// * stpcpy(str, "literal") ->
2130// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2131// strrchr:
2132// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2133// (if c is a constant integer and s is a constant string)
2134// * strrchr(s1,0) -> strchr(s1,0)
2135//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002136// strpbrk:
2137// * strpbrk(s,a) -> offset_in_for(s,a)
2138// (if s and a are both constant strings)
2139// * strpbrk(s,"") -> 0
2140// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2141//
2142// strspn, strcspn:
2143// * strspn(s,a) -> const_int (if both args are constant)
2144// * strspn("",a) -> 0
2145// * strspn(s,"") -> 0
2146// * strcspn(s,a) -> const_int (if both args are constant)
2147// * strcspn("",a) -> 0
2148// * strcspn(s,"") -> strlen(a)
2149//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002150// tan, tanf, tanl:
2151// * tan(atan(x)) -> x
2152//
2153// trunc, truncf, truncl:
2154// * trunc(cnst) -> cnst'
2155//
2156//