blob: fffd3250738af23157648b07bf62310a94aa72e8 [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();
Rafael Espindolae96af562010-06-16 19:34:01 +000069
70 // We never change the calling convention.
71 if (CI->getCallingConv() != llvm::CallingConv::C)
72 return NULL;
73
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000074 return CallOptimizer(CI->getCalledFunction(), CI, B);
75 }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000076};
77} // End anonymous namespace.
78
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000079
80//===----------------------------------------------------------------------===//
81// Helper Functions
82//===----------------------------------------------------------------------===//
83
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000084/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +000085/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000086static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
87 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
88 UI != E; ++UI) {
89 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
90 if (IC->isEquality())
91 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
92 if (C->isNullValue())
93 continue;
94 // Unknown instruction.
95 return false;
96 }
97 return true;
98}
99
Benjamin Kramer386e9182010-06-15 21:34:25 +0000100/// IsOnlyUsedInEqualityComparison - Return true if it is only used in equality
101/// comparisons with With.
102static bool IsOnlyUsedInEqualityComparison(Value *V, Value *With) {
103 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
104 UI != E; ++UI) {
105 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
106 if (IC->isEquality() && IC->getOperand(1) == With)
107 continue;
108 // Unknown instruction.
109 return false;
110 }
111 return true;
112}
113
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000114//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000115// String and Memory LibCall Optimizations
116//===----------------------------------------------------------------------===//
117
118//===---------------------------------------===//
119// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000120namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000121struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000122 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000123 // Verify the "strcat" function prototype.
124 const FunctionType *FT = Callee->getFunctionType();
125 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000126 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000127 FT->getParamType(0) != FT->getReturnType() ||
128 FT->getParamType(1) != FT->getReturnType())
129 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000130
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000131 // Extract some information from the instruction
Gabor Greifaee5dc12010-06-24 10:42:46 +0000132 Value *Dst = CI->getArgOperand(0);
133 Value *Src = CI->getArgOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000134
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000135 // See if we can get the length of the input string.
136 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000137 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000138 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000139
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000140 // Handle the simple, do-nothing case: strcat(x, "") -> x
141 if (Len == 0)
142 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000143
144 // These optimizations require TargetData.
145 if (!TD) return 0;
146
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000147 EmitStrLenMemCpy(Src, Dst, Len, B);
148 return Dst;
149 }
150
151 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000152 // We need to find the end of the destination string. That's where the
153 // memory is to be moved to. We just generate a call to strlen.
Eric Christopherb6174e32010-03-05 22:25:30 +0000154 Value *DstLen = EmitStrLen(Dst, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000155
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000156 // Now that we have the destination's length, we must index into the
157 // destination's pointer to get the actual memcpy destination (end of
158 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000159 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000160
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000161 // We have enough information to now generate the memcpy call to do the
162 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000163 EmitMemCpy(CpyDst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000164 ConstantInt::get(TD->getIntPtrType(*Context), Len+1),
165 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000166 }
167};
168
169//===---------------------------------------===//
170// 'strncat' Optimizations
171
Chris Lattner3e8b6632009-09-02 06:11:42 +0000172struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000173 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
174 // Verify the "strncat" function prototype.
175 const FunctionType *FT = Callee->getFunctionType();
176 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000177 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000178 FT->getParamType(0) != FT->getReturnType() ||
179 FT->getParamType(1) != FT->getReturnType() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000180 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000181 return 0;
182
183 // Extract some information from the instruction
Gabor Greifaee5dc12010-06-24 10:42:46 +0000184 Value *Dst = CI->getArgOperand(0);
185 Value *Src = CI->getArgOperand(1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000186 uint64_t Len;
187
188 // We don't do anything if length is not constant
Gabor Greifaee5dc12010-06-24 10:42:46 +0000189 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000190 Len = LengthArg->getZExtValue();
191 else
192 return 0;
193
194 // See if we can get the length of the input string.
195 uint64_t SrcLen = GetStringLength(Src);
196 if (SrcLen == 0) return 0;
197 --SrcLen; // Unbias length.
198
199 // Handle the simple, do-nothing cases:
200 // strncat(x, "", c) -> x
201 // strncat(x, c, 0) -> x
202 if (SrcLen == 0 || Len == 0) return Dst;
203
Dan Gohmanf14d9192009-08-18 00:48:13 +0000204 // These optimizations require TargetData.
205 if (!TD) return 0;
206
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000207 // We don't optimize this case
208 if (Len < SrcLen) return 0;
209
210 // strncat(x, s, c) -> strcat(x, s)
211 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000212 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000213 return Dst;
214 }
215};
216
217//===---------------------------------------===//
218// 'strchr' Optimizations
219
Chris Lattner3e8b6632009-09-02 06:11:42 +0000220struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000221 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000222 // Verify the "strchr" function prototype.
223 const FunctionType *FT = Callee->getFunctionType();
224 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000225 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000226 FT->getParamType(0) != FT->getReturnType())
227 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000228
Gabor Greifaee5dc12010-06-24 10:42:46 +0000229 Value *SrcStr = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000230
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000231 // If the second operand is non-constant, see if we can compute the length
232 // of the input string and turn this into memchr.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000233 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000234 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000235 // These optimizations require TargetData.
236 if (!TD) return 0;
237
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000238 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000239 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000240 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000241
Gabor Greifaee5dc12010-06-24 10:42:46 +0000242 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000243 ConstantInt::get(TD->getIntPtrType(*Context), Len),
244 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000245 }
246
247 // Otherwise, the character is a constant, see if the first argument is
248 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000249 std::string Str;
250 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000251 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000252
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000253 // strchr can find the nul character.
254 Str += '\0';
255 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000256
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000257 // Compute the offset.
258 uint64_t i = 0;
259 while (1) {
260 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000261 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000262 // Did we find our match?
263 if (Str[i] == CharValue)
264 break;
265 ++i;
266 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000267
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000268 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000269 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000270 return B.CreateGEP(SrcStr, Idx, "strchr");
271 }
272};
273
274//===---------------------------------------===//
Benjamin Kramer06f25cf2010-09-29 21:50:51 +0000275// 'strrchr' Optimizations
276
277struct StrRChrOpt : public LibCallOptimization {
278 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
279 // Verify the "strrchr" function prototype.
280 const FunctionType *FT = Callee->getFunctionType();
281 if (FT->getNumParams() != 2 ||
282 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
283 FT->getParamType(0) != FT->getReturnType())
284 return 0;
285
286 Value *SrcStr = CI->getArgOperand(0);
287 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
288
289 // Cannot fold anything if we're not looking for a constant.
290 if (!CharC)
291 return 0;
292
293 std::string Str;
294 if (!GetConstantStringInfo(SrcStr, Str)) {
295 // strrchr(s, 0) -> strchr(s, 0)
296 if (TD && CharC->isZero())
297 return EmitStrChr(SrcStr, '\0', B, TD);
298 return 0;
299 }
300
301 // strrchr can find the nul character.
302 Str += '\0';
303
304 // Compute the offset.
305 size_t I = Str.rfind(CharC->getSExtValue());
306 if (I == std::string::npos) // Didn't find the char. Return null.
307 return Constant::getNullValue(CI->getType());
308
309 // strrchr(s+n,c) -> gep(s+n+i,c)
310 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
311 return B.CreateGEP(SrcStr, Idx, "strrchr");
312 }
313};
314
315//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000316// 'strcmp' Optimizations
317
Chris Lattner3e8b6632009-09-02 06:11:42 +0000318struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000319 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000320 // Verify the "strcmp" function prototype.
321 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000322 if (FT->getNumParams() != 2 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000323 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000324 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000325 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000326 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000327
Gabor Greifaee5dc12010-06-24 10:42:46 +0000328 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000329 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000330 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000331
Bill Wendling0582ae92009-03-13 04:39:26 +0000332 std::string Str1, Str2;
333 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
334 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000335
Bill Wendling0582ae92009-03-13 04:39:26 +0000336 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000337 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000338
Bill Wendling0582ae92009-03-13 04:39:26 +0000339 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000340 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000341
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000342 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000343 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000344 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000345 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000346
347 // strcmp(P, "x") -> memcmp(P, "x", 2)
348 uint64_t Len1 = GetStringLength(Str1P);
349 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000350 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000351 // These optimizations require TargetData.
352 if (!TD) return 0;
353
Nick Lewycky13a09e22008-12-21 00:19:21 +0000354 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000355 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000356 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000357 }
358
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000359 return 0;
360 }
361};
362
363//===---------------------------------------===//
364// 'strncmp' Optimizations
365
Chris Lattner3e8b6632009-09-02 06:11:42 +0000366struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000367 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000368 // Verify the "strncmp" function prototype.
369 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000370 if (FT->getNumParams() != 3 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000371 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000372 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000373 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000374 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000375 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000376
Gabor Greifaee5dc12010-06-24 10:42:46 +0000377 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000378 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000379 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000380
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000381 // Get the length argument if it is constant.
382 uint64_t Length;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000383 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000384 Length = LengthArg->getZExtValue();
385 else
386 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000387
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000388 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000389 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000390
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000391 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000392 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD);
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000393
Bill Wendling0582ae92009-03-13 04:39:26 +0000394 std::string Str1, Str2;
395 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
396 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000397
Bill Wendling0582ae92009-03-13 04:39:26 +0000398 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000399 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000400
Bill Wendling0582ae92009-03-13 04:39:26 +0000401 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000402 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000403
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000404 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000405 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000406 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000407 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000408 return 0;
409 }
410};
411
412
413//===---------------------------------------===//
414// 'strcpy' Optimizations
415
Chris Lattner3e8b6632009-09-02 06:11:42 +0000416struct StrCpyOpt : public LibCallOptimization {
Evan Chengeb8c6452010-03-24 20:19:04 +0000417 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
418
419 StrCpyOpt(bool c) : OptChkCall(c) {}
420
Eric Christopher7a61d702008-08-08 19:39:37 +0000421 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000422 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000423 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000424 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000425 if (FT->getNumParams() != NumParams ||
426 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000427 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000428 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000429 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000430
Gabor Greifaee5dc12010-06-24 10:42:46 +0000431 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000432 if (Dst == Src) // strcpy(x,x) -> x
433 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000434
Dan Gohmanf14d9192009-08-18 00:48:13 +0000435 // These optimizations require TargetData.
436 if (!TD) return 0;
437
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000438 // See if we can get the length of the input string.
439 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000440 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000441
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000442 // We have enough information to now generate the memcpy call to do the
443 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000444 if (OptChkCall)
445 EmitMemCpyChk(Dst, Src,
446 ConstantInt::get(TD->getIntPtrType(*Context), Len),
Gabor Greifaee5dc12010-06-24 10:42:46 +0000447 CI->getArgOperand(2), B, TD);
Evan Cheng0289b412010-03-23 15:48:04 +0000448 else
449 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000450 ConstantInt::get(TD->getIntPtrType(*Context), Len),
451 1, false, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000452 return Dst;
453 }
454};
455
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000456//===---------------------------------------===//
457// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000458
Chris Lattner3e8b6632009-09-02 06:11:42 +0000459struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000460 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
461 const FunctionType *FT = Callee->getFunctionType();
462 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
463 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000464 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000465 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000466 return 0;
467
Gabor Greifaee5dc12010-06-24 10:42:46 +0000468 Value *Dst = CI->getArgOperand(0);
469 Value *Src = CI->getArgOperand(1);
470 Value *LenOp = CI->getArgOperand(2);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000471
472 // See if we can get the length of the input string.
473 uint64_t SrcLen = GetStringLength(Src);
474 if (SrcLen == 0) return 0;
475 --SrcLen;
476
477 if (SrcLen == 0) {
478 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Mon P Wang20adc9d2010-04-04 03:10:48 +0000479 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'),
480 LenOp, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000481 return Dst;
482 }
483
484 uint64_t Len;
485 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
486 Len = LengthArg->getZExtValue();
487 else
488 return 0;
489
490 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
491
Dan Gohmanf14d9192009-08-18 00:48:13 +0000492 // These optimizations require TargetData.
493 if (!TD) return 0;
494
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000495 // Let strncpy handle the zero padding
496 if (Len > SrcLen+1) return 0;
497
498 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000499 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000500 ConstantInt::get(TD->getIntPtrType(*Context), Len),
501 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000502
503 return Dst;
504 }
505};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000506
507//===---------------------------------------===//
508// 'strlen' Optimizations
509
Chris Lattner3e8b6632009-09-02 06:11:42 +0000510struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000511 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000512 const FunctionType *FT = Callee->getFunctionType();
513 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000514 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000515 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000516 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000517
Gabor Greifaee5dc12010-06-24 10:42:46 +0000518 Value *Src = CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000519
520 // Constant folding: strlen("xyz") -> 3
521 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000522 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000523
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000524 // strlen(x) != 0 --> *x != 0
525 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000526 if (IsOnlyUsedInZeroEqualityComparison(CI))
527 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
528 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000529 }
530};
531
532//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000533// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000534
Chris Lattner3e8b6632009-09-02 06:11:42 +0000535struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000536 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
537 const FunctionType *FT = Callee->getFunctionType();
538 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000539 !FT->getParamType(0)->isPointerTy() ||
540 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000541 return 0;
542
Gabor Greifaee5dc12010-06-24 10:42:46 +0000543 Value *EndPtr = CI->getArgOperand(1);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000544 if (isa<ConstantPointerNull>(EndPtr)) {
545 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000546 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000547 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000548
549 return 0;
550 }
551};
552
Chris Lattner24604112009-12-16 09:32:05 +0000553//===---------------------------------------===//
554// 'strstr' Optimizations
555
556struct StrStrOpt : public LibCallOptimization {
557 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
558 const FunctionType *FT = Callee->getFunctionType();
559 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000560 !FT->getParamType(0)->isPointerTy() ||
561 !FT->getParamType(1)->isPointerTy() ||
562 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000563 return 0;
564
565 // fold strstr(x, x) -> x.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000566 if (CI->getArgOperand(0) == CI->getArgOperand(1))
567 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000568
Benjamin Kramer386e9182010-06-15 21:34:25 +0000569 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000570 if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
571 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD);
572 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000573 StrLen, B, TD);
574 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
575 UI != UE; ) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000576 ICmpInst *Old = cast<ICmpInst>(*UI++);
Benjamin Kramer386e9182010-06-15 21:34:25 +0000577 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
578 ConstantInt::getNullValue(StrNCmp->getType()),
579 "cmp");
580 Old->replaceAllUsesWith(Cmp);
581 Old->eraseFromParent();
582 }
583 return CI;
584 }
585
Chris Lattner24604112009-12-16 09:32:05 +0000586 // See if either input string is a constant string.
587 std::string SearchStr, ToFindStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000588 bool HasStr1 = GetConstantStringInfo(CI->getArgOperand(0), SearchStr);
589 bool HasStr2 = GetConstantStringInfo(CI->getArgOperand(1), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000590
Chris Lattner24604112009-12-16 09:32:05 +0000591 // fold strstr(x, "") -> x.
592 if (HasStr2 && ToFindStr.empty())
Gabor Greifaee5dc12010-06-24 10:42:46 +0000593 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000594
Chris Lattner24604112009-12-16 09:32:05 +0000595 // If both strings are known, constant fold it.
596 if (HasStr1 && HasStr2) {
597 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000598
Chris Lattner24604112009-12-16 09:32:05 +0000599 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
600 return Constant::getNullValue(CI->getType());
601
602 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000603 Value *Result = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner24604112009-12-16 09:32:05 +0000604 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
605 return B.CreateBitCast(Result, CI->getType());
606 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000607
Chris Lattner24604112009-12-16 09:32:05 +0000608 // fold strstr(x, "y") -> strchr(x, 'y').
609 if (HasStr2 && ToFindStr.size() == 1)
Gabor Greifa3997812010-07-22 10:37:47 +0000610 return B.CreateBitCast(EmitStrChr(CI->getArgOperand(0),
611 ToFindStr[0], B, TD), CI->getType());
Chris Lattner24604112009-12-16 09:32:05 +0000612 return 0;
613 }
614};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000615
Nick Lewycky4c498412009-02-13 15:31:46 +0000616
617//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000618// 'memcmp' Optimizations
619
Chris Lattner3e8b6632009-09-02 06:11:42 +0000620struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000621 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000622 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000623 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
624 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000625 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000626 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000627
Gabor Greifaee5dc12010-06-24 10:42:46 +0000628 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000629
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000630 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000631 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000632
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000633 // Make sure we have a constant length.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000634 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000635 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000636 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000637
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000638 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000639 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000640
Benjamin Kramer48aefe12010-05-25 22:53:43 +0000641 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
642 if (Len == 1) {
643 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
644 CI->getType(), "lhsv");
645 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
646 CI->getType(), "rhsv");
Benjamin Kramer1464c1d2010-05-26 09:45:04 +0000647 return B.CreateSub(LHSV, RHSV, "chardiff");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000648 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000649
Benjamin Kramer992a6372009-11-05 17:44:22 +0000650 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
651 std::string LHSStr, RHSStr;
652 if (GetConstantStringInfo(LHS, LHSStr) &&
653 GetConstantStringInfo(RHS, RHSStr)) {
654 // Make sure we're not reading out-of-bounds memory.
655 if (Len > LHSStr.length() || Len > RHSStr.length())
656 return 0;
657 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
658 return ConstantInt::get(CI->getType(), Ret);
659 }
660
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000661 return 0;
662 }
663};
664
665//===---------------------------------------===//
666// 'memcpy' Optimizations
667
Chris Lattner3e8b6632009-09-02 06:11:42 +0000668struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000669 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000670 // These optimizations require TargetData.
671 if (!TD) return 0;
672
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000673 const FunctionType *FT = Callee->getFunctionType();
674 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000675 !FT->getParamType(0)->isPointerTy() ||
676 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000677 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000678 return 0;
679
680 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000681 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
682 CI->getArgOperand(2), 1, false, B, TD);
683 return CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000684 }
685};
686
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000687//===---------------------------------------===//
688// 'memmove' Optimizations
689
Chris Lattner3e8b6632009-09-02 06:11:42 +0000690struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000691 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000692 // These optimizations require TargetData.
693 if (!TD) return 0;
694
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000695 const FunctionType *FT = Callee->getFunctionType();
696 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000697 !FT->getParamType(0)->isPointerTy() ||
698 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000699 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000700 return 0;
701
702 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000703 EmitMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
704 CI->getArgOperand(2), 1, false, B, TD);
705 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000706 }
707};
708
709//===---------------------------------------===//
710// 'memset' Optimizations
711
Chris Lattner3e8b6632009-09-02 06:11:42 +0000712struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000713 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000714 // These optimizations require TargetData.
715 if (!TD) return 0;
716
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000717 const FunctionType *FT = Callee->getFunctionType();
718 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000719 !FT->getParamType(0)->isPointerTy() ||
720 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000721 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000722 return 0;
723
724 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Gabor Greifa3997812010-07-22 10:37:47 +0000725 Value *Val = B.CreateIntCast(CI->getArgOperand(1),
726 Type::getInt8Ty(*Context), false);
Gabor Greifaee5dc12010-06-24 10:42:46 +0000727 EmitMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), false, B, TD);
728 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000729 }
730};
731
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000732//===----------------------------------------------------------------------===//
733// Math Library Optimizations
734//===----------------------------------------------------------------------===//
735
736//===---------------------------------------===//
737// 'pow*' Optimizations
738
Chris Lattner3e8b6632009-09-02 06:11:42 +0000739struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000740 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000741 const FunctionType *FT = Callee->getFunctionType();
742 // Just make sure this has 2 arguments of the same FP type, which match the
743 // result type.
744 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
745 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000746 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000747 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000748
Gabor Greifaee5dc12010-06-24 10:42:46 +0000749 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000750 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
751 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
752 return Op1C;
753 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000754 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000755 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000756
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000757 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
758 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000759
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000760 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000761 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000762
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000763 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000764 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
765 // This is faster than calling pow, and still handles negative zero
766 // and negative infinite correctly.
767 // TODO: In fast-math mode, this could be just sqrt(x).
768 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000769 Value *Inf = ConstantFP::getInfinity(CI->getType());
770 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000771 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
772 Callee->getAttributes());
773 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
774 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000775 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
776 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
777 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000778 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000779
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000780 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
781 return Op1;
782 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000783 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000784 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000785 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000786 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000787 return 0;
788 }
789};
790
791//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000792// 'exp2' Optimizations
793
Chris Lattner3e8b6632009-09-02 06:11:42 +0000794struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000795 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000796 const FunctionType *FT = Callee->getFunctionType();
797 // Just make sure this has 1 argument of FP type, which matches the
798 // result type.
799 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000800 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000801 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000802
Gabor Greifaee5dc12010-06-24 10:42:46 +0000803 Value *Op = CI->getArgOperand(0);
Chris Lattnere818f772008-05-02 18:43:35 +0000804 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
805 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
806 Value *LdExpArg = 0;
807 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
808 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000809 LdExpArg = B.CreateSExt(OpC->getOperand(0),
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000810 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000811 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
812 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000813 LdExpArg = B.CreateZExt(OpC->getOperand(0),
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000814 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000815 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000816
Chris Lattnere818f772008-05-02 18:43:35 +0000817 if (LdExpArg) {
818 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000819 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000820 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000821 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000822 Name = "ldexp";
823 else
824 Name = "ldexpl";
825
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000826 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000827 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000828 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000829
830 Module *M = Caller->getParent();
831 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000832 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000833 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000834 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
835 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
836 CI->setCallingConv(F->getCallingConv());
837
838 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000839 }
840 return 0;
841 }
842};
Chris Lattnere818f772008-05-02 18:43:35 +0000843
844//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000845// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
846
Chris Lattner3e8b6632009-09-02 06:11:42 +0000847struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000848 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000849 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000850 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
851 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000852 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000853
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000854 // If this is something like 'floor((double)floatval)', convert to floorf.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000855 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000856 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000857 return 0;
858
859 // floor((double)floatval) -> (double)floorf(floatval)
860 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000861 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
862 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000863 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000864 }
865};
866
867//===----------------------------------------------------------------------===//
868// Integer Optimizations
869//===----------------------------------------------------------------------===//
870
871//===---------------------------------------===//
872// 'ffs*' Optimizations
873
Chris Lattner3e8b6632009-09-02 06:11:42 +0000874struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000875 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000876 const FunctionType *FT = Callee->getFunctionType();
877 // Just make sure this has 2 arguments of the same FP type, which match the
878 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000879 if (FT->getNumParams() != 1 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000880 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000881 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000882 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000883
Gabor Greifaee5dc12010-06-24 10:42:46 +0000884 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000885
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000886 // Constant fold.
887 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
888 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000889 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000890 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000891 CI->getValue().countTrailingZeros()+1);
892 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000893
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000894 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
895 const Type *ArgType = Op->getType();
896 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
897 Intrinsic::cttz, &ArgType, 1);
898 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000899 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000900 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000901
Owen Andersona7235ea2009-07-31 20:28:14 +0000902 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000903 return B.CreateSelect(Cond, V,
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000904 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000905 }
906};
907
908//===---------------------------------------===//
909// 'isdigit' Optimizations
910
Chris Lattner3e8b6632009-09-02 06:11:42 +0000911struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000912 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000913 const FunctionType *FT = Callee->getFunctionType();
914 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000915 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000916 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000917 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000918
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000919 // isdigit(c) -> (c-'0') <u 10
Gabor Greifaee5dc12010-06-24 10:42:46 +0000920 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000921 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000922 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000923 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000924 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000925 return B.CreateZExt(Op, CI->getType());
926 }
927};
928
929//===---------------------------------------===//
930// 'isascii' Optimizations
931
Chris Lattner3e8b6632009-09-02 06:11:42 +0000932struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000933 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000934 const FunctionType *FT = Callee->getFunctionType();
935 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000936 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000937 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000938 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000939
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000940 // isascii(c) -> c <u 128
Gabor Greifaee5dc12010-06-24 10:42:46 +0000941 Value *Op = CI->getArgOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +0000942 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000943 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000944 return B.CreateZExt(Op, CI->getType());
945 }
946};
Eric Christopher37c8b862009-10-07 21:14:25 +0000947
Chris Lattner313f0e62008-06-09 08:26:51 +0000948//===---------------------------------------===//
949// 'abs', 'labs', 'llabs' Optimizations
950
Chris Lattner3e8b6632009-09-02 06:11:42 +0000951struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000952 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +0000953 const FunctionType *FT = Callee->getFunctionType();
954 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +0000955 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +0000956 FT->getParamType(0) != FT->getReturnType())
957 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000958
Chris Lattner313f0e62008-06-09 08:26:51 +0000959 // abs(x) -> x >s -1 ? x : -x
Gabor Greifaee5dc12010-06-24 10:42:46 +0000960 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000961 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +0000962 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +0000963 "ispos");
964 Value *Neg = B.CreateNeg(Op, "neg");
965 return B.CreateSelect(Pos, Op, Neg);
966 }
967};
Eric Christopher37c8b862009-10-07 21:14:25 +0000968
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000969
970//===---------------------------------------===//
971// 'toascii' Optimizations
972
Chris Lattner3e8b6632009-09-02 06:11:42 +0000973struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000974 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000975 const FunctionType *FT = Callee->getFunctionType();
976 // We require i32(i32)
977 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000978 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000979 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000980
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000981 // isascii(c) -> c & 0x7f
Gabor Greifaee5dc12010-06-24 10:42:46 +0000982 return B.CreateAnd(CI->getArgOperand(0),
Owen Andersoneed707b2009-07-24 23:12:02 +0000983 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000984 }
985};
986
987//===----------------------------------------------------------------------===//
988// Formatting and IO Optimizations
989//===----------------------------------------------------------------------===//
990
991//===---------------------------------------===//
992// 'printf' Optimizations
993
Chris Lattner3e8b6632009-09-02 06:11:42 +0000994struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000995 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000996 // Require one fixed pointer argument and an integer/void result.
997 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000998 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
999 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001000 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001001 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001002
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001003 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001004 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001005 if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001006 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001007
1008 // Empty format string -> noop.
1009 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001010 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001011 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001012
Chris Lattner74965f22009-11-09 04:57:04 +00001013 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1014 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001015 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001016 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +00001017 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +00001018 if (CI->use_empty()) return CI;
1019 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001020 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001021
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001022 // printf("foo\n") --> puts("foo")
1023 if (FormatStr[FormatStr.size()-1] == '\n' &&
1024 FormatStr.find('%') == std::string::npos) { // no format characters.
1025 // Create a string literal with no \n on it. We expect the constant merge
1026 // pass to be run after this pass, to merge duplicate strings.
1027 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001028 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001029 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1030 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +00001031 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001032 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001033 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001034 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001035
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001036 // Optimize specific format strings.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001037 // printf("%c", chr) --> putchar(chr)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001038 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001039 CI->getArgOperand(1)->getType()->isIntegerTy()) {
1040 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001041
Chris Lattner74965f22009-11-09 04:57:04 +00001042 if (CI->use_empty()) return CI;
1043 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001044 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001045
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001046 // printf("%s\n", str) --> puts(str)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001047 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001048 CI->getArgOperand(1)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001049 CI->use_empty()) {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001050 EmitPutS(CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001051 return CI;
1052 }
1053 return 0;
1054 }
1055};
1056
1057//===---------------------------------------===//
1058// 'sprintf' Optimizations
1059
Chris Lattner3e8b6632009-09-02 06:11:42 +00001060struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001061 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001062 // Require two fixed pointer arguments and an integer result.
1063 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001064 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1065 !FT->getParamType(1)->isPointerTy() ||
1066 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001067 return 0;
1068
1069 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001070 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001071 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001072 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001073
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001074 // If we just have a format string (nothing else crazy) transform it.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001075 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001076 // Make sure there's no % in the constant array. We could try to handle
1077 // %% -> % in the future if we cared.
1078 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1079 if (FormatStr[i] == '%')
1080 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001081
1082 // These optimizations require TargetData.
1083 if (!TD) return 0;
1084
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001085 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
Gabor Greifa3997812010-07-22 10:37:47 +00001086 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), // Copy the
1087 ConstantInt::get(TD->getIntPtrType(*Context), // nul byte.
1088 FormatStr.size() + 1), 1, false, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001089 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001090 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001091
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001092 // The remaining optimizations require the format string to be "%s" or "%c"
1093 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001094 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1095 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001096 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001097
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001098 // Decode the second character of the format string.
1099 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001100 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Gabor Greifaee5dc12010-06-24 10:42:46 +00001101 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1102 Value *V = B.CreateTrunc(CI->getArgOperand(2),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001103 Type::getInt8Ty(*Context), "char");
Gabor Greifaee5dc12010-06-24 10:42:46 +00001104 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001105 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001106 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001107 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001108 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001109
Owen Andersoneed707b2009-07-24 23:12:02 +00001110 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001111 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001112
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001113 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001114 // These optimizations require TargetData.
1115 if (!TD) return 0;
1116
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001117 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001118 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001119
Gabor Greifaee5dc12010-06-24 10:42:46 +00001120 Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001121 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001122 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001123 "leninc");
Gabor Greifa3997812010-07-22 10:37:47 +00001124 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(2),
1125 IncLen, 1, false, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001126
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001127 // The sprintf result is the unincremented number of bytes in the string.
1128 return B.CreateIntCast(Len, CI->getType(), false);
1129 }
1130 return 0;
1131 }
1132};
1133
1134//===---------------------------------------===//
1135// 'fwrite' Optimizations
1136
Chris Lattner3e8b6632009-09-02 06:11:42 +00001137struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001138 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001139 // Require a pointer, an integer, an integer, a pointer, returning integer.
1140 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001141 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1142 !FT->getParamType(1)->isIntegerTy() ||
1143 !FT->getParamType(2)->isIntegerTy() ||
1144 !FT->getParamType(3)->isPointerTy() ||
1145 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001146 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001147
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001148 // Get the element size and count.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001149 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1150 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001151 if (!SizeC || !CountC) return 0;
1152 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001153
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001154 // If this is writing zero records, remove the call (it's a noop).
1155 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001156 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001157
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001158 // If this is writing one byte, turn it into fputc.
1159 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001160 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1161 EmitFPutC(Char, CI->getArgOperand(3), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001162 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001163 }
1164
1165 return 0;
1166 }
1167};
1168
1169//===---------------------------------------===//
1170// 'fputs' Optimizations
1171
Chris Lattner3e8b6632009-09-02 06:11:42 +00001172struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001173 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001174 // These optimizations require TargetData.
1175 if (!TD) return 0;
1176
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001177 // Require two pointers. Also, we can't optimize if return value is used.
1178 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001179 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1180 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001181 !CI->use_empty())
1182 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001183
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001184 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001185 uint64_t Len = GetStringLength(CI->getArgOperand(0));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001186 if (!Len) return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001187 EmitFWrite(CI->getArgOperand(0),
Owen Anderson1d0be152009-08-13 21:58:54 +00001188 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001189 CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001190 return CI; // Known to have no uses (see above).
1191 }
1192};
1193
1194//===---------------------------------------===//
1195// 'fprintf' Optimizations
1196
Chris Lattner3e8b6632009-09-02 06:11:42 +00001197struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001198 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001199 // Require two fixed paramters as pointers and integer result.
1200 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001201 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1202 !FT->getParamType(1)->isPointerTy() ||
1203 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001204 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001205
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001206 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001207 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001208 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001209 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001210
1211 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001212 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001213 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1214 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001215 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001216
1217 // These optimizations require TargetData.
1218 if (!TD) return 0;
1219
Gabor Greifaee5dc12010-06-24 10:42:46 +00001220 EmitFWrite(CI->getArgOperand(1),
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001221 ConstantInt::get(TD->getIntPtrType(*Context),
1222 FormatStr.size()),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001223 CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001224 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001225 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001226
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001227 // The remaining optimizations require the format string to be "%s" or "%c"
1228 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001229 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1230 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001231 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001232
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001233 // Decode the second character of the format string.
1234 if (FormatStr[1] == 'c') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001235 // fprintf(F, "%c", chr) --> fputc(chr, F)
1236 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1237 EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001238 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001239 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001240
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001241 if (FormatStr[1] == 's') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001242 // fprintf(F, "%s", str) --> fputs(str, F)
1243 if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001244 return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001245 EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001246 return CI;
1247 }
1248 return 0;
1249 }
1250};
1251
Bill Wendlingac178222008-05-05 21:37:59 +00001252} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001253
1254//===----------------------------------------------------------------------===//
1255// SimplifyLibCalls Pass Implementation
1256//===----------------------------------------------------------------------===//
1257
1258namespace {
1259 /// This pass optimizes well known library functions from libc and libm.
1260 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001261 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001262 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001263 // String and Memory LibCall Optimizations
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001264 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrRChrOpt StrRChr;
1265 StrCmpOpt StrCmp; StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
Evan Cheng0289b412010-03-23 15:48:04 +00001266 StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001267 StrToOpt StrTo; StrStrOpt StrStr;
1268 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001269 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001270 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001271 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001272 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1273 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001274 // Formatting and IO Optimizations
1275 SPrintFOpt SPrintF; PrintFOpt PrintF;
1276 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001277
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001278 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001279 public:
1280 static char ID; // Pass identification
Owen Anderson90c579d2010-08-06 18:33:48 +00001281 SimplifyLibCalls() : FunctionPass(ID), StrCpy(false), StrCpyChk(true) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001282 void InitOptimizations();
1283 bool runOnFunction(Function &F);
1284
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001285 void setDoesNotAccessMemory(Function &F);
1286 void setOnlyReadsMemory(Function &F);
1287 void setDoesNotThrow(Function &F);
1288 void setDoesNotCapture(Function &F, unsigned n);
1289 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001290 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001291
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001292 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001293 }
1294 };
1295 char SimplifyLibCalls::ID = 0;
1296} // end anonymous namespace.
1297
Owen Andersond13db2c2010-07-21 22:09:45 +00001298INITIALIZE_PASS(SimplifyLibCalls, "simplify-libcalls",
1299 "Simplify well-known library calls", false, false);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001300
1301// Public interface to the Simplify LibCalls pass.
1302FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001303 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001304}
1305
1306/// Optimizations - Populate the Optimizations map with all the optimizations
1307/// we know.
1308void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001309 // String and Memory LibCall Optimizations
1310 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001311 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001312 Optimizations["strchr"] = &StrChr;
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001313 Optimizations["strrchr"] = &StrRChr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001314 Optimizations["strcmp"] = &StrCmp;
1315 Optimizations["strncmp"] = &StrNCmp;
1316 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001317 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001318 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001319 Optimizations["strtol"] = &StrTo;
1320 Optimizations["strtod"] = &StrTo;
1321 Optimizations["strtof"] = &StrTo;
1322 Optimizations["strtoul"] = &StrTo;
1323 Optimizations["strtoll"] = &StrTo;
1324 Optimizations["strtold"] = &StrTo;
1325 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001326 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001327 Optimizations["memcmp"] = &MemCmp;
1328 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001329 Optimizations["memmove"] = &MemMove;
1330 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001331
Evan Cheng0289b412010-03-23 15:48:04 +00001332 // _chk variants of String and Memory LibCall Optimizations.
Evan Cheng0289b412010-03-23 15:48:04 +00001333 Optimizations["__strcpy_chk"] = &StrCpyChk;
1334
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001335 // Math Library Optimizations
1336 Optimizations["powf"] = &Pow;
1337 Optimizations["pow"] = &Pow;
1338 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001339 Optimizations["llvm.pow.f32"] = &Pow;
1340 Optimizations["llvm.pow.f64"] = &Pow;
1341 Optimizations["llvm.pow.f80"] = &Pow;
1342 Optimizations["llvm.pow.f128"] = &Pow;
1343 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001344 Optimizations["exp2l"] = &Exp2;
1345 Optimizations["exp2"] = &Exp2;
1346 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001347 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1348 Optimizations["llvm.exp2.f128"] = &Exp2;
1349 Optimizations["llvm.exp2.f80"] = &Exp2;
1350 Optimizations["llvm.exp2.f64"] = &Exp2;
1351 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001352
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001353#ifdef HAVE_FLOORF
1354 Optimizations["floor"] = &UnaryDoubleFP;
1355#endif
1356#ifdef HAVE_CEILF
1357 Optimizations["ceil"] = &UnaryDoubleFP;
1358#endif
1359#ifdef HAVE_ROUNDF
1360 Optimizations["round"] = &UnaryDoubleFP;
1361#endif
1362#ifdef HAVE_RINTF
1363 Optimizations["rint"] = &UnaryDoubleFP;
1364#endif
1365#ifdef HAVE_NEARBYINTF
1366 Optimizations["nearbyint"] = &UnaryDoubleFP;
1367#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001368
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001369 // Integer Optimizations
1370 Optimizations["ffs"] = &FFS;
1371 Optimizations["ffsl"] = &FFS;
1372 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001373 Optimizations["abs"] = &Abs;
1374 Optimizations["labs"] = &Abs;
1375 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001376 Optimizations["isdigit"] = &IsDigit;
1377 Optimizations["isascii"] = &IsAscii;
1378 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001379
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001380 // Formatting and IO Optimizations
1381 Optimizations["sprintf"] = &SPrintF;
1382 Optimizations["printf"] = &PrintF;
1383 Optimizations["fwrite"] = &FWrite;
1384 Optimizations["fputs"] = &FPuts;
1385 Optimizations["fprintf"] = &FPrintF;
1386}
1387
1388
1389/// runOnFunction - Top level algorithm.
1390///
1391bool SimplifyLibCalls::runOnFunction(Function &F) {
1392 if (Optimizations.empty())
1393 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001394
Dan Gohmanf14d9192009-08-18 00:48:13 +00001395 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001396
Owen Andersone922c022009-07-22 00:24:57 +00001397 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001398
1399 bool Changed = false;
1400 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1401 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1402 // Ignore non-calls.
1403 CallInst *CI = dyn_cast<CallInst>(I++);
1404 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001405
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001406 // Ignore indirect calls and calls to non-external functions.
1407 Function *Callee = CI->getCalledFunction();
1408 if (Callee == 0 || !Callee->isDeclaration() ||
1409 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1410 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001411
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001412 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001413 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1414 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001415
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001416 // Set the builder to the instruction after the call.
1417 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001418
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001419 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001420 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001421 if (Result == 0) continue;
1422
David Greene6a6b90e2010-01-05 01:27:21 +00001423 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1424 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001425
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001426 // Something changed!
1427 Changed = true;
1428 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001429
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001430 // Inspect the instruction after the call (which was potentially just
1431 // added) next.
1432 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001433
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001434 if (CI != Result && !CI->use_empty()) {
1435 CI->replaceAllUsesWith(Result);
1436 if (!Result->hasName())
1437 Result->takeName(CI);
1438 }
1439 CI->eraseFromParent();
1440 }
1441 }
1442 return Changed;
1443}
1444
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001445// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001446
1447void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1448 if (!F.doesNotAccessMemory()) {
1449 F.setDoesNotAccessMemory();
1450 ++NumAnnotated;
1451 Modified = true;
1452 }
1453}
1454void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1455 if (!F.onlyReadsMemory()) {
1456 F.setOnlyReadsMemory();
1457 ++NumAnnotated;
1458 Modified = true;
1459 }
1460}
1461void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1462 if (!F.doesNotThrow()) {
1463 F.setDoesNotThrow();
1464 ++NumAnnotated;
1465 Modified = true;
1466 }
1467}
1468void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1469 if (!F.doesNotCapture(n)) {
1470 F.setDoesNotCapture(n);
1471 ++NumAnnotated;
1472 Modified = true;
1473 }
1474}
1475void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1476 if (!F.doesNotAlias(n)) {
1477 F.setDoesNotAlias(n);
1478 ++NumAnnotated;
1479 Modified = true;
1480 }
1481}
1482
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001483/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001484///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001485bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001486 Modified = false;
1487 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1488 Function &F = *I;
1489 if (!F.isDeclaration())
1490 continue;
1491
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001492 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001493 continue;
1494
1495 const FunctionType *FTy = F.getFunctionType();
1496
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001497 StringRef Name = F.getName();
1498 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001499 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001500 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001501 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001502 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001503 continue;
1504 setOnlyReadsMemory(F);
1505 setDoesNotThrow(F);
1506 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001507 } else if (Name == "strchr" ||
1508 Name == "strrchr") {
1509 if (FTy->getNumParams() != 2 ||
1510 !FTy->getParamType(0)->isPointerTy() ||
1511 !FTy->getParamType(1)->isIntegerTy())
1512 continue;
1513 setOnlyReadsMemory(F);
1514 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001515 } else if (Name == "strcpy" ||
1516 Name == "stpcpy" ||
1517 Name == "strcat" ||
1518 Name == "strtol" ||
1519 Name == "strtod" ||
1520 Name == "strtof" ||
1521 Name == "strtoul" ||
1522 Name == "strtoll" ||
1523 Name == "strtold" ||
1524 Name == "strncat" ||
1525 Name == "strncpy" ||
1526 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001527 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001528 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001529 continue;
1530 setDoesNotThrow(F);
1531 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001532 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001533 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001534 !FTy->getParamType(0)->isPointerTy() ||
1535 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001536 continue;
1537 setDoesNotThrow(F);
1538 setDoesNotCapture(F, 1);
1539 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001540 } else if (Name == "strcmp" ||
1541 Name == "strspn" ||
1542 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001543 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001544 Name == "strcoll" ||
1545 Name == "strcasecmp" ||
1546 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001547 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001548 !FTy->getParamType(0)->isPointerTy() ||
1549 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001550 continue;
1551 setOnlyReadsMemory(F);
1552 setDoesNotThrow(F);
1553 setDoesNotCapture(F, 1);
1554 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001555 } else if (Name == "strstr" ||
1556 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001557 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001558 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001559 continue;
1560 setOnlyReadsMemory(F);
1561 setDoesNotThrow(F);
1562 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001563 } else if (Name == "strtok" ||
1564 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001565 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001566 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001567 continue;
1568 setDoesNotThrow(F);
1569 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001570 } else if (Name == "scanf" ||
1571 Name == "setbuf" ||
1572 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001573 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001574 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001575 continue;
1576 setDoesNotThrow(F);
1577 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001578 } else if (Name == "strdup" ||
1579 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001580 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001581 !FTy->getReturnType()->isPointerTy() ||
1582 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001583 continue;
1584 setDoesNotThrow(F);
1585 setDoesNotAlias(F, 0);
1586 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001587 } else if (Name == "stat" ||
1588 Name == "sscanf" ||
1589 Name == "sprintf" ||
1590 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001591 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001592 !FTy->getParamType(0)->isPointerTy() ||
1593 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001594 continue;
1595 setDoesNotThrow(F);
1596 setDoesNotCapture(F, 1);
1597 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001598 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001599 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001600 !FTy->getParamType(0)->isPointerTy() ||
1601 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001602 continue;
1603 setDoesNotThrow(F);
1604 setDoesNotCapture(F, 1);
1605 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001606 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001607 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001608 !FTy->getParamType(1)->isPointerTy() ||
1609 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001610 continue;
1611 setDoesNotThrow(F);
1612 setDoesNotCapture(F, 2);
1613 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001614 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001615 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001616 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001617 continue;
1618 // May throw; "system" is a valid pthread cancellation point.
1619 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001620 }
1621 break;
1622 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001623 if (Name == "malloc") {
1624 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001625 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001626 continue;
1627 setDoesNotThrow(F);
1628 setDoesNotAlias(F, 0);
1629 } else if (Name == "memcmp") {
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 setOnlyReadsMemory(F);
1635 setDoesNotThrow(F);
1636 setDoesNotCapture(F, 1);
1637 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001638 } else if (Name == "memchr" ||
1639 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001640 if (FTy->getNumParams() != 3)
1641 continue;
1642 setOnlyReadsMemory(F);
1643 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001644 } else if (Name == "modf" ||
1645 Name == "modff" ||
1646 Name == "modfl" ||
1647 Name == "memcpy" ||
1648 Name == "memccpy" ||
1649 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001650 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001651 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001652 continue;
1653 setDoesNotThrow(F);
1654 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001655 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001656 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001657 continue;
1658 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001659 } else if (Name == "mkdir" ||
1660 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001661 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001662 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001663 continue;
1664 setDoesNotThrow(F);
1665 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001666 }
1667 break;
1668 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001669 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001670 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001671 !FTy->getParamType(0)->isPointerTy() ||
1672 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001673 continue;
1674 setDoesNotThrow(F);
1675 setDoesNotAlias(F, 0);
1676 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001677 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001678 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001679 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001680 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001681 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001682 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001683 } else if (Name == "rmdir" ||
1684 Name == "rewind" ||
1685 Name == "remove" ||
1686 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001687 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001688 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001689 continue;
1690 setDoesNotThrow(F);
1691 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001692 } else if (Name == "rename" ||
1693 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001694 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001695 !FTy->getParamType(0)->isPointerTy() ||
1696 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001697 continue;
1698 setDoesNotThrow(F);
1699 setDoesNotCapture(F, 1);
1700 setDoesNotCapture(F, 2);
1701 }
1702 break;
1703 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001704 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001705 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001706 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001707 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001708 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001709 setDoesNotCapture(F, 2);
1710 }
1711 break;
1712 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001713 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001714 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001715 !FTy->getParamType(0)->isPointerTy() ||
1716 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001717 continue;
1718 setDoesNotThrow(F);
1719 setDoesNotCapture(F, 1);
1720 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001721 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001722 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001723 !FTy->getParamType(0)->isPointerTy() ||
1724 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001725 continue;
1726 setDoesNotThrow(F);
1727 setOnlyReadsMemory(F);
1728 setDoesNotCapture(F, 1);
1729 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001730 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001731 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001732 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001733 continue;
1734 setDoesNotThrow(F);
1735 setDoesNotCapture(F, 1);
1736 }
1737 break;
1738 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001739 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001740 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001741 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001742 continue;
1743 setDoesNotThrow(F);
1744 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001745 } else if (Name == "chmod" ||
1746 Name == "chown" ||
1747 Name == "ctermid" ||
1748 Name == "clearerr" ||
1749 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001750 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001751 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001752 continue;
1753 setDoesNotThrow(F);
1754 setDoesNotCapture(F, 1);
1755 }
1756 break;
1757 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001758 if (Name == "atoi" ||
1759 Name == "atol" ||
1760 Name == "atof" ||
1761 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001762 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001763 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001764 continue;
1765 setDoesNotThrow(F);
1766 setOnlyReadsMemory(F);
1767 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001768 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001769 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001770 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001771 continue;
1772 setDoesNotThrow(F);
1773 setDoesNotCapture(F, 1);
1774 }
1775 break;
1776 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001777 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001778 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001779 !FTy->getReturnType()->isPointerTy() ||
1780 !FTy->getParamType(0)->isPointerTy() ||
1781 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001782 continue;
1783 setDoesNotThrow(F);
1784 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001785 setDoesNotCapture(F, 1);
1786 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001787 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001788 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001789 !FTy->getReturnType()->isPointerTy() ||
1790 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001791 continue;
1792 setDoesNotThrow(F);
1793 setDoesNotAlias(F, 0);
1794 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001795 } else if (Name == "feof" ||
1796 Name == "free" ||
1797 Name == "fseek" ||
1798 Name == "ftell" ||
1799 Name == "fgetc" ||
1800 Name == "fseeko" ||
1801 Name == "ftello" ||
1802 Name == "fileno" ||
1803 Name == "fflush" ||
1804 Name == "fclose" ||
1805 Name == "fsetpos" ||
1806 Name == "flockfile" ||
1807 Name == "funlockfile" ||
1808 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001809 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001810 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001811 continue;
1812 setDoesNotThrow(F);
1813 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001814 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001815 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001816 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001817 continue;
1818 setDoesNotThrow(F);
1819 setDoesNotCapture(F, 1);
1820 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001821 } else if (Name == "fputc" ||
1822 Name == "fstat" ||
1823 Name == "frexp" ||
1824 Name == "frexpf" ||
1825 Name == "frexpl" ||
1826 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001827 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001828 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001829 continue;
1830 setDoesNotThrow(F);
1831 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001832 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001833 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001834 !FTy->getParamType(0)->isPointerTy() ||
1835 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001836 continue;
1837 setDoesNotThrow(F);
1838 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001839 } else if (Name == "fread" ||
1840 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001841 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001842 !FTy->getParamType(0)->isPointerTy() ||
1843 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001844 continue;
1845 setDoesNotThrow(F);
1846 setDoesNotCapture(F, 1);
1847 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001848 } else if (Name == "fputs" ||
1849 Name == "fscanf" ||
1850 Name == "fprintf" ||
1851 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001852 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001853 !FTy->getParamType(0)->isPointerTy() ||
1854 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001855 continue;
1856 setDoesNotThrow(F);
1857 setDoesNotCapture(F, 1);
1858 setDoesNotCapture(F, 2);
1859 }
1860 break;
1861 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001862 if (Name == "getc" ||
1863 Name == "getlogin_r" ||
1864 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001865 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001866 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001867 continue;
1868 setDoesNotThrow(F);
1869 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001870 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001871 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001872 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001873 continue;
1874 setDoesNotThrow(F);
1875 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001876 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001877 } else if (Name == "gets" ||
1878 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001879 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001880 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001881 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001882 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001883 continue;
1884 setDoesNotThrow(F);
1885 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001886 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001887 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001888 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001889 continue;
1890 setDoesNotThrow(F);
1891 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001892 }
1893 break;
1894 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001895 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001896 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001897 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001898 continue;
1899 setDoesNotThrow(F);
1900 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001901 } else if (Name == "uname" ||
1902 Name == "unlink" ||
1903 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001904 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001905 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001906 continue;
1907 setDoesNotThrow(F);
1908 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001909 } else if (Name == "utime" ||
1910 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001911 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001912 !FTy->getParamType(0)->isPointerTy() ||
1913 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001914 continue;
1915 setDoesNotThrow(F);
1916 setDoesNotCapture(F, 1);
1917 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001918 }
1919 break;
1920 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001921 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001922 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001923 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001924 continue;
1925 setDoesNotThrow(F);
1926 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001927 } else if (Name == "puts" ||
1928 Name == "printf" ||
1929 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001930 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001931 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001932 continue;
1933 setDoesNotThrow(F);
1934 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001935 } else if (Name == "pread" ||
1936 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001937 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001938 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001939 continue;
1940 // May throw; these are valid pthread cancellation points.
1941 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001942 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001943 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001944 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001945 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001946 !FTy->getReturnType()->isPointerTy() ||
1947 !FTy->getParamType(0)->isPointerTy() ||
1948 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001949 continue;
1950 setDoesNotThrow(F);
1951 setDoesNotAlias(F, 0);
1952 setDoesNotCapture(F, 1);
1953 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001954 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001955 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001956 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001957 continue;
1958 setDoesNotThrow(F);
1959 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001960 }
1961 break;
1962 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001963 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001964 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001965 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001966 continue;
1967 setDoesNotThrow(F);
1968 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001969 } else if (Name == "vsscanf" ||
1970 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001971 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001972 !FTy->getParamType(1)->isPointerTy() ||
1973 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001974 continue;
1975 setDoesNotThrow(F);
1976 setDoesNotCapture(F, 1);
1977 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001978 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00001979 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001980 continue;
1981 setDoesNotThrow(F);
1982 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001983 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001984 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001985 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001986 continue;
1987 setDoesNotThrow(F);
1988 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001989 } else if (Name == "vfprintf" ||
1990 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001991 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001992 !FTy->getParamType(0)->isPointerTy() ||
1993 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001994 continue;
1995 setDoesNotThrow(F);
1996 setDoesNotCapture(F, 1);
1997 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001998 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001999 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002000 !FTy->getParamType(0)->isPointerTy() ||
2001 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002002 continue;
2003 setDoesNotThrow(F);
2004 setDoesNotCapture(F, 1);
2005 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002006 }
2007 break;
2008 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002009 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002010 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002011 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002012 continue;
2013 // May throw; "open" is a valid pthread cancellation point.
2014 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002015 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002016 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002017 !FTy->getReturnType()->isPointerTy() ||
2018 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002019 continue;
2020 setDoesNotThrow(F);
2021 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002022 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002023 }
2024 break;
2025 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002026 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00002027 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002028 continue;
2029 setDoesNotThrow(F);
2030 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002031 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002032 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002033 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002034 continue;
2035 setDoesNotThrow(F);
2036 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002037 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002038 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002039 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002040 if (Name == "htonl" ||
2041 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002042 setDoesNotThrow(F);
2043 setDoesNotAccessMemory(F);
2044 }
2045 break;
2046 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002047 if (Name == "ntohl" ||
2048 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002049 setDoesNotThrow(F);
2050 setDoesNotAccessMemory(F);
2051 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002052 break;
2053 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002054 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002055 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002056 !FTy->getParamType(0)->isPointerTy() ||
2057 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002058 continue;
2059 setDoesNotThrow(F);
2060 setDoesNotCapture(F, 1);
2061 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002062 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002063 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002064 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002065 continue;
2066 setDoesNotThrow(F);
2067 setDoesNotCapture(F, 1);
2068 }
2069 break;
2070 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002071 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002072 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002073 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002074 continue;
2075 // May throw; places call through function pointer.
2076 setDoesNotCapture(F, 4);
2077 }
2078 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002079 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002080 if (Name == "__strdup" ||
2081 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002082 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002083 !FTy->getReturnType()->isPointerTy() ||
2084 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002085 continue;
2086 setDoesNotThrow(F);
2087 setDoesNotAlias(F, 0);
2088 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002089 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002090 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002091 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002092 continue;
2093 setDoesNotThrow(F);
2094 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002095 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002096 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002097 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002098 continue;
2099 setDoesNotThrow(F);
2100 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002101 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002102 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002103 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002104 continue;
2105 setDoesNotThrow(F);
2106 setDoesNotCapture(F, 2);
2107 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002108 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002109 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002110 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002111 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002112 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002113 continue;
2114 setDoesNotThrow(F);
2115 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002116 } else if (Name == "\1stat64" ||
2117 Name == "\1lstat64" ||
2118 Name == "\1statvfs64" ||
2119 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002120 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002121 !FTy->getParamType(0)->isPointerTy() ||
2122 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002123 continue;
2124 setDoesNotThrow(F);
2125 setDoesNotCapture(F, 1);
2126 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002127 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002128 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002129 !FTy->getReturnType()->isPointerTy() ||
2130 !FTy->getParamType(0)->isPointerTy() ||
2131 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002132 continue;
2133 setDoesNotThrow(F);
2134 setDoesNotAlias(F, 0);
2135 setDoesNotCapture(F, 1);
2136 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002137 } else if (Name == "\1fseeko64" ||
2138 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002139 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002140 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002141 continue;
2142 setDoesNotThrow(F);
2143 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002144 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002145 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002146 continue;
2147 setDoesNotThrow(F);
2148 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002149 } else if (Name == "\1fstat64" ||
2150 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002151 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002152 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002153 continue;
2154 setDoesNotThrow(F);
2155 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002156 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002157 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002158 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002159 continue;
2160 // May throw; "open" is a valid pthread cancellation point.
2161 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002162 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002163 break;
2164 }
2165 }
2166 return Modified;
2167}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002168
2169// TODO:
2170// Additional cases that we need to add to this file:
2171//
2172// cbrt:
2173// * cbrt(expN(X)) -> expN(x/3)
2174// * cbrt(sqrt(x)) -> pow(x,1/6)
2175// * cbrt(sqrt(x)) -> pow(x,1/9)
2176//
2177// cos, cosf, cosl:
2178// * cos(-x) -> cos(x)
2179//
2180// exp, expf, expl:
2181// * exp(log(x)) -> x
2182//
2183// log, logf, logl:
2184// * log(exp(x)) -> x
2185// * log(x**y) -> y*log(x)
2186// * log(exp(y)) -> y*log(e)
2187// * log(exp2(y)) -> y*log(2)
2188// * log(exp10(y)) -> y*log(10)
2189// * log(sqrt(x)) -> 0.5*log(x)
2190// * log(pow(x,y)) -> y*log(x)
2191//
2192// lround, lroundf, lroundl:
2193// * lround(cnst) -> cnst'
2194//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002195// pow, powf, powl:
2196// * pow(exp(x),y) -> exp(x*y)
2197// * pow(sqrt(x),y) -> pow(x,y*0.5)
2198// * pow(pow(x,y),z)-> pow(x,y*z)
2199//
2200// puts:
Dan Gohman2511bd02010-08-04 01:16:35 +00002201// * puts("") -> putchar('\n')
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002202//
2203// round, roundf, roundl:
2204// * round(cnst) -> cnst'
2205//
2206// signbit:
2207// * signbit(cnst) -> cnst'
2208// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2209//
2210// sqrt, sqrtf, sqrtl:
2211// * sqrt(expN(x)) -> expN(x*0.5)
2212// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2213// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2214//
2215// stpcpy:
2216// * stpcpy(str, "literal") ->
2217// llvm.memcpy(str,"literal",strlen("literal")+1,1)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002218//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002219// strpbrk:
2220// * strpbrk(s,a) -> offset_in_for(s,a)
2221// (if s and a are both constant strings)
2222// * strpbrk(s,"") -> 0
2223// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2224//
2225// strspn, strcspn:
2226// * strspn(s,a) -> const_int (if both args are constant)
2227// * strspn("",a) -> 0
2228// * strspn(s,"") -> 0
2229// * strcspn(s,a) -> const_int (if both args are constant)
2230// * strcspn("",a) -> 0
2231// * strcspn(s,"") -> strlen(a)
2232//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002233// tan, tanf, tanl:
2234// * tan(atan(x)) -> x
2235//
2236// trunc, truncf, truncl:
2237// * trunc(cnst) -> cnst'
2238//
2239//