blob: 5941ea6571b76bc58114ea0759b46f394faed0cb [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,
Eric Christopherb6174e32010-03-05 22:25:30 +0000145 ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000146 }
147};
148
149//===---------------------------------------===//
150// 'strncat' Optimizations
151
Chris Lattner3e8b6632009-09-02 06:11:42 +0000152struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000153 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
154 // Verify the "strncat" function prototype.
155 const FunctionType *FT = Callee->getFunctionType();
156 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000157 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000158 FT->getParamType(0) != FT->getReturnType() ||
159 FT->getParamType(1) != FT->getReturnType() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000160 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000161 return 0;
162
163 // Extract some information from the instruction
164 Value *Dst = CI->getOperand(1);
165 Value *Src = CI->getOperand(2);
166 uint64_t Len;
167
168 // We don't do anything if length is not constant
169 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
170 Len = LengthArg->getZExtValue();
171 else
172 return 0;
173
174 // See if we can get the length of the input string.
175 uint64_t SrcLen = GetStringLength(Src);
176 if (SrcLen == 0) return 0;
177 --SrcLen; // Unbias length.
178
179 // Handle the simple, do-nothing cases:
180 // strncat(x, "", c) -> x
181 // strncat(x, c, 0) -> x
182 if (SrcLen == 0 || Len == 0) return Dst;
183
Dan Gohmanf14d9192009-08-18 00:48:13 +0000184 // These optimizations require TargetData.
185 if (!TD) return 0;
186
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000187 // We don't optimize this case
188 if (Len < SrcLen) return 0;
189
190 // strncat(x, s, c) -> strcat(x, s)
191 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000192 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000193 return Dst;
194 }
195};
196
197//===---------------------------------------===//
198// 'strchr' Optimizations
199
Chris Lattner3e8b6632009-09-02 06:11:42 +0000200struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000201 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000202 // Verify the "strchr" function prototype.
203 const FunctionType *FT = Callee->getFunctionType();
204 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000205 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000206 FT->getParamType(0) != FT->getReturnType())
207 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000208
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000209 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000210
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000211 // If the second operand is non-constant, see if we can compute the length
212 // of the input string and turn this into memchr.
213 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
214 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000215 // These optimizations require TargetData.
216 if (!TD) return 0;
217
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000218 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000219 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000220 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000221
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000222 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000223 ConstantInt::get(TD->getIntPtrType(*Context), Len),
224 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000225 }
226
227 // Otherwise, the character is a constant, see if the first argument is
228 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000229 std::string Str;
230 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000231 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000232
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000233 // strchr can find the nul character.
234 Str += '\0';
235 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000236
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000237 // Compute the offset.
238 uint64_t i = 0;
239 while (1) {
240 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000241 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000242 // Did we find our match?
243 if (Str[i] == CharValue)
244 break;
245 ++i;
246 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000247
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000248 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000249 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000250 return B.CreateGEP(SrcStr, Idx, "strchr");
251 }
252};
253
254//===---------------------------------------===//
255// 'strcmp' Optimizations
256
Chris Lattner3e8b6632009-09-02 06:11:42 +0000257struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000258 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000259 // Verify the "strcmp" function prototype.
260 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000261 if (FT->getNumParams() != 2 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000262 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000263 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000264 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000265 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000266
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000267 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
268 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000269 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000270
Bill Wendling0582ae92009-03-13 04:39:26 +0000271 std::string Str1, Str2;
272 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
273 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000274
Bill Wendling0582ae92009-03-13 04:39:26 +0000275 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000276 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000277
Bill Wendling0582ae92009-03-13 04:39:26 +0000278 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000279 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000280
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000281 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000282 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000283 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000284 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000285
286 // strcmp(P, "x") -> memcmp(P, "x", 2)
287 uint64_t Len1 = GetStringLength(Str1P);
288 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000289 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000290 // These optimizations require TargetData.
291 if (!TD) return 0;
292
Nick Lewycky13a09e22008-12-21 00:19:21 +0000293 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000294 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000295 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000296 }
297
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000298 return 0;
299 }
300};
301
302//===---------------------------------------===//
303// 'strncmp' Optimizations
304
Chris Lattner3e8b6632009-09-02 06:11:42 +0000305struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000306 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000307 // Verify the "strncmp" function prototype.
308 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000309 if (FT->getNumParams() != 3 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000310 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000311 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000312 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000313 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000314 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000315
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000316 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
317 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000318 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000319
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000320 // Get the length argument if it is constant.
321 uint64_t Length;
322 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
323 Length = LengthArg->getZExtValue();
324 else
325 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000326
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000327 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000328 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000329
Bill Wendling0582ae92009-03-13 04:39:26 +0000330 std::string Str1, Str2;
331 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
332 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000333
Bill Wendling0582ae92009-03-13 04:39:26 +0000334 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000335 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000336
Bill Wendling0582ae92009-03-13 04:39:26 +0000337 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000338 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000339
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000340 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000341 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000342 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000343 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000344 return 0;
345 }
346};
347
348
349//===---------------------------------------===//
350// 'strcpy' Optimizations
351
Chris Lattner3e8b6632009-09-02 06:11:42 +0000352struct StrCpyOpt : public LibCallOptimization {
Evan Chengeb8c6452010-03-24 20:19:04 +0000353 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
354
355 StrCpyOpt(bool c) : OptChkCall(c) {}
356
Eric Christopher7a61d702008-08-08 19:39:37 +0000357 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000358 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000359 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000360 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000361 if (FT->getNumParams() != NumParams ||
362 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000363 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000364 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000365 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000366
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000367 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
368 if (Dst == Src) // strcpy(x,x) -> x
369 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000370
Dan Gohmanf14d9192009-08-18 00:48:13 +0000371 // These optimizations require TargetData.
372 if (!TD) return 0;
373
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000374 // See if we can get the length of the input string.
375 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000376 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000377
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000378 // We have enough information to now generate the memcpy call to do the
379 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000380 if (OptChkCall)
381 EmitMemCpyChk(Dst, Src,
382 ConstantInt::get(TD->getIntPtrType(*Context), Len),
383 CI->getOperand(3), B, TD);
384 else
385 EmitMemCpy(Dst, Src,
386 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000387 return Dst;
388 }
389};
390
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000391//===---------------------------------------===//
392// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000393
Chris Lattner3e8b6632009-09-02 06:11:42 +0000394struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000395 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
396 const FunctionType *FT = Callee->getFunctionType();
397 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
398 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000399 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000400 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000401 return 0;
402
403 Value *Dst = CI->getOperand(1);
404 Value *Src = CI->getOperand(2);
405 Value *LenOp = CI->getOperand(3);
406
407 // See if we can get the length of the input string.
408 uint64_t SrcLen = GetStringLength(Src);
409 if (SrcLen == 0) return 0;
410 --SrcLen;
411
412 if (SrcLen == 0) {
413 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000414 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
Eric Christopherb6174e32010-03-05 22:25:30 +0000415 B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000416 return Dst;
417 }
418
419 uint64_t Len;
420 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
421 Len = LengthArg->getZExtValue();
422 else
423 return 0;
424
425 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
426
Dan Gohmanf14d9192009-08-18 00:48:13 +0000427 // These optimizations require TargetData.
428 if (!TD) return 0;
429
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000430 // Let strncpy handle the zero padding
431 if (Len > SrcLen+1) return 0;
432
433 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000434 EmitMemCpy(Dst, Src,
Eric Christopherb6174e32010-03-05 22:25:30 +0000435 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000436
437 return Dst;
438 }
439};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000440
441//===---------------------------------------===//
442// 'strlen' Optimizations
443
Chris Lattner3e8b6632009-09-02 06:11:42 +0000444struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000445 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000446 const FunctionType *FT = Callee->getFunctionType();
447 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000448 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000449 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000450 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000451
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000452 Value *Src = CI->getOperand(1);
453
454 // Constant folding: strlen("xyz") -> 3
455 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000456 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000457
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000458 // strlen(x) != 0 --> *x != 0
459 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000460 if (IsOnlyUsedInZeroEqualityComparison(CI))
461 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
462 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000463 }
464};
465
466//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000467// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000468
Chris Lattner3e8b6632009-09-02 06:11:42 +0000469struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000470 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
471 const FunctionType *FT = Callee->getFunctionType();
472 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000473 !FT->getParamType(0)->isPointerTy() ||
474 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000475 return 0;
476
477 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000478 if (isa<ConstantPointerNull>(EndPtr)) {
479 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000480 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000481 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000482
483 return 0;
484 }
485};
486
Chris Lattner24604112009-12-16 09:32:05 +0000487//===---------------------------------------===//
488// 'strstr' Optimizations
489
490struct StrStrOpt : public LibCallOptimization {
491 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
492 const FunctionType *FT = Callee->getFunctionType();
493 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000494 !FT->getParamType(0)->isPointerTy() ||
495 !FT->getParamType(1)->isPointerTy() ||
496 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000497 return 0;
498
499 // fold strstr(x, x) -> x.
500 if (CI->getOperand(1) == CI->getOperand(2))
501 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000502
Chris Lattner24604112009-12-16 09:32:05 +0000503 // See if either input string is a constant string.
504 std::string SearchStr, ToFindStr;
505 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
506 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000507
Chris Lattner24604112009-12-16 09:32:05 +0000508 // fold strstr(x, "") -> x.
509 if (HasStr2 && ToFindStr.empty())
510 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000511
Chris Lattner24604112009-12-16 09:32:05 +0000512 // If both strings are known, constant fold it.
513 if (HasStr1 && HasStr2) {
514 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000515
Chris Lattner24604112009-12-16 09:32:05 +0000516 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
517 return Constant::getNullValue(CI->getType());
518
519 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
520 Value *Result = CastToCStr(CI->getOperand(1), B);
521 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
522 return B.CreateBitCast(Result, CI->getType());
523 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000524
Chris Lattner24604112009-12-16 09:32:05 +0000525 // fold strstr(x, "y") -> strchr(x, 'y').
526 if (HasStr2 && ToFindStr.size() == 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000527 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B, TD),
Chris Lattner24604112009-12-16 09:32:05 +0000528 CI->getType());
529 return 0;
530 }
531};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000532
Nick Lewycky4c498412009-02-13 15:31:46 +0000533
534//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000535// 'memcmp' Optimizations
536
Chris Lattner3e8b6632009-09-02 06:11:42 +0000537struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000538 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000539 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000540 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
541 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000542 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000543 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000544
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000545 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000546
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000547 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000548 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000549
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000550 // Make sure we have a constant length.
551 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000552 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000553 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000554
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000555 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000556 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000557
558 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
559 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
560 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +0000561 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000562 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000563
Benjamin Kramer992a6372009-11-05 17:44:22 +0000564 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
565 std::string LHSStr, RHSStr;
566 if (GetConstantStringInfo(LHS, LHSStr) &&
567 GetConstantStringInfo(RHS, RHSStr)) {
568 // Make sure we're not reading out-of-bounds memory.
569 if (Len > LHSStr.length() || Len > RHSStr.length())
570 return 0;
571 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
572 return ConstantInt::get(CI->getType(), Ret);
573 }
574
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000575 return 0;
576 }
577};
578
579//===---------------------------------------===//
580// 'memcpy' Optimizations
581
Chris Lattner3e8b6632009-09-02 06:11:42 +0000582struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000583 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000584 // These optimizations require TargetData.
585 if (!TD) return 0;
586
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000587 const FunctionType *FT = Callee->getFunctionType();
588 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000589 !FT->getParamType(0)->isPointerTy() ||
590 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000591 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000592 return 0;
593
594 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000595 EmitMemCpy(CI->getOperand(1), CI->getOperand(2),
596 CI->getOperand(3), 1, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000597 return CI->getOperand(1);
598 }
599};
600
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000601//===---------------------------------------===//
602// 'memmove' Optimizations
603
Chris Lattner3e8b6632009-09-02 06:11:42 +0000604struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000605 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000606 // These optimizations require TargetData.
607 if (!TD) return 0;
608
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000609 const FunctionType *FT = Callee->getFunctionType();
610 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000611 !FT->getParamType(0)->isPointerTy() ||
612 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000613 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000614 return 0;
615
616 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000617 EmitMemMove(CI->getOperand(1), CI->getOperand(2),
618 CI->getOperand(3), 1, B, TD);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000619 return CI->getOperand(1);
620 }
621};
622
623//===---------------------------------------===//
624// 'memset' Optimizations
625
Chris Lattner3e8b6632009-09-02 06:11:42 +0000626struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000627 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000628 // These optimizations require TargetData.
629 if (!TD) return 0;
630
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000631 const FunctionType *FT = Callee->getFunctionType();
632 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000633 !FT->getParamType(0)->isPointerTy() ||
634 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000635 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000636 return 0;
637
638 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000639 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
640 false);
Eric Christopherb6174e32010-03-05 22:25:30 +0000641 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B, TD);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000642 return CI->getOperand(1);
643 }
644};
645
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000646//===----------------------------------------------------------------------===//
647// Math Library Optimizations
648//===----------------------------------------------------------------------===//
649
650//===---------------------------------------===//
651// 'pow*' Optimizations
652
Chris Lattner3e8b6632009-09-02 06:11:42 +0000653struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000654 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000655 const FunctionType *FT = Callee->getFunctionType();
656 // Just make sure this has 2 arguments of the same FP type, which match the
657 // result type.
658 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
659 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000660 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000661 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000662
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000663 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
664 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
665 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
666 return Op1C;
667 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000668 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000669 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000670
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000671 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
672 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000673
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000674 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000675 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000676
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000677 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000678 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
679 // This is faster than calling pow, and still handles negative zero
680 // and negative infinite correctly.
681 // TODO: In fast-math mode, this could be just sqrt(x).
682 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000683 Value *Inf = ConstantFP::getInfinity(CI->getType());
684 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000685 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
686 Callee->getAttributes());
687 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
688 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000689 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
690 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
691 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000692 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000693
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000694 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
695 return Op1;
696 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000697 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000698 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000699 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000700 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000701 return 0;
702 }
703};
704
705//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000706// 'exp2' Optimizations
707
Chris Lattner3e8b6632009-09-02 06:11:42 +0000708struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000709 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000710 const FunctionType *FT = Callee->getFunctionType();
711 // Just make sure this has 1 argument of FP type, which matches the
712 // result type.
713 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000714 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000715 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000716
Chris Lattnere818f772008-05-02 18:43:35 +0000717 Value *Op = CI->getOperand(1);
718 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
719 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
720 Value *LdExpArg = 0;
721 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
722 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000723 LdExpArg = B.CreateSExt(OpC->getOperand(0),
724 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000725 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
726 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000727 LdExpArg = B.CreateZExt(OpC->getOperand(0),
728 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000729 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000730
Chris Lattnere818f772008-05-02 18:43:35 +0000731 if (LdExpArg) {
732 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000733 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000734 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000735 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000736 Name = "ldexp";
737 else
738 Name = "ldexpl";
739
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000740 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000741 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000742 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000743
744 Module *M = Caller->getParent();
745 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000746 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000747 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000748 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
749 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
750 CI->setCallingConv(F->getCallingConv());
751
752 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000753 }
754 return 0;
755 }
756};
Chris Lattnere818f772008-05-02 18:43:35 +0000757
758//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000759// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
760
Chris Lattner3e8b6632009-09-02 06:11:42 +0000761struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000762 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000763 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000764 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
765 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000766 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000767
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000768 // If this is something like 'floor((double)floatval)', convert to floorf.
769 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000770 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000771 return 0;
772
773 // floor((double)floatval) -> (double)floorf(floatval)
774 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000775 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
776 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000777 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000778 }
779};
780
781//===----------------------------------------------------------------------===//
782// Integer Optimizations
783//===----------------------------------------------------------------------===//
784
785//===---------------------------------------===//
786// 'ffs*' Optimizations
787
Chris Lattner3e8b6632009-09-02 06:11:42 +0000788struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000789 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000790 const FunctionType *FT = Callee->getFunctionType();
791 // Just make sure this has 2 arguments of the same FP type, which match the
792 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000793 if (FT->getNumParams() != 1 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000794 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000795 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000796 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000797
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000798 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000799
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000800 // Constant fold.
801 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
802 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000803 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000804 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000805 CI->getValue().countTrailingZeros()+1);
806 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000807
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000808 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
809 const Type *ArgType = Op->getType();
810 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
811 Intrinsic::cttz, &ArgType, 1);
812 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000813 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000814 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000815
Owen Andersona7235ea2009-07-31 20:28:14 +0000816 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000817 return B.CreateSelect(Cond, V,
818 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000819 }
820};
821
822//===---------------------------------------===//
823// 'isdigit' Optimizations
824
Chris Lattner3e8b6632009-09-02 06:11:42 +0000825struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000826 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000827 const FunctionType *FT = Callee->getFunctionType();
828 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000829 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000830 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000831 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000832
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000833 // isdigit(c) -> (c-'0') <u 10
834 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000835 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000836 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000837 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000838 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000839 return B.CreateZExt(Op, CI->getType());
840 }
841};
842
843//===---------------------------------------===//
844// 'isascii' Optimizations
845
Chris Lattner3e8b6632009-09-02 06:11:42 +0000846struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000847 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000848 const FunctionType *FT = Callee->getFunctionType();
849 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000850 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000851 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000852 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000853
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000854 // isascii(c) -> c <u 128
855 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000856 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000857 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000858 return B.CreateZExt(Op, CI->getType());
859 }
860};
Eric Christopher37c8b862009-10-07 21:14:25 +0000861
Chris Lattner313f0e62008-06-09 08:26:51 +0000862//===---------------------------------------===//
863// 'abs', 'labs', 'llabs' Optimizations
864
Chris Lattner3e8b6632009-09-02 06:11:42 +0000865struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000866 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +0000867 const FunctionType *FT = Callee->getFunctionType();
868 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +0000869 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +0000870 FT->getParamType(0) != FT->getReturnType())
871 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000872
Chris Lattner313f0e62008-06-09 08:26:51 +0000873 // abs(x) -> x >s -1 ? x : -x
874 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000875 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +0000876 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +0000877 "ispos");
878 Value *Neg = B.CreateNeg(Op, "neg");
879 return B.CreateSelect(Pos, Op, Neg);
880 }
881};
Eric Christopher37c8b862009-10-07 21:14:25 +0000882
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000883
884//===---------------------------------------===//
885// 'toascii' Optimizations
886
Chris Lattner3e8b6632009-09-02 06:11:42 +0000887struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000888 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000889 const FunctionType *FT = Callee->getFunctionType();
890 // We require i32(i32)
891 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000892 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000893 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000894
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000895 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000896 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +0000897 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000898 }
899};
900
901//===----------------------------------------------------------------------===//
902// Formatting and IO Optimizations
903//===----------------------------------------------------------------------===//
904
905//===---------------------------------------===//
906// 'printf' Optimizations
907
Chris Lattner3e8b6632009-09-02 06:11:42 +0000908struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000909 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000910 // Require one fixed pointer argument and an integer/void result.
911 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000912 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
913 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000914 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000915 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000916
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000917 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000918 std::string FormatStr;
919 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
920 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000921
922 // Empty format string -> noop.
923 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +0000924 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000925 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000926
Chris Lattner74965f22009-11-09 04:57:04 +0000927 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
928 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000929 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +0000930 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000931 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +0000932 if (CI->use_empty()) return CI;
933 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000934 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000935
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000936 // printf("foo\n") --> puts("foo")
937 if (FormatStr[FormatStr.size()-1] == '\n' &&
938 FormatStr.find('%') == std::string::npos) { // no format characters.
939 // Create a string literal with no \n on it. We expect the constant merge
940 // pass to be run after this pass, to merge duplicate strings.
941 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000942 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +0000943 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
944 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +0000945 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000946 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000947 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000948 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000949
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000950 // Optimize specific format strings.
951 // printf("%c", chr) --> putchar(*(i8*)dst)
952 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
Duncan Sands1df98592010-02-16 11:11:14 +0000953 CI->getOperand(2)->getType()->isIntegerTy()) {
Eric Christopherb6174e32010-03-05 22:25:30 +0000954 Value *Res = EmitPutChar(CI->getOperand(2), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000955
Chris Lattner74965f22009-11-09 04:57:04 +0000956 if (CI->use_empty()) return CI;
957 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000958 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000959
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000960 // printf("%s\n", str) --> puts(str)
961 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
Duncan Sands1df98592010-02-16 11:11:14 +0000962 CI->getOperand(2)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000963 CI->use_empty()) {
Eric Christopherb6174e32010-03-05 22:25:30 +0000964 EmitPutS(CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000965 return CI;
966 }
967 return 0;
968 }
969};
970
971//===---------------------------------------===//
972// 'sprintf' Optimizations
973
Chris Lattner3e8b6632009-09-02 06:11:42 +0000974struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000975 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000976 // Require two fixed pointer arguments and an integer result.
977 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000978 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
979 !FT->getParamType(1)->isPointerTy() ||
980 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000981 return 0;
982
983 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000984 std::string FormatStr;
985 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
986 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000987
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000988 // If we just have a format string (nothing else crazy) transform it.
989 if (CI->getNumOperands() == 3) {
990 // Make sure there's no % in the constant array. We could try to handle
991 // %% -> % in the future if we cared.
992 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
993 if (FormatStr[i] == '%')
994 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +0000995
996 // These optimizations require TargetData.
997 if (!TD) return 0;
998
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000999 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1000 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Eric Christopherb6174e32010-03-05 22:25:30 +00001001 ConstantInt::get(TD->getIntPtrType(*Context),
1002 FormatStr.size()+1), 1, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001003 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001004 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001005
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001006 // The remaining optimizations require the format string to be "%s" or "%c"
1007 // and have an extra operand.
1008 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1009 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001010
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001011 // Decode the second character of the format string.
1012 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001013 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Duncan Sands1df98592010-02-16 11:11:14 +00001014 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001015 Value *V = B.CreateTrunc(CI->getOperand(3),
1016 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001017 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1018 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001019 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1020 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001021 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001022
Owen Andersoneed707b2009-07-24 23:12:02 +00001023 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001024 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001025
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001026 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001027 // These optimizations require TargetData.
1028 if (!TD) return 0;
1029
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001030 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Duncan Sands1df98592010-02-16 11:11:14 +00001031 if (!CI->getOperand(3)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001032
Eric Christopherb6174e32010-03-05 22:25:30 +00001033 Value *Len = EmitStrLen(CI->getOperand(3), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001034 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001035 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001036 "leninc");
Eric Christopherb6174e32010-03-05 22:25:30 +00001037 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001038
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001039 // The sprintf result is the unincremented number of bytes in the string.
1040 return B.CreateIntCast(Len, CI->getType(), false);
1041 }
1042 return 0;
1043 }
1044};
1045
1046//===---------------------------------------===//
1047// 'fwrite' Optimizations
1048
Chris Lattner3e8b6632009-09-02 06:11:42 +00001049struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001050 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001051 // Require a pointer, an integer, an integer, a pointer, returning integer.
1052 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001053 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1054 !FT->getParamType(1)->isIntegerTy() ||
1055 !FT->getParamType(2)->isIntegerTy() ||
1056 !FT->getParamType(3)->isPointerTy() ||
1057 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001058 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001059
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001060 // Get the element size and count.
1061 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1062 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1063 if (!SizeC || !CountC) return 0;
1064 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001065
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001066 // If this is writing zero records, remove the call (it's a noop).
1067 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001068 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001069
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001070 // If this is writing one byte, turn it into fputc.
1071 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1072 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
Eric Christopherb6174e32010-03-05 22:25:30 +00001073 EmitFPutC(Char, CI->getOperand(4), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001074 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001075 }
1076
1077 return 0;
1078 }
1079};
1080
1081//===---------------------------------------===//
1082// 'fputs' Optimizations
1083
Chris Lattner3e8b6632009-09-02 06:11:42 +00001084struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001085 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001086 // These optimizations require TargetData.
1087 if (!TD) return 0;
1088
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001089 // Require two pointers. Also, we can't optimize if return value is used.
1090 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001091 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1092 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001093 !CI->use_empty())
1094 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001095
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001096 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1097 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001098 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001099 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001100 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Eric Christopherb6174e32010-03-05 22:25:30 +00001101 CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001102 return CI; // Known to have no uses (see above).
1103 }
1104};
1105
1106//===---------------------------------------===//
1107// 'fprintf' Optimizations
1108
Chris Lattner3e8b6632009-09-02 06:11:42 +00001109struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001110 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001111 // Require two fixed paramters as pointers and integer result.
1112 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001113 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1114 !FT->getParamType(1)->isPointerTy() ||
1115 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001116 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001117
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001118 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001119 std::string FormatStr;
1120 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1121 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001122
1123 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1124 if (CI->getNumOperands() == 3) {
1125 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1126 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001127 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001128
1129 // These optimizations require TargetData.
1130 if (!TD) return 0;
1131
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001132 EmitFWrite(CI->getOperand(2),
1133 ConstantInt::get(TD->getIntPtrType(*Context),
1134 FormatStr.size()),
Eric Christopherb6174e32010-03-05 22:25:30 +00001135 CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001136 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001137 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001138
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001139 // The remaining optimizations require the format string to be "%s" or "%c"
1140 // and have an extra operand.
1141 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1142 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001143
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001144 // Decode the second character of the format string.
1145 if (FormatStr[1] == 'c') {
1146 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
Duncan Sands1df98592010-02-16 11:11:14 +00001147 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
Eric Christopherb6174e32010-03-05 22:25:30 +00001148 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001149 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001150 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001151
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001152 if (FormatStr[1] == 's') {
1153 // fprintf(F, "%s", str) -> fputs(str, F)
Duncan Sands1df98592010-02-16 11:11:14 +00001154 if (!CI->getOperand(3)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001155 return 0;
Eric Christopherb6174e32010-03-05 22:25:30 +00001156 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001157 return CI;
1158 }
1159 return 0;
1160 }
1161};
1162
Bill Wendlingac178222008-05-05 21:37:59 +00001163} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001164
1165//===----------------------------------------------------------------------===//
1166// SimplifyLibCalls Pass Implementation
1167//===----------------------------------------------------------------------===//
1168
1169namespace {
1170 /// This pass optimizes well known library functions from libc and libm.
1171 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001172 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001173 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001174 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001175 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
Evan Cheng0289b412010-03-23 15:48:04 +00001176 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
1177 StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001178 StrToOpt StrTo; StrStrOpt StrStr;
1179 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001180 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001181 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001182 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001183 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1184 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001185 // Formatting and IO Optimizations
1186 SPrintFOpt SPrintF; PrintFOpt PrintF;
1187 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001188
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001189 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001190 public:
1191 static char ID; // Pass identification
Evan Chengeb8c6452010-03-24 20:19:04 +00001192 SimplifyLibCalls() : FunctionPass(&ID), StrCpy(false), StrCpyChk(true) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001193 void InitOptimizations();
1194 bool runOnFunction(Function &F);
1195
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001196 void setDoesNotAccessMemory(Function &F);
1197 void setOnlyReadsMemory(Function &F);
1198 void setDoesNotThrow(Function &F);
1199 void setDoesNotCapture(Function &F, unsigned n);
1200 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001201 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001202
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001203 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001204 }
1205 };
1206 char SimplifyLibCalls::ID = 0;
1207} // end anonymous namespace.
1208
1209static RegisterPass<SimplifyLibCalls>
1210X("simplify-libcalls", "Simplify well-known library calls");
1211
1212// Public interface to the Simplify LibCalls pass.
1213FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001214 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001215}
1216
1217/// Optimizations - Populate the Optimizations map with all the optimizations
1218/// we know.
1219void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001220 // String and Memory LibCall Optimizations
1221 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001222 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001223 Optimizations["strchr"] = &StrChr;
1224 Optimizations["strcmp"] = &StrCmp;
1225 Optimizations["strncmp"] = &StrNCmp;
1226 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001227 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001228 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001229 Optimizations["strtol"] = &StrTo;
1230 Optimizations["strtod"] = &StrTo;
1231 Optimizations["strtof"] = &StrTo;
1232 Optimizations["strtoul"] = &StrTo;
1233 Optimizations["strtoll"] = &StrTo;
1234 Optimizations["strtold"] = &StrTo;
1235 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001236 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001237 Optimizations["memcmp"] = &MemCmp;
1238 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001239 Optimizations["memmove"] = &MemMove;
1240 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001241
Evan Cheng0289b412010-03-23 15:48:04 +00001242 // _chk variants of String and Memory LibCall Optimizations.
Evan Cheng0289b412010-03-23 15:48:04 +00001243 Optimizations["__strcpy_chk"] = &StrCpyChk;
1244
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001245 // Math Library Optimizations
1246 Optimizations["powf"] = &Pow;
1247 Optimizations["pow"] = &Pow;
1248 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001249 Optimizations["llvm.pow.f32"] = &Pow;
1250 Optimizations["llvm.pow.f64"] = &Pow;
1251 Optimizations["llvm.pow.f80"] = &Pow;
1252 Optimizations["llvm.pow.f128"] = &Pow;
1253 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001254 Optimizations["exp2l"] = &Exp2;
1255 Optimizations["exp2"] = &Exp2;
1256 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001257 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1258 Optimizations["llvm.exp2.f128"] = &Exp2;
1259 Optimizations["llvm.exp2.f80"] = &Exp2;
1260 Optimizations["llvm.exp2.f64"] = &Exp2;
1261 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001262
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001263#ifdef HAVE_FLOORF
1264 Optimizations["floor"] = &UnaryDoubleFP;
1265#endif
1266#ifdef HAVE_CEILF
1267 Optimizations["ceil"] = &UnaryDoubleFP;
1268#endif
1269#ifdef HAVE_ROUNDF
1270 Optimizations["round"] = &UnaryDoubleFP;
1271#endif
1272#ifdef HAVE_RINTF
1273 Optimizations["rint"] = &UnaryDoubleFP;
1274#endif
1275#ifdef HAVE_NEARBYINTF
1276 Optimizations["nearbyint"] = &UnaryDoubleFP;
1277#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001278
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001279 // Integer Optimizations
1280 Optimizations["ffs"] = &FFS;
1281 Optimizations["ffsl"] = &FFS;
1282 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001283 Optimizations["abs"] = &Abs;
1284 Optimizations["labs"] = &Abs;
1285 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001286 Optimizations["isdigit"] = &IsDigit;
1287 Optimizations["isascii"] = &IsAscii;
1288 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001289
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001290 // Formatting and IO Optimizations
1291 Optimizations["sprintf"] = &SPrintF;
1292 Optimizations["printf"] = &PrintF;
1293 Optimizations["fwrite"] = &FWrite;
1294 Optimizations["fputs"] = &FPuts;
1295 Optimizations["fprintf"] = &FPrintF;
1296}
1297
1298
1299/// runOnFunction - Top level algorithm.
1300///
1301bool SimplifyLibCalls::runOnFunction(Function &F) {
1302 if (Optimizations.empty())
1303 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001304
Dan Gohmanf14d9192009-08-18 00:48:13 +00001305 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001306
Owen Andersone922c022009-07-22 00:24:57 +00001307 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001308
1309 bool Changed = false;
1310 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1311 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1312 // Ignore non-calls.
1313 CallInst *CI = dyn_cast<CallInst>(I++);
1314 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001315
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001316 // Ignore indirect calls and calls to non-external functions.
1317 Function *Callee = CI->getCalledFunction();
1318 if (Callee == 0 || !Callee->isDeclaration() ||
1319 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1320 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001321
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001322 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001323 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1324 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001325
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001326 // Set the builder to the instruction after the call.
1327 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001328
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001329 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001330 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001331 if (Result == 0) continue;
1332
David Greene6a6b90e2010-01-05 01:27:21 +00001333 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1334 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001335
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001336 // Something changed!
1337 Changed = true;
1338 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001339
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001340 // Inspect the instruction after the call (which was potentially just
1341 // added) next.
1342 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001343
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001344 if (CI != Result && !CI->use_empty()) {
1345 CI->replaceAllUsesWith(Result);
1346 if (!Result->hasName())
1347 Result->takeName(CI);
1348 }
1349 CI->eraseFromParent();
1350 }
1351 }
1352 return Changed;
1353}
1354
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001355// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001356
1357void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1358 if (!F.doesNotAccessMemory()) {
1359 F.setDoesNotAccessMemory();
1360 ++NumAnnotated;
1361 Modified = true;
1362 }
1363}
1364void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1365 if (!F.onlyReadsMemory()) {
1366 F.setOnlyReadsMemory();
1367 ++NumAnnotated;
1368 Modified = true;
1369 }
1370}
1371void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1372 if (!F.doesNotThrow()) {
1373 F.setDoesNotThrow();
1374 ++NumAnnotated;
1375 Modified = true;
1376 }
1377}
1378void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1379 if (!F.doesNotCapture(n)) {
1380 F.setDoesNotCapture(n);
1381 ++NumAnnotated;
1382 Modified = true;
1383 }
1384}
1385void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1386 if (!F.doesNotAlias(n)) {
1387 F.setDoesNotAlias(n);
1388 ++NumAnnotated;
1389 Modified = true;
1390 }
1391}
1392
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001393/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001394///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001395bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001396 Modified = false;
1397 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1398 Function &F = *I;
1399 if (!F.isDeclaration())
1400 continue;
1401
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001402 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001403 continue;
1404
1405 const FunctionType *FTy = F.getFunctionType();
1406
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001407 StringRef Name = F.getName();
1408 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001409 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001410 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001411 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001412 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001413 continue;
1414 setOnlyReadsMemory(F);
1415 setDoesNotThrow(F);
1416 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001417 } else if (Name == "strchr" ||
1418 Name == "strrchr") {
1419 if (FTy->getNumParams() != 2 ||
1420 !FTy->getParamType(0)->isPointerTy() ||
1421 !FTy->getParamType(1)->isIntegerTy())
1422 continue;
1423 setOnlyReadsMemory(F);
1424 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001425 } else if (Name == "strcpy" ||
1426 Name == "stpcpy" ||
1427 Name == "strcat" ||
1428 Name == "strtol" ||
1429 Name == "strtod" ||
1430 Name == "strtof" ||
1431 Name == "strtoul" ||
1432 Name == "strtoll" ||
1433 Name == "strtold" ||
1434 Name == "strncat" ||
1435 Name == "strncpy" ||
1436 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001437 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001438 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001439 continue;
1440 setDoesNotThrow(F);
1441 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001442 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001443 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001444 !FTy->getParamType(0)->isPointerTy() ||
1445 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001446 continue;
1447 setDoesNotThrow(F);
1448 setDoesNotCapture(F, 1);
1449 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001450 } else if (Name == "strcmp" ||
1451 Name == "strspn" ||
1452 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001453 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001454 Name == "strcoll" ||
1455 Name == "strcasecmp" ||
1456 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001457 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001458 !FTy->getParamType(0)->isPointerTy() ||
1459 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001460 continue;
1461 setOnlyReadsMemory(F);
1462 setDoesNotThrow(F);
1463 setDoesNotCapture(F, 1);
1464 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001465 } else if (Name == "strstr" ||
1466 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001467 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001468 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001469 continue;
1470 setOnlyReadsMemory(F);
1471 setDoesNotThrow(F);
1472 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001473 } else if (Name == "strtok" ||
1474 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001475 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001476 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001477 continue;
1478 setDoesNotThrow(F);
1479 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001480 } else if (Name == "scanf" ||
1481 Name == "setbuf" ||
1482 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001483 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001484 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001485 continue;
1486 setDoesNotThrow(F);
1487 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001488 } else if (Name == "strdup" ||
1489 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001490 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001491 !FTy->getReturnType()->isPointerTy() ||
1492 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001493 continue;
1494 setDoesNotThrow(F);
1495 setDoesNotAlias(F, 0);
1496 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001497 } else if (Name == "stat" ||
1498 Name == "sscanf" ||
1499 Name == "sprintf" ||
1500 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001501 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001502 !FTy->getParamType(0)->isPointerTy() ||
1503 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001504 continue;
1505 setDoesNotThrow(F);
1506 setDoesNotCapture(F, 1);
1507 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001508 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001509 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001510 !FTy->getParamType(0)->isPointerTy() ||
1511 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001512 continue;
1513 setDoesNotThrow(F);
1514 setDoesNotCapture(F, 1);
1515 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001516 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001517 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001518 !FTy->getParamType(1)->isPointerTy() ||
1519 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001520 continue;
1521 setDoesNotThrow(F);
1522 setDoesNotCapture(F, 2);
1523 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001524 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001525 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001526 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001527 continue;
1528 // May throw; "system" is a valid pthread cancellation point.
1529 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001530 }
1531 break;
1532 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001533 if (Name == "malloc") {
1534 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001535 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001536 continue;
1537 setDoesNotThrow(F);
1538 setDoesNotAlias(F, 0);
1539 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001540 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001541 !FTy->getParamType(0)->isPointerTy() ||
1542 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001543 continue;
1544 setOnlyReadsMemory(F);
1545 setDoesNotThrow(F);
1546 setDoesNotCapture(F, 1);
1547 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001548 } else if (Name == "memchr" ||
1549 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001550 if (FTy->getNumParams() != 3)
1551 continue;
1552 setOnlyReadsMemory(F);
1553 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001554 } else if (Name == "modf" ||
1555 Name == "modff" ||
1556 Name == "modfl" ||
1557 Name == "memcpy" ||
1558 Name == "memccpy" ||
1559 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001560 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001561 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001562 continue;
1563 setDoesNotThrow(F);
1564 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001565 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001566 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001567 continue;
1568 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001569 } else if (Name == "mkdir" ||
1570 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001571 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001572 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001573 continue;
1574 setDoesNotThrow(F);
1575 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001576 }
1577 break;
1578 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001579 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001580 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001581 !FTy->getParamType(0)->isPointerTy() ||
1582 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001583 continue;
1584 setDoesNotThrow(F);
1585 setDoesNotAlias(F, 0);
1586 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001587 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001588 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001589 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001590 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001591 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001592 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001593 } else if (Name == "rmdir" ||
1594 Name == "rewind" ||
1595 Name == "remove" ||
1596 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001597 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001598 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001599 continue;
1600 setDoesNotThrow(F);
1601 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001602 } else if (Name == "rename" ||
1603 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001604 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001605 !FTy->getParamType(0)->isPointerTy() ||
1606 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001607 continue;
1608 setDoesNotThrow(F);
1609 setDoesNotCapture(F, 1);
1610 setDoesNotCapture(F, 2);
1611 }
1612 break;
1613 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001614 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001615 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001616 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001617 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001618 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001619 setDoesNotCapture(F, 2);
1620 }
1621 break;
1622 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001623 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001624 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001625 !FTy->getParamType(0)->isPointerTy() ||
1626 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001627 continue;
1628 setDoesNotThrow(F);
1629 setDoesNotCapture(F, 1);
1630 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001631 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001632 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001633 !FTy->getParamType(0)->isPointerTy() ||
1634 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001635 continue;
1636 setDoesNotThrow(F);
1637 setOnlyReadsMemory(F);
1638 setDoesNotCapture(F, 1);
1639 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001640 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001641 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001642 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001643 continue;
1644 setDoesNotThrow(F);
1645 setDoesNotCapture(F, 1);
1646 }
1647 break;
1648 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001649 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001650 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001651 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001652 continue;
1653 setDoesNotThrow(F);
1654 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001655 } else if (Name == "chmod" ||
1656 Name == "chown" ||
1657 Name == "ctermid" ||
1658 Name == "clearerr" ||
1659 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001660 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001661 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001662 continue;
1663 setDoesNotThrow(F);
1664 setDoesNotCapture(F, 1);
1665 }
1666 break;
1667 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001668 if (Name == "atoi" ||
1669 Name == "atol" ||
1670 Name == "atof" ||
1671 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001672 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001673 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001674 continue;
1675 setDoesNotThrow(F);
1676 setOnlyReadsMemory(F);
1677 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001678 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001679 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001680 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001681 continue;
1682 setDoesNotThrow(F);
1683 setDoesNotCapture(F, 1);
1684 }
1685 break;
1686 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001687 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001688 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001689 !FTy->getReturnType()->isPointerTy() ||
1690 !FTy->getParamType(0)->isPointerTy() ||
1691 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001692 continue;
1693 setDoesNotThrow(F);
1694 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001695 setDoesNotCapture(F, 1);
1696 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001697 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001698 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001699 !FTy->getReturnType()->isPointerTy() ||
1700 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001701 continue;
1702 setDoesNotThrow(F);
1703 setDoesNotAlias(F, 0);
1704 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001705 } else if (Name == "feof" ||
1706 Name == "free" ||
1707 Name == "fseek" ||
1708 Name == "ftell" ||
1709 Name == "fgetc" ||
1710 Name == "fseeko" ||
1711 Name == "ftello" ||
1712 Name == "fileno" ||
1713 Name == "fflush" ||
1714 Name == "fclose" ||
1715 Name == "fsetpos" ||
1716 Name == "flockfile" ||
1717 Name == "funlockfile" ||
1718 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001719 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001720 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001721 continue;
1722 setDoesNotThrow(F);
1723 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001724 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001725 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001726 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001727 continue;
1728 setDoesNotThrow(F);
1729 setDoesNotCapture(F, 1);
1730 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001731 } else if (Name == "fputc" ||
1732 Name == "fstat" ||
1733 Name == "frexp" ||
1734 Name == "frexpf" ||
1735 Name == "frexpl" ||
1736 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001737 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001738 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001739 continue;
1740 setDoesNotThrow(F);
1741 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001742 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001743 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001744 !FTy->getParamType(0)->isPointerTy() ||
1745 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001746 continue;
1747 setDoesNotThrow(F);
1748 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001749 } else if (Name == "fread" ||
1750 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001751 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001752 !FTy->getParamType(0)->isPointerTy() ||
1753 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001754 continue;
1755 setDoesNotThrow(F);
1756 setDoesNotCapture(F, 1);
1757 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001758 } else if (Name == "fputs" ||
1759 Name == "fscanf" ||
1760 Name == "fprintf" ||
1761 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001762 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001763 !FTy->getParamType(0)->isPointerTy() ||
1764 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001765 continue;
1766 setDoesNotThrow(F);
1767 setDoesNotCapture(F, 1);
1768 setDoesNotCapture(F, 2);
1769 }
1770 break;
1771 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001772 if (Name == "getc" ||
1773 Name == "getlogin_r" ||
1774 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001775 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001776 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001777 continue;
1778 setDoesNotThrow(F);
1779 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001780 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001781 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001782 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001783 continue;
1784 setDoesNotThrow(F);
1785 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001786 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001787 } else if (Name == "gets" ||
1788 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001789 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001790 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001791 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001792 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001793 continue;
1794 setDoesNotThrow(F);
1795 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001796 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001797 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001798 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001799 continue;
1800 setDoesNotThrow(F);
1801 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001802 }
1803 break;
1804 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001805 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001806 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001807 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001808 continue;
1809 setDoesNotThrow(F);
1810 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001811 } else if (Name == "uname" ||
1812 Name == "unlink" ||
1813 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001814 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001815 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001816 continue;
1817 setDoesNotThrow(F);
1818 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001819 } else if (Name == "utime" ||
1820 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001821 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001822 !FTy->getParamType(0)->isPointerTy() ||
1823 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001824 continue;
1825 setDoesNotThrow(F);
1826 setDoesNotCapture(F, 1);
1827 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001828 }
1829 break;
1830 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001831 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001832 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001833 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001834 continue;
1835 setDoesNotThrow(F);
1836 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001837 } else if (Name == "puts" ||
1838 Name == "printf" ||
1839 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001840 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001841 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001842 continue;
1843 setDoesNotThrow(F);
1844 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001845 } else if (Name == "pread" ||
1846 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001847 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001848 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001849 continue;
1850 // May throw; these are valid pthread cancellation points.
1851 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001852 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001853 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001854 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001855 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001856 !FTy->getReturnType()->isPointerTy() ||
1857 !FTy->getParamType(0)->isPointerTy() ||
1858 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001859 continue;
1860 setDoesNotThrow(F);
1861 setDoesNotAlias(F, 0);
1862 setDoesNotCapture(F, 1);
1863 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001864 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001865 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001866 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001867 continue;
1868 setDoesNotThrow(F);
1869 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001870 }
1871 break;
1872 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001873 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001874 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001875 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001876 continue;
1877 setDoesNotThrow(F);
1878 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001879 } else if (Name == "vsscanf" ||
1880 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001881 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001882 !FTy->getParamType(1)->isPointerTy() ||
1883 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001884 continue;
1885 setDoesNotThrow(F);
1886 setDoesNotCapture(F, 1);
1887 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001888 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00001889 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001890 continue;
1891 setDoesNotThrow(F);
1892 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001893 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001894 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001895 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001896 continue;
1897 setDoesNotThrow(F);
1898 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001899 } else if (Name == "vfprintf" ||
1900 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001901 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001902 !FTy->getParamType(0)->isPointerTy() ||
1903 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001904 continue;
1905 setDoesNotThrow(F);
1906 setDoesNotCapture(F, 1);
1907 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001908 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001909 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001910 !FTy->getParamType(0)->isPointerTy() ||
1911 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001912 continue;
1913 setDoesNotThrow(F);
1914 setDoesNotCapture(F, 1);
1915 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001916 }
1917 break;
1918 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001919 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001920 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001921 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001922 continue;
1923 // May throw; "open" is a valid pthread cancellation point.
1924 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001925 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001926 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001927 !FTy->getReturnType()->isPointerTy() ||
1928 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001929 continue;
1930 setDoesNotThrow(F);
1931 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00001932 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001933 }
1934 break;
1935 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001936 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00001937 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001938 continue;
1939 setDoesNotThrow(F);
1940 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001941 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001942 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001943 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001944 continue;
1945 setDoesNotThrow(F);
1946 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001947 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001948 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001949 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001950 if (Name == "htonl" ||
1951 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001952 setDoesNotThrow(F);
1953 setDoesNotAccessMemory(F);
1954 }
1955 break;
1956 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001957 if (Name == "ntohl" ||
1958 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001959 setDoesNotThrow(F);
1960 setDoesNotAccessMemory(F);
1961 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001962 break;
1963 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001964 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001965 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001966 !FTy->getParamType(0)->isPointerTy() ||
1967 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001968 continue;
1969 setDoesNotThrow(F);
1970 setDoesNotCapture(F, 1);
1971 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001972 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001973 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001974 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001975 continue;
1976 setDoesNotThrow(F);
1977 setDoesNotCapture(F, 1);
1978 }
1979 break;
1980 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001981 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001982 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001983 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001984 continue;
1985 // May throw; places call through function pointer.
1986 setDoesNotCapture(F, 4);
1987 }
1988 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001989 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001990 if (Name == "__strdup" ||
1991 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001992 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001993 !FTy->getReturnType()->isPointerTy() ||
1994 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001995 continue;
1996 setDoesNotThrow(F);
1997 setDoesNotAlias(F, 0);
1998 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001999 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002000 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002001 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002002 continue;
2003 setDoesNotThrow(F);
2004 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002005 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002006 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002007 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002008 continue;
2009 setDoesNotThrow(F);
2010 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002011 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002012 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002013 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002014 continue;
2015 setDoesNotThrow(F);
2016 setDoesNotCapture(F, 2);
2017 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002018 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002019 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002020 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002021 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002022 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002023 continue;
2024 setDoesNotThrow(F);
2025 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002026 } else if (Name == "\1stat64" ||
2027 Name == "\1lstat64" ||
2028 Name == "\1statvfs64" ||
2029 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002030 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002031 !FTy->getParamType(0)->isPointerTy() ||
2032 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002033 continue;
2034 setDoesNotThrow(F);
2035 setDoesNotCapture(F, 1);
2036 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002037 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002038 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002039 !FTy->getReturnType()->isPointerTy() ||
2040 !FTy->getParamType(0)->isPointerTy() ||
2041 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002042 continue;
2043 setDoesNotThrow(F);
2044 setDoesNotAlias(F, 0);
2045 setDoesNotCapture(F, 1);
2046 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002047 } else if (Name == "\1fseeko64" ||
2048 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002049 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002050 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002051 continue;
2052 setDoesNotThrow(F);
2053 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002054 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002055 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002056 continue;
2057 setDoesNotThrow(F);
2058 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002059 } else if (Name == "\1fstat64" ||
2060 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002061 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002062 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002063 continue;
2064 setDoesNotThrow(F);
2065 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002066 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002067 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002068 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002069 continue;
2070 // May throw; "open" is a valid pthread cancellation point.
2071 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002072 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002073 break;
2074 }
2075 }
2076 return Modified;
2077}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002078
2079// TODO:
2080// Additional cases that we need to add to this file:
2081//
2082// cbrt:
2083// * cbrt(expN(X)) -> expN(x/3)
2084// * cbrt(sqrt(x)) -> pow(x,1/6)
2085// * cbrt(sqrt(x)) -> pow(x,1/9)
2086//
2087// cos, cosf, cosl:
2088// * cos(-x) -> cos(x)
2089//
2090// exp, expf, expl:
2091// * exp(log(x)) -> x
2092//
2093// log, logf, logl:
2094// * log(exp(x)) -> x
2095// * log(x**y) -> y*log(x)
2096// * log(exp(y)) -> y*log(e)
2097// * log(exp2(y)) -> y*log(2)
2098// * log(exp10(y)) -> y*log(10)
2099// * log(sqrt(x)) -> 0.5*log(x)
2100// * log(pow(x,y)) -> y*log(x)
2101//
2102// lround, lroundf, lroundl:
2103// * lround(cnst) -> cnst'
2104//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002105// pow, powf, powl:
2106// * pow(exp(x),y) -> exp(x*y)
2107// * pow(sqrt(x),y) -> pow(x,y*0.5)
2108// * pow(pow(x,y),z)-> pow(x,y*z)
2109//
2110// puts:
2111// * puts("") -> putchar("\n")
2112//
2113// round, roundf, roundl:
2114// * round(cnst) -> cnst'
2115//
2116// signbit:
2117// * signbit(cnst) -> cnst'
2118// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2119//
2120// sqrt, sqrtf, sqrtl:
2121// * sqrt(expN(x)) -> expN(x*0.5)
2122// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2123// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2124//
2125// stpcpy:
2126// * stpcpy(str, "literal") ->
2127// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2128// strrchr:
2129// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2130// (if c is a constant integer and s is a constant string)
2131// * strrchr(s1,0) -> strchr(s1,0)
2132//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002133// strpbrk:
2134// * strpbrk(s,a) -> offset_in_for(s,a)
2135// (if s and a are both constant strings)
2136// * strpbrk(s,"") -> 0
2137// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2138//
2139// strspn, strcspn:
2140// * strspn(s,a) -> const_int (if both args are constant)
2141// * strspn("",a) -> 0
2142// * strspn(s,"") -> 0
2143// * strcspn(s,a) -> const_int (if both args are constant)
2144// * strcspn("",a) -> 0
2145// * strcspn(s,"") -> strlen(a)
2146//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002147// tan, tanf, tanl:
2148// * tan(atan(x)) -> x
2149//
2150// trunc, truncf, truncl:
2151// * trunc(cnst) -> cnst'
2152//
2153//