blob: 0f3ab7d8b536da3b3b58a82025e4a6db6f784643 [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
Eric Christopher551754c2010-04-16 23:37:20 +0000113 Value *Dst = CI->getOperand(1);
114 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000115
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000116 // See if we can get the length of the input string.
117 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000118 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000119 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000120
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000121 // Handle the simple, do-nothing case: strcat(x, "") -> x
122 if (Len == 0)
123 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000124
125 // These optimizations require TargetData.
126 if (!TD) return 0;
127
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000128 EmitStrLenMemCpy(Src, Dst, Len, B);
129 return Dst;
130 }
131
132 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000133 // We need to find the end of the destination string. That's where the
134 // memory is to be moved to. We just generate a call to strlen.
Eric Christopherb6174e32010-03-05 22:25:30 +0000135 Value *DstLen = EmitStrLen(Dst, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000136
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000137 // Now that we have the destination's length, we must index into the
138 // destination's pointer to get the actual memcpy destination (end of
139 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000140 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000141
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000142 // We have enough information to now generate the memcpy call to do the
143 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000144 EmitMemCpy(CpyDst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000145 ConstantInt::get(TD->getIntPtrType(*Context), Len+1),
146 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000147 }
148};
149
150//===---------------------------------------===//
151// 'strncat' Optimizations
152
Chris Lattner3e8b6632009-09-02 06:11:42 +0000153struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000154 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
155 // Verify the "strncat" function prototype.
156 const FunctionType *FT = Callee->getFunctionType();
157 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000158 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000159 FT->getParamType(0) != FT->getReturnType() ||
160 FT->getParamType(1) != FT->getReturnType() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000161 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000162 return 0;
163
164 // Extract some information from the instruction
Eric Christopher551754c2010-04-16 23:37:20 +0000165 Value *Dst = CI->getOperand(1);
166 Value *Src = CI->getOperand(2);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000167 uint64_t Len;
168
169 // We don't do anything if length is not constant
Eric Christopher551754c2010-04-16 23:37:20 +0000170 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000171 Len = LengthArg->getZExtValue();
172 else
173 return 0;
174
175 // See if we can get the length of the input string.
176 uint64_t SrcLen = GetStringLength(Src);
177 if (SrcLen == 0) return 0;
178 --SrcLen; // Unbias length.
179
180 // Handle the simple, do-nothing cases:
181 // strncat(x, "", c) -> x
182 // strncat(x, c, 0) -> x
183 if (SrcLen == 0 || Len == 0) return Dst;
184
Dan Gohmanf14d9192009-08-18 00:48:13 +0000185 // These optimizations require TargetData.
186 if (!TD) return 0;
187
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000188 // We don't optimize this case
189 if (Len < SrcLen) return 0;
190
191 // strncat(x, s, c) -> strcat(x, s)
192 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000193 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000194 return Dst;
195 }
196};
197
198//===---------------------------------------===//
199// 'strchr' Optimizations
200
Chris Lattner3e8b6632009-09-02 06:11:42 +0000201struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000202 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000203 // Verify the "strchr" function prototype.
204 const FunctionType *FT = Callee->getFunctionType();
205 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000206 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000207 FT->getParamType(0) != FT->getReturnType())
208 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000209
Eric Christopher551754c2010-04-16 23:37:20 +0000210 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000211
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000212 // If the second operand is non-constant, see if we can compute the length
213 // of the input string and turn this into memchr.
Eric Christopher551754c2010-04-16 23:37:20 +0000214 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000215 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000216 // These optimizations require TargetData.
217 if (!TD) return 0;
218
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000219 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000220 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000221 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000222
Eric Christopher551754c2010-04-16 23:37:20 +0000223 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000224 ConstantInt::get(TD->getIntPtrType(*Context), Len),
225 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000226 }
227
228 // Otherwise, the character is a constant, see if the first argument is
229 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000230 std::string Str;
231 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000232 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000233
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000234 // strchr can find the nul character.
235 Str += '\0';
236 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000237
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000238 // Compute the offset.
239 uint64_t i = 0;
240 while (1) {
241 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000242 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000243 // Did we find our match?
244 if (Str[i] == CharValue)
245 break;
246 ++i;
247 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000248
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000249 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000250 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000251 return B.CreateGEP(SrcStr, Idx, "strchr");
252 }
253};
254
255//===---------------------------------------===//
256// 'strcmp' Optimizations
257
Chris Lattner3e8b6632009-09-02 06:11:42 +0000258struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000259 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000260 // Verify the "strcmp" function prototype.
261 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000262 if (FT->getNumParams() != 2 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000263 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000264 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000265 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000266 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000267
Eric Christopher551754c2010-04-16 23:37:20 +0000268 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000269 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000270 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000271
Bill Wendling0582ae92009-03-13 04:39:26 +0000272 std::string Str1, Str2;
273 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
274 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000275
Bill Wendling0582ae92009-03-13 04:39:26 +0000276 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000277 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000278
Bill Wendling0582ae92009-03-13 04:39:26 +0000279 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000280 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000281
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000282 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000283 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000284 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000285 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000286
287 // strcmp(P, "x") -> memcmp(P, "x", 2)
288 uint64_t Len1 = GetStringLength(Str1P);
289 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000290 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000291 // These optimizations require TargetData.
292 if (!TD) return 0;
293
Nick Lewycky13a09e22008-12-21 00:19:21 +0000294 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000295 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000296 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000297 }
298
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000299 return 0;
300 }
301};
302
303//===---------------------------------------===//
304// 'strncmp' Optimizations
305
Chris Lattner3e8b6632009-09-02 06:11:42 +0000306struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000307 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000308 // Verify the "strncmp" function prototype.
309 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000310 if (FT->getNumParams() != 3 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000311 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000312 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000313 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000314 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000315 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000316
Eric Christopher551754c2010-04-16 23:37:20 +0000317 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000318 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000319 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000320
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000321 // Get the length argument if it is constant.
322 uint64_t Length;
Eric Christopher551754c2010-04-16 23:37:20 +0000323 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000324 Length = LengthArg->getZExtValue();
325 else
326 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000327
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000328 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000329 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000330
Bill Wendling0582ae92009-03-13 04:39:26 +0000331 std::string Str1, Str2;
332 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
333 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000334
Bill Wendling0582ae92009-03-13 04:39:26 +0000335 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000336 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000337
Bill Wendling0582ae92009-03-13 04:39:26 +0000338 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000339 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000340
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000341 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000342 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000343 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000344 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000345 return 0;
346 }
347};
348
349
350//===---------------------------------------===//
351// 'strcpy' Optimizations
352
Chris Lattner3e8b6632009-09-02 06:11:42 +0000353struct StrCpyOpt : public LibCallOptimization {
Evan Chengeb8c6452010-03-24 20:19:04 +0000354 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
355
356 StrCpyOpt(bool c) : OptChkCall(c) {}
357
Eric Christopher7a61d702008-08-08 19:39:37 +0000358 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000359 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000360 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000361 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000362 if (FT->getNumParams() != NumParams ||
363 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000364 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000365 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000366 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000367
Eric Christopher551754c2010-04-16 23:37:20 +0000368 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000369 if (Dst == Src) // strcpy(x,x) -> x
370 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000371
Dan Gohmanf14d9192009-08-18 00:48:13 +0000372 // These optimizations require TargetData.
373 if (!TD) return 0;
374
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000375 // See if we can get the length of the input string.
376 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000377 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000378
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000379 // We have enough information to now generate the memcpy call to do the
380 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000381 if (OptChkCall)
382 EmitMemCpyChk(Dst, Src,
383 ConstantInt::get(TD->getIntPtrType(*Context), Len),
Eric Christopher551754c2010-04-16 23:37:20 +0000384 CI->getOperand(3), B, TD);
Evan Cheng0289b412010-03-23 15:48:04 +0000385 else
386 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000387 ConstantInt::get(TD->getIntPtrType(*Context), Len),
388 1, false, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000389 return Dst;
390 }
391};
392
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000393//===---------------------------------------===//
394// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000395
Chris Lattner3e8b6632009-09-02 06:11:42 +0000396struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000397 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
398 const FunctionType *FT = Callee->getFunctionType();
399 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
400 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000401 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000402 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000403 return 0;
404
Eric Christopher551754c2010-04-16 23:37:20 +0000405 Value *Dst = CI->getOperand(1);
406 Value *Src = CI->getOperand(2);
407 Value *LenOp = CI->getOperand(3);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000408
409 // See if we can get the length of the input string.
410 uint64_t SrcLen = GetStringLength(Src);
411 if (SrcLen == 0) return 0;
412 --SrcLen;
413
414 if (SrcLen == 0) {
415 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Mon P Wang20adc9d2010-04-04 03:10:48 +0000416 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'),
417 LenOp, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000418 return Dst;
419 }
420
421 uint64_t Len;
422 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
423 Len = LengthArg->getZExtValue();
424 else
425 return 0;
426
427 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
428
Dan Gohmanf14d9192009-08-18 00:48:13 +0000429 // These optimizations require TargetData.
430 if (!TD) return 0;
431
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000432 // Let strncpy handle the zero padding
433 if (Len > SrcLen+1) return 0;
434
435 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000436 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000437 ConstantInt::get(TD->getIntPtrType(*Context), Len),
438 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000439
440 return Dst;
441 }
442};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000443
444//===---------------------------------------===//
445// 'strlen' Optimizations
446
Chris Lattner3e8b6632009-09-02 06:11:42 +0000447struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000448 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000449 const FunctionType *FT = Callee->getFunctionType();
450 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000451 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000452 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000453 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000454
Eric Christopher551754c2010-04-16 23:37:20 +0000455 Value *Src = CI->getOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000456
457 // Constant folding: strlen("xyz") -> 3
458 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000459 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000460
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000461 // strlen(x) != 0 --> *x != 0
462 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000463 if (IsOnlyUsedInZeroEqualityComparison(CI))
464 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
465 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000466 }
467};
468
469//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000470// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000471
Chris Lattner3e8b6632009-09-02 06:11:42 +0000472struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000473 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
474 const FunctionType *FT = Callee->getFunctionType();
475 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000476 !FT->getParamType(0)->isPointerTy() ||
477 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000478 return 0;
479
Eric Christopher551754c2010-04-16 23:37:20 +0000480 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000481 if (isa<ConstantPointerNull>(EndPtr)) {
482 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000483 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000484 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000485
486 return 0;
487 }
488};
489
Chris Lattner24604112009-12-16 09:32:05 +0000490//===---------------------------------------===//
491// 'strstr' Optimizations
492
493struct StrStrOpt : public LibCallOptimization {
494 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
495 const FunctionType *FT = Callee->getFunctionType();
496 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000497 !FT->getParamType(0)->isPointerTy() ||
498 !FT->getParamType(1)->isPointerTy() ||
499 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000500 return 0;
501
502 // fold strstr(x, x) -> x.
Eric Christopher551754c2010-04-16 23:37:20 +0000503 if (CI->getOperand(1) == CI->getOperand(2))
504 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000505
Chris Lattner24604112009-12-16 09:32:05 +0000506 // See if either input string is a constant string.
507 std::string SearchStr, ToFindStr;
Eric Christopher551754c2010-04-16 23:37:20 +0000508 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
509 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000510
Chris Lattner24604112009-12-16 09:32:05 +0000511 // fold strstr(x, "") -> x.
512 if (HasStr2 && ToFindStr.empty())
Eric Christopher551754c2010-04-16 23:37:20 +0000513 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000514
Chris Lattner24604112009-12-16 09:32:05 +0000515 // If both strings are known, constant fold it.
516 if (HasStr1 && HasStr2) {
517 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000518
Chris Lattner24604112009-12-16 09:32:05 +0000519 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
520 return Constant::getNullValue(CI->getType());
521
522 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000523 Value *Result = CastToCStr(CI->getOperand(1), B);
Chris Lattner24604112009-12-16 09:32:05 +0000524 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
525 return B.CreateBitCast(Result, CI->getType());
526 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000527
Chris Lattner24604112009-12-16 09:32:05 +0000528 // fold strstr(x, "y") -> strchr(x, 'y').
529 if (HasStr2 && ToFindStr.size() == 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000530 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B, TD),
Chris Lattner24604112009-12-16 09:32:05 +0000531 CI->getType());
532 return 0;
533 }
534};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000535
Nick Lewycky4c498412009-02-13 15:31:46 +0000536
537//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000538// 'memcmp' Optimizations
539
Chris Lattner3e8b6632009-09-02 06:11:42 +0000540struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000541 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000542 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000543 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
544 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000545 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000546 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000547
Eric Christopher551754c2010-04-16 23:37:20 +0000548 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000549
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000550 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000551 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000552
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000553 // Make sure we have a constant length.
Eric Christopher551754c2010-04-16 23:37:20 +0000554 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000555 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000556 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000557
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000558 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000559 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000560
Benjamin Kramer48aefe12010-05-25 22:53:43 +0000561 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
562 if (Len == 1) {
563 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
564 CI->getType(), "lhsv");
565 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
566 CI->getType(), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +0000567 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000568 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000569
Benjamin Kramer992a6372009-11-05 17:44:22 +0000570 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
571 std::string LHSStr, RHSStr;
572 if (GetConstantStringInfo(LHS, LHSStr) &&
573 GetConstantStringInfo(RHS, RHSStr)) {
574 // Make sure we're not reading out-of-bounds memory.
575 if (Len > LHSStr.length() || Len > RHSStr.length())
576 return 0;
577 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
578 return ConstantInt::get(CI->getType(), Ret);
579 }
580
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000581 return 0;
582 }
583};
584
585//===---------------------------------------===//
586// 'memcpy' Optimizations
587
Chris Lattner3e8b6632009-09-02 06:11:42 +0000588struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000589 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000590 // These optimizations require TargetData.
591 if (!TD) return 0;
592
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000593 const FunctionType *FT = Callee->getFunctionType();
594 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000595 !FT->getParamType(0)->isPointerTy() ||
596 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000597 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000598 return 0;
599
600 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000601 EmitMemCpy(CI->getOperand(1), CI->getOperand(2),
602 CI->getOperand(3), 1, false, B, TD);
603 return CI->getOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000604 }
605};
606
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000607//===---------------------------------------===//
608// 'memmove' Optimizations
609
Chris Lattner3e8b6632009-09-02 06:11:42 +0000610struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000611 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000612 // These optimizations require TargetData.
613 if (!TD) return 0;
614
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000615 const FunctionType *FT = Callee->getFunctionType();
616 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000617 !FT->getParamType(0)->isPointerTy() ||
618 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000619 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000620 return 0;
621
622 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000623 EmitMemMove(CI->getOperand(1), CI->getOperand(2),
624 CI->getOperand(3), 1, false, B, TD);
625 return CI->getOperand(1);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000626 }
627};
628
629//===---------------------------------------===//
630// 'memset' Optimizations
631
Chris Lattner3e8b6632009-09-02 06:11:42 +0000632struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000633 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000634 // These optimizations require TargetData.
635 if (!TD) return 0;
636
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000637 const FunctionType *FT = Callee->getFunctionType();
638 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000639 !FT->getParamType(0)->isPointerTy() ||
640 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000641 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000642 return 0;
643
644 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher551754c2010-04-16 23:37:20 +0000645 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
646 false);
647 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), false, B, TD);
648 return CI->getOperand(1);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000649 }
650};
651
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000652//===----------------------------------------------------------------------===//
653// Math Library Optimizations
654//===----------------------------------------------------------------------===//
655
656//===---------------------------------------===//
657// 'pow*' Optimizations
658
Chris Lattner3e8b6632009-09-02 06:11:42 +0000659struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000660 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000661 const FunctionType *FT = Callee->getFunctionType();
662 // Just make sure this has 2 arguments of the same FP type, which match the
663 // result type.
664 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
665 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000666 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000667 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000668
Eric Christopher551754c2010-04-16 23:37:20 +0000669 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000670 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
671 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
672 return Op1C;
673 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000674 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000675 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000676
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000677 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
678 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000679
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000680 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000681 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000682
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000683 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000684 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
685 // This is faster than calling pow, and still handles negative zero
686 // and negative infinite correctly.
687 // TODO: In fast-math mode, this could be just sqrt(x).
688 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000689 Value *Inf = ConstantFP::getInfinity(CI->getType());
690 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000691 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
692 Callee->getAttributes());
693 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
694 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000695 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
696 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
697 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000698 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000699
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000700 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
701 return Op1;
702 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000703 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000704 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000705 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000706 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000707 return 0;
708 }
709};
710
711//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000712// 'exp2' Optimizations
713
Chris Lattner3e8b6632009-09-02 06:11:42 +0000714struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000715 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000716 const FunctionType *FT = Callee->getFunctionType();
717 // Just make sure this has 1 argument of FP type, which matches the
718 // result type.
719 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000720 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000721 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000722
Eric Christopher551754c2010-04-16 23:37:20 +0000723 Value *Op = CI->getOperand(1);
Chris Lattnere818f772008-05-02 18:43:35 +0000724 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
725 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
726 Value *LdExpArg = 0;
727 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
728 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000729 LdExpArg = B.CreateSExt(OpC->getOperand(0),
730 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000731 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
732 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000733 LdExpArg = B.CreateZExt(OpC->getOperand(0),
734 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000735 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000736
Chris Lattnere818f772008-05-02 18:43:35 +0000737 if (LdExpArg) {
738 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000739 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000740 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000741 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000742 Name = "ldexp";
743 else
744 Name = "ldexpl";
745
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000746 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000747 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000748 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000749
750 Module *M = Caller->getParent();
751 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000752 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000753 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000754 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
755 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
756 CI->setCallingConv(F->getCallingConv());
757
758 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000759 }
760 return 0;
761 }
762};
Chris Lattnere818f772008-05-02 18:43:35 +0000763
764//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000765// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
766
Chris Lattner3e8b6632009-09-02 06:11:42 +0000767struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000768 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000769 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000770 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
771 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000772 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000773
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000774 // If this is something like 'floor((double)floatval)', convert to floorf.
Eric Christopher551754c2010-04-16 23:37:20 +0000775 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000776 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000777 return 0;
778
779 // floor((double)floatval) -> (double)floorf(floatval)
780 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000781 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
782 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000783 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000784 }
785};
786
787//===----------------------------------------------------------------------===//
788// Integer Optimizations
789//===----------------------------------------------------------------------===//
790
791//===---------------------------------------===//
792// 'ffs*' Optimizations
793
Chris Lattner3e8b6632009-09-02 06:11:42 +0000794struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000795 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000796 const FunctionType *FT = Callee->getFunctionType();
797 // Just make sure this has 2 arguments of the same FP type, which match the
798 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000799 if (FT->getNumParams() != 1 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000800 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000801 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000802 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000803
Eric Christopher551754c2010-04-16 23:37:20 +0000804 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000805
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000806 // Constant fold.
807 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
808 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000809 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000810 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000811 CI->getValue().countTrailingZeros()+1);
812 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000813
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000814 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
815 const Type *ArgType = Op->getType();
816 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
817 Intrinsic::cttz, &ArgType, 1);
818 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000819 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000820 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000821
Owen Andersona7235ea2009-07-31 20:28:14 +0000822 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000823 return B.CreateSelect(Cond, V,
824 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000825 }
826};
827
828//===---------------------------------------===//
829// 'isdigit' Optimizations
830
Chris Lattner3e8b6632009-09-02 06:11:42 +0000831struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000832 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000833 const FunctionType *FT = Callee->getFunctionType();
834 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000835 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000836 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000837 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000838
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000839 // isdigit(c) -> (c-'0') <u 10
Eric Christopher551754c2010-04-16 23:37:20 +0000840 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000841 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000842 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000843 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000844 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000845 return B.CreateZExt(Op, CI->getType());
846 }
847};
848
849//===---------------------------------------===//
850// 'isascii' Optimizations
851
Chris Lattner3e8b6632009-09-02 06:11:42 +0000852struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000853 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000854 const FunctionType *FT = Callee->getFunctionType();
855 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000856 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000857 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000858 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000859
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000860 // isascii(c) -> c <u 128
Eric Christopher551754c2010-04-16 23:37:20 +0000861 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000862 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000863 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000864 return B.CreateZExt(Op, CI->getType());
865 }
866};
Eric Christopher37c8b862009-10-07 21:14:25 +0000867
Chris Lattner313f0e62008-06-09 08:26:51 +0000868//===---------------------------------------===//
869// 'abs', 'labs', 'llabs' Optimizations
870
Chris Lattner3e8b6632009-09-02 06:11:42 +0000871struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000872 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +0000873 const FunctionType *FT = Callee->getFunctionType();
874 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +0000875 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +0000876 FT->getParamType(0) != FT->getReturnType())
877 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000878
Chris Lattner313f0e62008-06-09 08:26:51 +0000879 // abs(x) -> x >s -1 ? x : -x
Eric Christopher551754c2010-04-16 23:37:20 +0000880 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000881 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +0000882 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +0000883 "ispos");
884 Value *Neg = B.CreateNeg(Op, "neg");
885 return B.CreateSelect(Pos, Op, Neg);
886 }
887};
Eric Christopher37c8b862009-10-07 21:14:25 +0000888
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000889
890//===---------------------------------------===//
891// 'toascii' Optimizations
892
Chris Lattner3e8b6632009-09-02 06:11:42 +0000893struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000894 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000895 const FunctionType *FT = Callee->getFunctionType();
896 // We require i32(i32)
897 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000898 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000899 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000900
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000901 // isascii(c) -> c & 0x7f
Eric Christopher551754c2010-04-16 23:37:20 +0000902 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +0000903 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000904 }
905};
906
907//===----------------------------------------------------------------------===//
908// Formatting and IO Optimizations
909//===----------------------------------------------------------------------===//
910
911//===---------------------------------------===//
912// 'printf' Optimizations
913
Chris Lattner3e8b6632009-09-02 06:11:42 +0000914struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000915 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000916 // Require one fixed pointer argument and an integer/void result.
917 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000918 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
919 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000920 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000921 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000922
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000923 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000924 std::string FormatStr;
Eric Christopher551754c2010-04-16 23:37:20 +0000925 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +0000926 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000927
928 // Empty format string -> noop.
929 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +0000930 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000931 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000932
Chris Lattner74965f22009-11-09 04:57:04 +0000933 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
934 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000935 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +0000936 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000937 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +0000938 if (CI->use_empty()) return CI;
939 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000940 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000941
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000942 // printf("foo\n") --> puts("foo")
943 if (FormatStr[FormatStr.size()-1] == '\n' &&
944 FormatStr.find('%') == std::string::npos) { // no format characters.
945 // Create a string literal with no \n on it. We expect the constant merge
946 // pass to be run after this pass, to merge duplicate strings.
947 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000948 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +0000949 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
950 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +0000951 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000952 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000953 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000954 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000955
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000956 // Optimize specific format strings.
Eric Christopher551754c2010-04-16 23:37:20 +0000957 // printf("%c", chr) --> putchar(*(i8*)dst)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000958 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
Eric Christopher551754c2010-04-16 23:37:20 +0000959 CI->getOperand(2)->getType()->isIntegerTy()) {
960 Value *Res = EmitPutChar(CI->getOperand(2), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000961
Chris Lattner74965f22009-11-09 04:57:04 +0000962 if (CI->use_empty()) return CI;
963 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000964 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000965
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000966 // printf("%s\n", str) --> puts(str)
967 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
Eric Christopher551754c2010-04-16 23:37:20 +0000968 CI->getOperand(2)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000969 CI->use_empty()) {
Eric Christopher551754c2010-04-16 23:37:20 +0000970 EmitPutS(CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000971 return CI;
972 }
973 return 0;
974 }
975};
976
977//===---------------------------------------===//
978// 'sprintf' Optimizations
979
Chris Lattner3e8b6632009-09-02 06:11:42 +0000980struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000981 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000982 // Require two fixed pointer arguments and an integer result.
983 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000984 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
985 !FT->getParamType(1)->isPointerTy() ||
986 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000987 return 0;
988
989 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000990 std::string FormatStr;
Eric Christopher551754c2010-04-16 23:37:20 +0000991 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +0000992 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000993
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000994 // If we just have a format string (nothing else crazy) transform it.
995 if (CI->getNumOperands() == 3) {
996 // Make sure there's no % in the constant array. We could try to handle
997 // %% -> % in the future if we cared.
998 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
999 if (FormatStr[i] == '%')
1000 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001001
1002 // These optimizations require TargetData.
1003 if (!TD) return 0;
1004
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001005 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
Eric Christopher551754c2010-04-16 23:37:20 +00001006 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Eric Christopherb6174e32010-03-05 22:25:30 +00001007 ConstantInt::get(TD->getIntPtrType(*Context),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001008 FormatStr.size()+1), 1, false, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001009 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001010 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001011
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001012 // The remaining optimizations require the format string to be "%s" or "%c"
1013 // and have an extra operand.
1014 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1015 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001016
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001017 // Decode the second character of the format string.
1018 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001019 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Eric Christopher551754c2010-04-16 23:37:20 +00001020 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
1021 Value *V = B.CreateTrunc(CI->getOperand(3),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001022 Type::getInt8Ty(*Context), "char");
Eric Christopher551754c2010-04-16 23:37:20 +00001023 Value *Ptr = CastToCStr(CI->getOperand(1), B);
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001024 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001025 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001026 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001027 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001028
Owen Andersoneed707b2009-07-24 23:12:02 +00001029 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001030 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001031
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001032 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001033 // These optimizations require TargetData.
1034 if (!TD) return 0;
1035
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001036 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Eric Christopher551754c2010-04-16 23:37:20 +00001037 if (!CI->getOperand(3)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001038
Eric Christopher551754c2010-04-16 23:37:20 +00001039 Value *Len = EmitStrLen(CI->getOperand(3), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001040 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001041 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001042 "leninc");
Eric Christopher551754c2010-04-16 23:37:20 +00001043 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, false, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001044
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001045 // The sprintf result is the unincremented number of bytes in the string.
1046 return B.CreateIntCast(Len, CI->getType(), false);
1047 }
1048 return 0;
1049 }
1050};
1051
1052//===---------------------------------------===//
1053// 'fwrite' Optimizations
1054
Chris Lattner3e8b6632009-09-02 06:11:42 +00001055struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001056 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001057 // Require a pointer, an integer, an integer, a pointer, returning integer.
1058 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001059 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1060 !FT->getParamType(1)->isIntegerTy() ||
1061 !FT->getParamType(2)->isIntegerTy() ||
1062 !FT->getParamType(3)->isPointerTy() ||
1063 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001064 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001065
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001066 // Get the element size and count.
Eric Christopher551754c2010-04-16 23:37:20 +00001067 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1068 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001069 if (!SizeC || !CountC) return 0;
1070 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001071
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001072 // If this is writing zero records, remove the call (it's a noop).
1073 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001074 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001075
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001076 // If this is writing one byte, turn it into fputc.
1077 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Eric Christopher551754c2010-04-16 23:37:20 +00001078 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1079 EmitFPutC(Char, CI->getOperand(4), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001080 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001081 }
1082
1083 return 0;
1084 }
1085};
1086
1087//===---------------------------------------===//
1088// 'fputs' Optimizations
1089
Chris Lattner3e8b6632009-09-02 06:11:42 +00001090struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001091 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001092 // These optimizations require TargetData.
1093 if (!TD) return 0;
1094
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001095 // Require two pointers. Also, we can't optimize if return value is used.
1096 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001097 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1098 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001099 !CI->use_empty())
1100 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001101
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001102 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
Eric Christopher551754c2010-04-16 23:37:20 +00001103 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001104 if (!Len) return 0;
Eric Christopher551754c2010-04-16 23:37:20 +00001105 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001106 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Eric Christopher551754c2010-04-16 23:37:20 +00001107 CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001108 return CI; // Known to have no uses (see above).
1109 }
1110};
1111
1112//===---------------------------------------===//
1113// 'fprintf' Optimizations
1114
Chris Lattner3e8b6632009-09-02 06:11:42 +00001115struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001116 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001117 // Require two fixed paramters as pointers and integer result.
1118 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001119 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1120 !FT->getParamType(1)->isPointerTy() ||
1121 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001122 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001123
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001124 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001125 std::string FormatStr;
Eric Christopher551754c2010-04-16 23:37:20 +00001126 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001127 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001128
1129 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1130 if (CI->getNumOperands() == 3) {
1131 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1132 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001133 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001134
1135 // These optimizations require TargetData.
1136 if (!TD) return 0;
1137
Eric Christopher551754c2010-04-16 23:37:20 +00001138 EmitFWrite(CI->getOperand(2),
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001139 ConstantInt::get(TD->getIntPtrType(*Context),
1140 FormatStr.size()),
Eric Christopher551754c2010-04-16 23:37:20 +00001141 CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001142 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001143 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001144
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001145 // The remaining optimizations require the format string to be "%s" or "%c"
1146 // and have an extra operand.
1147 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1148 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001149
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001150 // Decode the second character of the format string.
1151 if (FormatStr[1] == 'c') {
Eric Christopher551754c2010-04-16 23:37:20 +00001152 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1153 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
1154 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001155 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001156 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001157
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001158 if (FormatStr[1] == 's') {
Eric Christopher551754c2010-04-16 23:37:20 +00001159 // fprintf(F, "%s", str) -> fputs(str, F)
1160 if (!CI->getOperand(3)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001161 return 0;
Eric Christopher551754c2010-04-16 23:37:20 +00001162 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001163 return CI;
1164 }
1165 return 0;
1166 }
1167};
1168
Bill Wendlingac178222008-05-05 21:37:59 +00001169} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001170
1171//===----------------------------------------------------------------------===//
1172// SimplifyLibCalls Pass Implementation
1173//===----------------------------------------------------------------------===//
1174
1175namespace {
1176 /// This pass optimizes well known library functions from libc and libm.
1177 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001178 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001179 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001180 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001181 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
Evan Cheng0289b412010-03-23 15:48:04 +00001182 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
1183 StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001184 StrToOpt StrTo; StrStrOpt StrStr;
1185 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001186 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001187 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001188 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001189 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1190 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001191 // Formatting and IO Optimizations
1192 SPrintFOpt SPrintF; PrintFOpt PrintF;
1193 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001194
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001195 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001196 public:
1197 static char ID; // Pass identification
Evan Chengeb8c6452010-03-24 20:19:04 +00001198 SimplifyLibCalls() : FunctionPass(&ID), StrCpy(false), StrCpyChk(true) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001199 void InitOptimizations();
1200 bool runOnFunction(Function &F);
1201
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001202 void setDoesNotAccessMemory(Function &F);
1203 void setOnlyReadsMemory(Function &F);
1204 void setDoesNotThrow(Function &F);
1205 void setDoesNotCapture(Function &F, unsigned n);
1206 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001207 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001208
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001209 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001210 }
1211 };
1212 char SimplifyLibCalls::ID = 0;
1213} // end anonymous namespace.
1214
1215static RegisterPass<SimplifyLibCalls>
1216X("simplify-libcalls", "Simplify well-known library calls");
1217
1218// Public interface to the Simplify LibCalls pass.
1219FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001220 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001221}
1222
1223/// Optimizations - Populate the Optimizations map with all the optimizations
1224/// we know.
1225void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001226 // String and Memory LibCall Optimizations
1227 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001228 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001229 Optimizations["strchr"] = &StrChr;
1230 Optimizations["strcmp"] = &StrCmp;
1231 Optimizations["strncmp"] = &StrNCmp;
1232 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001233 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001234 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001235 Optimizations["strtol"] = &StrTo;
1236 Optimizations["strtod"] = &StrTo;
1237 Optimizations["strtof"] = &StrTo;
1238 Optimizations["strtoul"] = &StrTo;
1239 Optimizations["strtoll"] = &StrTo;
1240 Optimizations["strtold"] = &StrTo;
1241 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001242 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001243 Optimizations["memcmp"] = &MemCmp;
1244 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001245 Optimizations["memmove"] = &MemMove;
1246 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001247
Evan Cheng0289b412010-03-23 15:48:04 +00001248 // _chk variants of String and Memory LibCall Optimizations.
Evan Cheng0289b412010-03-23 15:48:04 +00001249 Optimizations["__strcpy_chk"] = &StrCpyChk;
1250
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001251 // Math Library Optimizations
1252 Optimizations["powf"] = &Pow;
1253 Optimizations["pow"] = &Pow;
1254 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001255 Optimizations["llvm.pow.f32"] = &Pow;
1256 Optimizations["llvm.pow.f64"] = &Pow;
1257 Optimizations["llvm.pow.f80"] = &Pow;
1258 Optimizations["llvm.pow.f128"] = &Pow;
1259 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001260 Optimizations["exp2l"] = &Exp2;
1261 Optimizations["exp2"] = &Exp2;
1262 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001263 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1264 Optimizations["llvm.exp2.f128"] = &Exp2;
1265 Optimizations["llvm.exp2.f80"] = &Exp2;
1266 Optimizations["llvm.exp2.f64"] = &Exp2;
1267 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001268
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001269#ifdef HAVE_FLOORF
1270 Optimizations["floor"] = &UnaryDoubleFP;
1271#endif
1272#ifdef HAVE_CEILF
1273 Optimizations["ceil"] = &UnaryDoubleFP;
1274#endif
1275#ifdef HAVE_ROUNDF
1276 Optimizations["round"] = &UnaryDoubleFP;
1277#endif
1278#ifdef HAVE_RINTF
1279 Optimizations["rint"] = &UnaryDoubleFP;
1280#endif
1281#ifdef HAVE_NEARBYINTF
1282 Optimizations["nearbyint"] = &UnaryDoubleFP;
1283#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001284
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001285 // Integer Optimizations
1286 Optimizations["ffs"] = &FFS;
1287 Optimizations["ffsl"] = &FFS;
1288 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001289 Optimizations["abs"] = &Abs;
1290 Optimizations["labs"] = &Abs;
1291 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001292 Optimizations["isdigit"] = &IsDigit;
1293 Optimizations["isascii"] = &IsAscii;
1294 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001295
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001296 // Formatting and IO Optimizations
1297 Optimizations["sprintf"] = &SPrintF;
1298 Optimizations["printf"] = &PrintF;
1299 Optimizations["fwrite"] = &FWrite;
1300 Optimizations["fputs"] = &FPuts;
1301 Optimizations["fprintf"] = &FPrintF;
1302}
1303
1304
1305/// runOnFunction - Top level algorithm.
1306///
1307bool SimplifyLibCalls::runOnFunction(Function &F) {
1308 if (Optimizations.empty())
1309 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001310
Dan Gohmanf14d9192009-08-18 00:48:13 +00001311 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001312
Owen Andersone922c022009-07-22 00:24:57 +00001313 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001314
1315 bool Changed = false;
1316 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1317 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1318 // Ignore non-calls.
1319 CallInst *CI = dyn_cast<CallInst>(I++);
1320 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001321
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001322 // Ignore indirect calls and calls to non-external functions.
1323 Function *Callee = CI->getCalledFunction();
1324 if (Callee == 0 || !Callee->isDeclaration() ||
1325 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1326 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001327
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001328 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001329 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1330 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001331
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001332 // Set the builder to the instruction after the call.
1333 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001334
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001335 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001336 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001337 if (Result == 0) continue;
1338
David Greene6a6b90e2010-01-05 01:27:21 +00001339 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1340 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001341
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001342 // Something changed!
1343 Changed = true;
1344 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001345
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001346 // Inspect the instruction after the call (which was potentially just
1347 // added) next.
1348 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001349
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001350 if (CI != Result && !CI->use_empty()) {
1351 CI->replaceAllUsesWith(Result);
1352 if (!Result->hasName())
1353 Result->takeName(CI);
1354 }
1355 CI->eraseFromParent();
1356 }
1357 }
1358 return Changed;
1359}
1360
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001361// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001362
1363void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1364 if (!F.doesNotAccessMemory()) {
1365 F.setDoesNotAccessMemory();
1366 ++NumAnnotated;
1367 Modified = true;
1368 }
1369}
1370void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1371 if (!F.onlyReadsMemory()) {
1372 F.setOnlyReadsMemory();
1373 ++NumAnnotated;
1374 Modified = true;
1375 }
1376}
1377void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1378 if (!F.doesNotThrow()) {
1379 F.setDoesNotThrow();
1380 ++NumAnnotated;
1381 Modified = true;
1382 }
1383}
1384void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1385 if (!F.doesNotCapture(n)) {
1386 F.setDoesNotCapture(n);
1387 ++NumAnnotated;
1388 Modified = true;
1389 }
1390}
1391void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1392 if (!F.doesNotAlias(n)) {
1393 F.setDoesNotAlias(n);
1394 ++NumAnnotated;
1395 Modified = true;
1396 }
1397}
1398
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001399/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001400///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001401bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001402 Modified = false;
1403 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1404 Function &F = *I;
1405 if (!F.isDeclaration())
1406 continue;
1407
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001408 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001409 continue;
1410
1411 const FunctionType *FTy = F.getFunctionType();
1412
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001413 StringRef Name = F.getName();
1414 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001415 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001416 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001417 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001418 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001419 continue;
1420 setOnlyReadsMemory(F);
1421 setDoesNotThrow(F);
1422 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001423 } else if (Name == "strchr" ||
1424 Name == "strrchr") {
1425 if (FTy->getNumParams() != 2 ||
1426 !FTy->getParamType(0)->isPointerTy() ||
1427 !FTy->getParamType(1)->isIntegerTy())
1428 continue;
1429 setOnlyReadsMemory(F);
1430 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001431 } else if (Name == "strcpy" ||
1432 Name == "stpcpy" ||
1433 Name == "strcat" ||
1434 Name == "strtol" ||
1435 Name == "strtod" ||
1436 Name == "strtof" ||
1437 Name == "strtoul" ||
1438 Name == "strtoll" ||
1439 Name == "strtold" ||
1440 Name == "strncat" ||
1441 Name == "strncpy" ||
1442 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001443 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001444 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001445 continue;
1446 setDoesNotThrow(F);
1447 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001448 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001449 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001450 !FTy->getParamType(0)->isPointerTy() ||
1451 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001452 continue;
1453 setDoesNotThrow(F);
1454 setDoesNotCapture(F, 1);
1455 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001456 } else if (Name == "strcmp" ||
1457 Name == "strspn" ||
1458 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001459 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001460 Name == "strcoll" ||
1461 Name == "strcasecmp" ||
1462 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001463 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001464 !FTy->getParamType(0)->isPointerTy() ||
1465 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001466 continue;
1467 setOnlyReadsMemory(F);
1468 setDoesNotThrow(F);
1469 setDoesNotCapture(F, 1);
1470 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001471 } else if (Name == "strstr" ||
1472 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001473 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001474 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001475 continue;
1476 setOnlyReadsMemory(F);
1477 setDoesNotThrow(F);
1478 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001479 } else if (Name == "strtok" ||
1480 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001481 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001482 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001483 continue;
1484 setDoesNotThrow(F);
1485 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001486 } else if (Name == "scanf" ||
1487 Name == "setbuf" ||
1488 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001489 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001490 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001491 continue;
1492 setDoesNotThrow(F);
1493 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001494 } else if (Name == "strdup" ||
1495 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001496 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001497 !FTy->getReturnType()->isPointerTy() ||
1498 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001499 continue;
1500 setDoesNotThrow(F);
1501 setDoesNotAlias(F, 0);
1502 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001503 } else if (Name == "stat" ||
1504 Name == "sscanf" ||
1505 Name == "sprintf" ||
1506 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001507 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001508 !FTy->getParamType(0)->isPointerTy() ||
1509 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001510 continue;
1511 setDoesNotThrow(F);
1512 setDoesNotCapture(F, 1);
1513 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001514 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001515 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001516 !FTy->getParamType(0)->isPointerTy() ||
1517 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001518 continue;
1519 setDoesNotThrow(F);
1520 setDoesNotCapture(F, 1);
1521 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001522 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001523 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001524 !FTy->getParamType(1)->isPointerTy() ||
1525 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001526 continue;
1527 setDoesNotThrow(F);
1528 setDoesNotCapture(F, 2);
1529 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001530 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001531 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001532 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001533 continue;
1534 // May throw; "system" is a valid pthread cancellation point.
1535 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001536 }
1537 break;
1538 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001539 if (Name == "malloc") {
1540 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001541 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001542 continue;
1543 setDoesNotThrow(F);
1544 setDoesNotAlias(F, 0);
1545 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001546 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001547 !FTy->getParamType(0)->isPointerTy() ||
1548 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001549 continue;
1550 setOnlyReadsMemory(F);
1551 setDoesNotThrow(F);
1552 setDoesNotCapture(F, 1);
1553 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001554 } else if (Name == "memchr" ||
1555 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001556 if (FTy->getNumParams() != 3)
1557 continue;
1558 setOnlyReadsMemory(F);
1559 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001560 } else if (Name == "modf" ||
1561 Name == "modff" ||
1562 Name == "modfl" ||
1563 Name == "memcpy" ||
1564 Name == "memccpy" ||
1565 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001566 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001567 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001568 continue;
1569 setDoesNotThrow(F);
1570 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001571 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001572 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001573 continue;
1574 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001575 } else if (Name == "mkdir" ||
1576 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001577 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001578 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001579 continue;
1580 setDoesNotThrow(F);
1581 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001582 }
1583 break;
1584 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001585 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001586 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001587 !FTy->getParamType(0)->isPointerTy() ||
1588 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001589 continue;
1590 setDoesNotThrow(F);
1591 setDoesNotAlias(F, 0);
1592 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001593 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001594 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001595 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001596 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001597 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001598 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001599 } else if (Name == "rmdir" ||
1600 Name == "rewind" ||
1601 Name == "remove" ||
1602 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001603 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001604 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001605 continue;
1606 setDoesNotThrow(F);
1607 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001608 } else if (Name == "rename" ||
1609 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001610 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001611 !FTy->getParamType(0)->isPointerTy() ||
1612 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001613 continue;
1614 setDoesNotThrow(F);
1615 setDoesNotCapture(F, 1);
1616 setDoesNotCapture(F, 2);
1617 }
1618 break;
1619 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001620 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001621 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001622 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001623 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001624 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001625 setDoesNotCapture(F, 2);
1626 }
1627 break;
1628 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001629 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001630 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001631 !FTy->getParamType(0)->isPointerTy() ||
1632 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001633 continue;
1634 setDoesNotThrow(F);
1635 setDoesNotCapture(F, 1);
1636 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001637 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001638 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001639 !FTy->getParamType(0)->isPointerTy() ||
1640 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001641 continue;
1642 setDoesNotThrow(F);
1643 setOnlyReadsMemory(F);
1644 setDoesNotCapture(F, 1);
1645 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001646 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001647 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001648 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001649 continue;
1650 setDoesNotThrow(F);
1651 setDoesNotCapture(F, 1);
1652 }
1653 break;
1654 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001655 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001656 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001657 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001658 continue;
1659 setDoesNotThrow(F);
1660 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001661 } else if (Name == "chmod" ||
1662 Name == "chown" ||
1663 Name == "ctermid" ||
1664 Name == "clearerr" ||
1665 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001666 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001667 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001668 continue;
1669 setDoesNotThrow(F);
1670 setDoesNotCapture(F, 1);
1671 }
1672 break;
1673 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001674 if (Name == "atoi" ||
1675 Name == "atol" ||
1676 Name == "atof" ||
1677 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001678 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001679 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001680 continue;
1681 setDoesNotThrow(F);
1682 setOnlyReadsMemory(F);
1683 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001684 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001685 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001686 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001687 continue;
1688 setDoesNotThrow(F);
1689 setDoesNotCapture(F, 1);
1690 }
1691 break;
1692 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001693 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001694 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001695 !FTy->getReturnType()->isPointerTy() ||
1696 !FTy->getParamType(0)->isPointerTy() ||
1697 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001698 continue;
1699 setDoesNotThrow(F);
1700 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001701 setDoesNotCapture(F, 1);
1702 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001703 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001704 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001705 !FTy->getReturnType()->isPointerTy() ||
1706 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001707 continue;
1708 setDoesNotThrow(F);
1709 setDoesNotAlias(F, 0);
1710 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001711 } else if (Name == "feof" ||
1712 Name == "free" ||
1713 Name == "fseek" ||
1714 Name == "ftell" ||
1715 Name == "fgetc" ||
1716 Name == "fseeko" ||
1717 Name == "ftello" ||
1718 Name == "fileno" ||
1719 Name == "fflush" ||
1720 Name == "fclose" ||
1721 Name == "fsetpos" ||
1722 Name == "flockfile" ||
1723 Name == "funlockfile" ||
1724 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001725 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001726 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001727 continue;
1728 setDoesNotThrow(F);
1729 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001730 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001731 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001732 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001733 continue;
1734 setDoesNotThrow(F);
1735 setDoesNotCapture(F, 1);
1736 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001737 } else if (Name == "fputc" ||
1738 Name == "fstat" ||
1739 Name == "frexp" ||
1740 Name == "frexpf" ||
1741 Name == "frexpl" ||
1742 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001743 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001744 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001745 continue;
1746 setDoesNotThrow(F);
1747 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001748 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001749 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001750 !FTy->getParamType(0)->isPointerTy() ||
1751 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001752 continue;
1753 setDoesNotThrow(F);
1754 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001755 } else if (Name == "fread" ||
1756 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001757 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001758 !FTy->getParamType(0)->isPointerTy() ||
1759 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001760 continue;
1761 setDoesNotThrow(F);
1762 setDoesNotCapture(F, 1);
1763 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001764 } else if (Name == "fputs" ||
1765 Name == "fscanf" ||
1766 Name == "fprintf" ||
1767 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001768 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001769 !FTy->getParamType(0)->isPointerTy() ||
1770 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001771 continue;
1772 setDoesNotThrow(F);
1773 setDoesNotCapture(F, 1);
1774 setDoesNotCapture(F, 2);
1775 }
1776 break;
1777 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001778 if (Name == "getc" ||
1779 Name == "getlogin_r" ||
1780 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001781 if (FTy->getNumParams() == 0 ||
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 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001786 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001787 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001788 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001789 continue;
1790 setDoesNotThrow(F);
1791 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001792 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001793 } else if (Name == "gets" ||
1794 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001795 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001796 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001797 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001798 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001799 continue;
1800 setDoesNotThrow(F);
1801 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001802 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001803 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001804 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001805 continue;
1806 setDoesNotThrow(F);
1807 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001808 }
1809 break;
1810 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001811 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001812 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001813 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001814 continue;
1815 setDoesNotThrow(F);
1816 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001817 } else if (Name == "uname" ||
1818 Name == "unlink" ||
1819 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001820 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001821 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001822 continue;
1823 setDoesNotThrow(F);
1824 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001825 } else if (Name == "utime" ||
1826 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001827 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001828 !FTy->getParamType(0)->isPointerTy() ||
1829 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001830 continue;
1831 setDoesNotThrow(F);
1832 setDoesNotCapture(F, 1);
1833 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001834 }
1835 break;
1836 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001837 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001838 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001839 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001840 continue;
1841 setDoesNotThrow(F);
1842 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001843 } else if (Name == "puts" ||
1844 Name == "printf" ||
1845 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001846 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001847 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001848 continue;
1849 setDoesNotThrow(F);
1850 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001851 } else if (Name == "pread" ||
1852 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001853 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001854 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001855 continue;
1856 // May throw; these are valid pthread cancellation points.
1857 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001858 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001859 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001860 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001861 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001862 !FTy->getReturnType()->isPointerTy() ||
1863 !FTy->getParamType(0)->isPointerTy() ||
1864 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001865 continue;
1866 setDoesNotThrow(F);
1867 setDoesNotAlias(F, 0);
1868 setDoesNotCapture(F, 1);
1869 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001870 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001871 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001872 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001873 continue;
1874 setDoesNotThrow(F);
1875 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001876 }
1877 break;
1878 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001879 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001880 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001881 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001882 continue;
1883 setDoesNotThrow(F);
1884 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001885 } else if (Name == "vsscanf" ||
1886 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001887 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001888 !FTy->getParamType(1)->isPointerTy() ||
1889 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001890 continue;
1891 setDoesNotThrow(F);
1892 setDoesNotCapture(F, 1);
1893 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001894 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00001895 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001896 continue;
1897 setDoesNotThrow(F);
1898 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001899 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001900 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001901 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001902 continue;
1903 setDoesNotThrow(F);
1904 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001905 } else if (Name == "vfprintf" ||
1906 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001907 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001908 !FTy->getParamType(0)->isPointerTy() ||
1909 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001910 continue;
1911 setDoesNotThrow(F);
1912 setDoesNotCapture(F, 1);
1913 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001914 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001915 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001916 !FTy->getParamType(0)->isPointerTy() ||
1917 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001918 continue;
1919 setDoesNotThrow(F);
1920 setDoesNotCapture(F, 1);
1921 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001922 }
1923 break;
1924 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001925 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001926 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001927 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001928 continue;
1929 // May throw; "open" is a valid pthread cancellation point.
1930 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001931 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001932 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001933 !FTy->getReturnType()->isPointerTy() ||
1934 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001935 continue;
1936 setDoesNotThrow(F);
1937 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00001938 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001939 }
1940 break;
1941 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001942 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00001943 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001944 continue;
1945 setDoesNotThrow(F);
1946 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001947 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001948 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001949 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001950 continue;
1951 setDoesNotThrow(F);
1952 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001953 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001954 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001955 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001956 if (Name == "htonl" ||
1957 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001958 setDoesNotThrow(F);
1959 setDoesNotAccessMemory(F);
1960 }
1961 break;
1962 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001963 if (Name == "ntohl" ||
1964 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001965 setDoesNotThrow(F);
1966 setDoesNotAccessMemory(F);
1967 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001968 break;
1969 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001970 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001971 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001972 !FTy->getParamType(0)->isPointerTy() ||
1973 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001974 continue;
1975 setDoesNotThrow(F);
1976 setDoesNotCapture(F, 1);
1977 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001978 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001979 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001980 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001981 continue;
1982 setDoesNotThrow(F);
1983 setDoesNotCapture(F, 1);
1984 }
1985 break;
1986 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001987 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001988 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001989 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001990 continue;
1991 // May throw; places call through function pointer.
1992 setDoesNotCapture(F, 4);
1993 }
1994 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001995 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001996 if (Name == "__strdup" ||
1997 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001998 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001999 !FTy->getReturnType()->isPointerTy() ||
2000 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002001 continue;
2002 setDoesNotThrow(F);
2003 setDoesNotAlias(F, 0);
2004 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002005 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002006 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002007 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002008 continue;
2009 setDoesNotThrow(F);
2010 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002011 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002012 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002013 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002014 continue;
2015 setDoesNotThrow(F);
2016 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002017 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002018 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002019 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002020 continue;
2021 setDoesNotThrow(F);
2022 setDoesNotCapture(F, 2);
2023 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002024 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002025 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002026 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002027 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002028 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002029 continue;
2030 setDoesNotThrow(F);
2031 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002032 } else if (Name == "\1stat64" ||
2033 Name == "\1lstat64" ||
2034 Name == "\1statvfs64" ||
2035 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002036 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002037 !FTy->getParamType(0)->isPointerTy() ||
2038 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002039 continue;
2040 setDoesNotThrow(F);
2041 setDoesNotCapture(F, 1);
2042 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002043 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002044 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002045 !FTy->getReturnType()->isPointerTy() ||
2046 !FTy->getParamType(0)->isPointerTy() ||
2047 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002048 continue;
2049 setDoesNotThrow(F);
2050 setDoesNotAlias(F, 0);
2051 setDoesNotCapture(F, 1);
2052 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002053 } else if (Name == "\1fseeko64" ||
2054 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002055 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002056 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002057 continue;
2058 setDoesNotThrow(F);
2059 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002060 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002061 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002062 continue;
2063 setDoesNotThrow(F);
2064 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002065 } else if (Name == "\1fstat64" ||
2066 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002067 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002068 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002069 continue;
2070 setDoesNotThrow(F);
2071 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002072 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002073 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002074 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002075 continue;
2076 // May throw; "open" is a valid pthread cancellation point.
2077 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002078 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002079 break;
2080 }
2081 }
2082 return Modified;
2083}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002084
2085// TODO:
2086// Additional cases that we need to add to this file:
2087//
2088// cbrt:
2089// * cbrt(expN(X)) -> expN(x/3)
2090// * cbrt(sqrt(x)) -> pow(x,1/6)
2091// * cbrt(sqrt(x)) -> pow(x,1/9)
2092//
2093// cos, cosf, cosl:
2094// * cos(-x) -> cos(x)
2095//
2096// exp, expf, expl:
2097// * exp(log(x)) -> x
2098//
2099// log, logf, logl:
2100// * log(exp(x)) -> x
2101// * log(x**y) -> y*log(x)
2102// * log(exp(y)) -> y*log(e)
2103// * log(exp2(y)) -> y*log(2)
2104// * log(exp10(y)) -> y*log(10)
2105// * log(sqrt(x)) -> 0.5*log(x)
2106// * log(pow(x,y)) -> y*log(x)
2107//
2108// lround, lroundf, lroundl:
2109// * lround(cnst) -> cnst'
2110//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002111// pow, powf, powl:
2112// * pow(exp(x),y) -> exp(x*y)
2113// * pow(sqrt(x),y) -> pow(x,y*0.5)
2114// * pow(pow(x,y),z)-> pow(x,y*z)
2115//
2116// puts:
2117// * puts("") -> putchar("\n")
2118//
2119// round, roundf, roundl:
2120// * round(cnst) -> cnst'
2121//
2122// signbit:
2123// * signbit(cnst) -> cnst'
2124// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2125//
2126// sqrt, sqrtf, sqrtl:
2127// * sqrt(expN(x)) -> expN(x*0.5)
2128// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2129// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2130//
2131// stpcpy:
2132// * stpcpy(str, "literal") ->
2133// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2134// strrchr:
2135// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2136// (if c is a constant integer and s is a constant string)
2137// * strrchr(s1,0) -> strchr(s1,0)
2138//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002139// strpbrk:
2140// * strpbrk(s,a) -> offset_in_for(s,a)
2141// (if s and a are both constant strings)
2142// * strpbrk(s,"") -> 0
2143// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2144//
2145// strspn, strcspn:
2146// * strspn(s,a) -> const_int (if both args are constant)
2147// * strspn("",a) -> 0
2148// * strspn(s,"") -> 0
2149// * strcspn(s,a) -> const_int (if both args are constant)
2150// * strcspn("",a) -> 0
2151// * strcspn(s,"") -> strlen(a)
2152//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002153// tan, tanf, tanl:
2154// * tan(atan(x)) -> x
2155//
2156// trunc, truncf, truncl:
2157// * trunc(cnst) -> cnst'
2158//
2159//