blob: 75754a558541fa5e6bdf4ebf60b735cbbbba05dc [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';
Eric Christopher37c8b862009-10-07 21:14:25 +0000255
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000256 // Compute the offset.
Benjamin Kramere2609902010-09-29 22:29:12 +0000257 size_t I = Str.find(CharC->getSExtValue());
258 if (I == std::string::npos) // Didn't find the char. strchr returns null.
259 return Constant::getNullValue(CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000260
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000261 // strchr(s+n,c) -> gep(s+n+i,c)
Benjamin Kramere2609902010-09-29 22:29:12 +0000262 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000263 return B.CreateGEP(SrcStr, Idx, "strchr");
264 }
265};
266
267//===---------------------------------------===//
Benjamin Kramer06f25cf2010-09-29 21:50:51 +0000268// 'strrchr' Optimizations
269
270struct StrRChrOpt : public LibCallOptimization {
271 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
272 // Verify the "strrchr" function prototype.
273 const FunctionType *FT = Callee->getFunctionType();
274 if (FT->getNumParams() != 2 ||
275 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
276 FT->getParamType(0) != FT->getReturnType())
277 return 0;
278
279 Value *SrcStr = CI->getArgOperand(0);
280 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
281
282 // Cannot fold anything if we're not looking for a constant.
283 if (!CharC)
284 return 0;
285
286 std::string Str;
287 if (!GetConstantStringInfo(SrcStr, Str)) {
288 // strrchr(s, 0) -> strchr(s, 0)
289 if (TD && CharC->isZero())
290 return EmitStrChr(SrcStr, '\0', B, TD);
291 return 0;
292 }
293
294 // strrchr can find the nul character.
295 Str += '\0';
296
297 // Compute the offset.
298 size_t I = Str.rfind(CharC->getSExtValue());
299 if (I == std::string::npos) // Didn't find the char. Return null.
300 return Constant::getNullValue(CI->getType());
301
302 // strrchr(s+n,c) -> gep(s+n+i,c)
303 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
304 return B.CreateGEP(SrcStr, Idx, "strrchr");
305 }
306};
307
308//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000309// 'strcmp' Optimizations
310
Chris Lattner3e8b6632009-09-02 06:11:42 +0000311struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000312 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000313 // Verify the "strcmp" function prototype.
314 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000315 if (FT->getNumParams() != 2 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000316 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000317 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000318 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000319 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000320
Gabor Greifaee5dc12010-06-24 10:42:46 +0000321 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000322 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000323 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000324
Bill Wendling0582ae92009-03-13 04:39:26 +0000325 std::string Str1, Str2;
326 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
327 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000328
Bill Wendling0582ae92009-03-13 04:39:26 +0000329 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000330 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000331
Bill Wendling0582ae92009-03-13 04:39:26 +0000332 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000333 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000334
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000335 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000336 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000337 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000338 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000339
340 // strcmp(P, "x") -> memcmp(P, "x", 2)
341 uint64_t Len1 = GetStringLength(Str1P);
342 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000343 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000344 // These optimizations require TargetData.
345 if (!TD) return 0;
346
Nick Lewycky13a09e22008-12-21 00:19:21 +0000347 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000348 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000349 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000350 }
351
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000352 return 0;
353 }
354};
355
356//===---------------------------------------===//
357// 'strncmp' Optimizations
358
Chris Lattner3e8b6632009-09-02 06:11:42 +0000359struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000360 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000361 // Verify the "strncmp" function prototype.
362 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000363 if (FT->getNumParams() != 3 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000364 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000365 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000366 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000367 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000368 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000369
Gabor Greifaee5dc12010-06-24 10:42:46 +0000370 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000371 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000372 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000373
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000374 // Get the length argument if it is constant.
375 uint64_t Length;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000376 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000377 Length = LengthArg->getZExtValue();
378 else
379 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000380
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000381 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000382 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000383
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000384 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000385 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD);
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000386
Bill Wendling0582ae92009-03-13 04:39:26 +0000387 std::string Str1, Str2;
388 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
389 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000390
Bill Wendling0582ae92009-03-13 04:39:26 +0000391 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000392 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000393
Bill Wendling0582ae92009-03-13 04:39:26 +0000394 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000395 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000396
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000397 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000398 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000399 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000400 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000401 return 0;
402 }
403};
404
405
406//===---------------------------------------===//
407// 'strcpy' Optimizations
408
Chris Lattner3e8b6632009-09-02 06:11:42 +0000409struct StrCpyOpt : public LibCallOptimization {
Evan Chengeb8c6452010-03-24 20:19:04 +0000410 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
411
412 StrCpyOpt(bool c) : OptChkCall(c) {}
413
Eric Christopher7a61d702008-08-08 19:39:37 +0000414 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000415 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000416 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000417 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000418 if (FT->getNumParams() != NumParams ||
419 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000420 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000421 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000422 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000423
Gabor Greifaee5dc12010-06-24 10:42:46 +0000424 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000425 if (Dst == Src) // strcpy(x,x) -> x
426 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000427
Dan Gohmanf14d9192009-08-18 00:48:13 +0000428 // These optimizations require TargetData.
429 if (!TD) return 0;
430
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000431 // See if we can get the length of the input string.
432 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000433 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000434
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000435 // We have enough information to now generate the memcpy call to do the
436 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000437 if (OptChkCall)
438 EmitMemCpyChk(Dst, Src,
439 ConstantInt::get(TD->getIntPtrType(*Context), Len),
Gabor Greifaee5dc12010-06-24 10:42:46 +0000440 CI->getArgOperand(2), B, TD);
Evan Cheng0289b412010-03-23 15:48:04 +0000441 else
442 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000443 ConstantInt::get(TD->getIntPtrType(*Context), Len),
444 1, false, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000445 return Dst;
446 }
447};
448
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000449//===---------------------------------------===//
450// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000451
Chris Lattner3e8b6632009-09-02 06:11:42 +0000452struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000453 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
454 const FunctionType *FT = Callee->getFunctionType();
455 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
456 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000457 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000458 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000459 return 0;
460
Gabor Greifaee5dc12010-06-24 10:42:46 +0000461 Value *Dst = CI->getArgOperand(0);
462 Value *Src = CI->getArgOperand(1);
463 Value *LenOp = CI->getArgOperand(2);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000464
465 // See if we can get the length of the input string.
466 uint64_t SrcLen = GetStringLength(Src);
467 if (SrcLen == 0) return 0;
468 --SrcLen;
469
470 if (SrcLen == 0) {
471 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Mon P Wang20adc9d2010-04-04 03:10:48 +0000472 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'),
473 LenOp, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000474 return Dst;
475 }
476
477 uint64_t Len;
478 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
479 Len = LengthArg->getZExtValue();
480 else
481 return 0;
482
483 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
484
Dan Gohmanf14d9192009-08-18 00:48:13 +0000485 // These optimizations require TargetData.
486 if (!TD) return 0;
487
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000488 // Let strncpy handle the zero padding
489 if (Len > SrcLen+1) return 0;
490
491 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000492 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000493 ConstantInt::get(TD->getIntPtrType(*Context), Len),
494 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000495
496 return Dst;
497 }
498};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000499
500//===---------------------------------------===//
501// 'strlen' Optimizations
502
Chris Lattner3e8b6632009-09-02 06:11:42 +0000503struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000504 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000505 const FunctionType *FT = Callee->getFunctionType();
506 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000507 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000508 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000509 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000510
Gabor Greifaee5dc12010-06-24 10:42:46 +0000511 Value *Src = CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000512
513 // Constant folding: strlen("xyz") -> 3
514 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000515 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000516
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000517 // strlen(x) != 0 --> *x != 0
518 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000519 if (IsOnlyUsedInZeroEqualityComparison(CI))
520 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
521 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000522 }
523};
524
525//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000526// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000527
Chris Lattner3e8b6632009-09-02 06:11:42 +0000528struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000529 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
530 const FunctionType *FT = Callee->getFunctionType();
531 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000532 !FT->getParamType(0)->isPointerTy() ||
533 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000534 return 0;
535
Gabor Greifaee5dc12010-06-24 10:42:46 +0000536 Value *EndPtr = CI->getArgOperand(1);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000537 if (isa<ConstantPointerNull>(EndPtr)) {
538 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000539 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000540 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000541
542 return 0;
543 }
544};
545
Chris Lattner24604112009-12-16 09:32:05 +0000546//===---------------------------------------===//
547// 'strstr' Optimizations
548
549struct StrStrOpt : public LibCallOptimization {
550 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
551 const FunctionType *FT = Callee->getFunctionType();
552 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000553 !FT->getParamType(0)->isPointerTy() ||
554 !FT->getParamType(1)->isPointerTy() ||
555 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000556 return 0;
557
558 // fold strstr(x, x) -> x.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000559 if (CI->getArgOperand(0) == CI->getArgOperand(1))
560 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000561
Benjamin Kramer386e9182010-06-15 21:34:25 +0000562 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000563 if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
564 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD);
565 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000566 StrLen, B, TD);
567 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
568 UI != UE; ) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000569 ICmpInst *Old = cast<ICmpInst>(*UI++);
Benjamin Kramer386e9182010-06-15 21:34:25 +0000570 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
571 ConstantInt::getNullValue(StrNCmp->getType()),
572 "cmp");
573 Old->replaceAllUsesWith(Cmp);
574 Old->eraseFromParent();
575 }
576 return CI;
577 }
578
Chris Lattner24604112009-12-16 09:32:05 +0000579 // See if either input string is a constant string.
580 std::string SearchStr, ToFindStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000581 bool HasStr1 = GetConstantStringInfo(CI->getArgOperand(0), SearchStr);
582 bool HasStr2 = GetConstantStringInfo(CI->getArgOperand(1), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000583
Chris Lattner24604112009-12-16 09:32:05 +0000584 // fold strstr(x, "") -> x.
585 if (HasStr2 && ToFindStr.empty())
Gabor Greifaee5dc12010-06-24 10:42:46 +0000586 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000587
Chris Lattner24604112009-12-16 09:32:05 +0000588 // If both strings are known, constant fold it.
589 if (HasStr1 && HasStr2) {
590 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000591
Chris Lattner24604112009-12-16 09:32:05 +0000592 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
593 return Constant::getNullValue(CI->getType());
594
595 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000596 Value *Result = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner24604112009-12-16 09:32:05 +0000597 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
598 return B.CreateBitCast(Result, CI->getType());
599 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000600
Chris Lattner24604112009-12-16 09:32:05 +0000601 // fold strstr(x, "y") -> strchr(x, 'y').
602 if (HasStr2 && ToFindStr.size() == 1)
Gabor Greifa3997812010-07-22 10:37:47 +0000603 return B.CreateBitCast(EmitStrChr(CI->getArgOperand(0),
604 ToFindStr[0], B, TD), CI->getType());
Chris Lattner24604112009-12-16 09:32:05 +0000605 return 0;
606 }
607};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000608
Nick Lewycky4c498412009-02-13 15:31:46 +0000609
610//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000611// 'memcmp' Optimizations
612
Chris Lattner3e8b6632009-09-02 06:11:42 +0000613struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000614 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000615 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000616 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
617 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000618 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000619 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000620
Gabor Greifaee5dc12010-06-24 10:42:46 +0000621 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000622
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000623 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000624 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000625
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000626 // Make sure we have a constant length.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000627 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000628 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000629 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000630
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000631 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000632 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000633
Benjamin Kramer48aefe12010-05-25 22:53:43 +0000634 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
635 if (Len == 1) {
636 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
637 CI->getType(), "lhsv");
638 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
639 CI->getType(), "rhsv");
Benjamin Kramer1464c1d2010-05-26 09:45:04 +0000640 return B.CreateSub(LHSV, RHSV, "chardiff");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000641 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000642
Benjamin Kramer992a6372009-11-05 17:44:22 +0000643 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
644 std::string LHSStr, RHSStr;
645 if (GetConstantStringInfo(LHS, LHSStr) &&
646 GetConstantStringInfo(RHS, RHSStr)) {
647 // Make sure we're not reading out-of-bounds memory.
648 if (Len > LHSStr.length() || Len > RHSStr.length())
649 return 0;
650 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
651 return ConstantInt::get(CI->getType(), Ret);
652 }
653
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000654 return 0;
655 }
656};
657
658//===---------------------------------------===//
659// 'memcpy' Optimizations
660
Chris Lattner3e8b6632009-09-02 06:11:42 +0000661struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000662 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000663 // These optimizations require TargetData.
664 if (!TD) return 0;
665
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000666 const FunctionType *FT = Callee->getFunctionType();
667 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000668 !FT->getParamType(0)->isPointerTy() ||
669 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000670 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000671 return 0;
672
673 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000674 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
675 CI->getArgOperand(2), 1, false, B, TD);
676 return CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000677 }
678};
679
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000680//===---------------------------------------===//
681// 'memmove' Optimizations
682
Chris Lattner3e8b6632009-09-02 06:11:42 +0000683struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000684 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000685 // These optimizations require TargetData.
686 if (!TD) return 0;
687
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000688 const FunctionType *FT = Callee->getFunctionType();
689 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000690 !FT->getParamType(0)->isPointerTy() ||
691 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000692 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000693 return 0;
694
695 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000696 EmitMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
697 CI->getArgOperand(2), 1, false, B, TD);
698 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000699 }
700};
701
702//===---------------------------------------===//
703// 'memset' Optimizations
704
Chris Lattner3e8b6632009-09-02 06:11:42 +0000705struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000706 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000707 // These optimizations require TargetData.
708 if (!TD) return 0;
709
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000710 const FunctionType *FT = Callee->getFunctionType();
711 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000712 !FT->getParamType(0)->isPointerTy() ||
713 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000714 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000715 return 0;
716
717 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Gabor Greifa3997812010-07-22 10:37:47 +0000718 Value *Val = B.CreateIntCast(CI->getArgOperand(1),
719 Type::getInt8Ty(*Context), false);
Gabor Greifaee5dc12010-06-24 10:42:46 +0000720 EmitMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), false, B, TD);
721 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000722 }
723};
724
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000725//===----------------------------------------------------------------------===//
726// Math Library Optimizations
727//===----------------------------------------------------------------------===//
728
729//===---------------------------------------===//
730// 'pow*' Optimizations
731
Chris Lattner3e8b6632009-09-02 06:11:42 +0000732struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000733 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000734 const FunctionType *FT = Callee->getFunctionType();
735 // Just make sure this has 2 arguments of the same FP type, which match the
736 // result type.
737 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
738 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000739 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000740 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000741
Gabor Greifaee5dc12010-06-24 10:42:46 +0000742 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000743 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
744 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
745 return Op1C;
746 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000747 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000748 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000749
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000750 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
751 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000752
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000753 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000754 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000755
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000756 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000757 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
758 // This is faster than calling pow, and still handles negative zero
759 // and negative infinite correctly.
760 // TODO: In fast-math mode, this could be just sqrt(x).
761 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000762 Value *Inf = ConstantFP::getInfinity(CI->getType());
763 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000764 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
765 Callee->getAttributes());
766 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
767 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000768 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
769 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
770 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000771 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000772
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000773 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
774 return Op1;
775 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000776 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000777 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000778 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000779 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000780 return 0;
781 }
782};
783
784//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000785// 'exp2' Optimizations
786
Chris Lattner3e8b6632009-09-02 06:11:42 +0000787struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000788 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000789 const FunctionType *FT = Callee->getFunctionType();
790 // Just make sure this has 1 argument of FP type, which matches the
791 // result type.
792 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000793 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000794 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000795
Gabor Greifaee5dc12010-06-24 10:42:46 +0000796 Value *Op = CI->getArgOperand(0);
Chris Lattnere818f772008-05-02 18:43:35 +0000797 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
798 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
799 Value *LdExpArg = 0;
800 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
801 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000802 LdExpArg = B.CreateSExt(OpC->getOperand(0),
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000803 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000804 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
805 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000806 LdExpArg = B.CreateZExt(OpC->getOperand(0),
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000807 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000808 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000809
Chris Lattnere818f772008-05-02 18:43:35 +0000810 if (LdExpArg) {
811 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000812 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000813 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000814 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000815 Name = "ldexp";
816 else
817 Name = "ldexpl";
818
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000819 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000820 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000821 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000822
823 Module *M = Caller->getParent();
824 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000825 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000826 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000827 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
828 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
829 CI->setCallingConv(F->getCallingConv());
830
831 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000832 }
833 return 0;
834 }
835};
Chris Lattnere818f772008-05-02 18:43:35 +0000836
837//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000838// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
839
Chris Lattner3e8b6632009-09-02 06:11:42 +0000840struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000841 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000842 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000843 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
844 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000845 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000846
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000847 // If this is something like 'floor((double)floatval)', convert to floorf.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000848 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000849 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000850 return 0;
851
852 // floor((double)floatval) -> (double)floorf(floatval)
853 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000854 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
855 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000856 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000857 }
858};
859
860//===----------------------------------------------------------------------===//
861// Integer Optimizations
862//===----------------------------------------------------------------------===//
863
864//===---------------------------------------===//
865// 'ffs*' Optimizations
866
Chris Lattner3e8b6632009-09-02 06:11:42 +0000867struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000868 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000869 const FunctionType *FT = Callee->getFunctionType();
870 // Just make sure this has 2 arguments of the same FP type, which match the
871 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000872 if (FT->getNumParams() != 1 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000873 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000874 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000875 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000876
Gabor Greifaee5dc12010-06-24 10:42:46 +0000877 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000878
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000879 // Constant fold.
880 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
881 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000882 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000883 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000884 CI->getValue().countTrailingZeros()+1);
885 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000886
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000887 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
888 const Type *ArgType = Op->getType();
889 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
890 Intrinsic::cttz, &ArgType, 1);
891 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000892 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000893 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000894
Owen Andersona7235ea2009-07-31 20:28:14 +0000895 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000896 return B.CreateSelect(Cond, V,
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000897 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000898 }
899};
900
901//===---------------------------------------===//
902// 'isdigit' Optimizations
903
Chris Lattner3e8b6632009-09-02 06:11:42 +0000904struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000905 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000906 const FunctionType *FT = Callee->getFunctionType();
907 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000908 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000909 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000910 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000911
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000912 // isdigit(c) -> (c-'0') <u 10
Gabor Greifaee5dc12010-06-24 10:42:46 +0000913 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000914 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000915 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000916 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000917 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000918 return B.CreateZExt(Op, CI->getType());
919 }
920};
921
922//===---------------------------------------===//
923// 'isascii' Optimizations
924
Chris Lattner3e8b6632009-09-02 06:11:42 +0000925struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000926 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000927 const FunctionType *FT = Callee->getFunctionType();
928 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000929 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000930 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000931 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000932
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000933 // isascii(c) -> c <u 128
Gabor Greifaee5dc12010-06-24 10:42:46 +0000934 Value *Op = CI->getArgOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +0000935 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000936 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000937 return B.CreateZExt(Op, CI->getType());
938 }
939};
Eric Christopher37c8b862009-10-07 21:14:25 +0000940
Chris Lattner313f0e62008-06-09 08:26:51 +0000941//===---------------------------------------===//
942// 'abs', 'labs', 'llabs' Optimizations
943
Chris Lattner3e8b6632009-09-02 06:11:42 +0000944struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000945 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +0000946 const FunctionType *FT = Callee->getFunctionType();
947 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +0000948 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +0000949 FT->getParamType(0) != FT->getReturnType())
950 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000951
Chris Lattner313f0e62008-06-09 08:26:51 +0000952 // abs(x) -> x >s -1 ? x : -x
Gabor Greifaee5dc12010-06-24 10:42:46 +0000953 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000954 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +0000955 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +0000956 "ispos");
957 Value *Neg = B.CreateNeg(Op, "neg");
958 return B.CreateSelect(Pos, Op, Neg);
959 }
960};
Eric Christopher37c8b862009-10-07 21:14:25 +0000961
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000962
963//===---------------------------------------===//
964// 'toascii' Optimizations
965
Chris Lattner3e8b6632009-09-02 06:11:42 +0000966struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000967 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000968 const FunctionType *FT = Callee->getFunctionType();
969 // We require i32(i32)
970 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000971 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000972 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000973
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000974 // isascii(c) -> c & 0x7f
Gabor Greifaee5dc12010-06-24 10:42:46 +0000975 return B.CreateAnd(CI->getArgOperand(0),
Owen Andersoneed707b2009-07-24 23:12:02 +0000976 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000977 }
978};
979
980//===----------------------------------------------------------------------===//
981// Formatting and IO Optimizations
982//===----------------------------------------------------------------------===//
983
984//===---------------------------------------===//
985// 'printf' Optimizations
986
Chris Lattner3e8b6632009-09-02 06:11:42 +0000987struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000988 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000989 // Require one fixed pointer argument and an integer/void result.
990 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000991 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
992 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000993 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000994 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000995
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000996 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000997 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000998 if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +0000999 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001000
1001 // Empty format string -> noop.
1002 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001003 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001004 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001005
Chris Lattner74965f22009-11-09 04:57:04 +00001006 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1007 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001008 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001009 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +00001010 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +00001011 if (CI->use_empty()) return CI;
1012 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001013 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001014
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001015 // printf("foo\n") --> puts("foo")
1016 if (FormatStr[FormatStr.size()-1] == '\n' &&
1017 FormatStr.find('%') == std::string::npos) { // no format characters.
1018 // Create a string literal with no \n on it. We expect the constant merge
1019 // pass to be run after this pass, to merge duplicate strings.
1020 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001021 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001022 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1023 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +00001024 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001025 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001026 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001027 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001028
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001029 // Optimize specific format strings.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001030 // printf("%c", chr) --> putchar(chr)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001031 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001032 CI->getArgOperand(1)->getType()->isIntegerTy()) {
1033 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001034
Chris Lattner74965f22009-11-09 04:57:04 +00001035 if (CI->use_empty()) return CI;
1036 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001037 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001038
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001039 // printf("%s\n", str) --> puts(str)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001040 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001041 CI->getArgOperand(1)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001042 CI->use_empty()) {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001043 EmitPutS(CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001044 return CI;
1045 }
1046 return 0;
1047 }
1048};
1049
1050//===---------------------------------------===//
1051// 'sprintf' Optimizations
1052
Chris Lattner3e8b6632009-09-02 06:11:42 +00001053struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001054 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001055 // Require two fixed pointer arguments and an integer result.
1056 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001057 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1058 !FT->getParamType(1)->isPointerTy() ||
1059 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001060 return 0;
1061
1062 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001063 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001064 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001065 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001066
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001067 // If we just have a format string (nothing else crazy) transform it.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001068 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001069 // Make sure there's no % in the constant array. We could try to handle
1070 // %% -> % in the future if we cared.
1071 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1072 if (FormatStr[i] == '%')
1073 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001074
1075 // These optimizations require TargetData.
1076 if (!TD) return 0;
1077
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001078 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
Gabor Greifa3997812010-07-22 10:37:47 +00001079 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), // Copy the
1080 ConstantInt::get(TD->getIntPtrType(*Context), // nul byte.
1081 FormatStr.size() + 1), 1, false, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001082 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001083 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001084
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001085 // The remaining optimizations require the format string to be "%s" or "%c"
1086 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001087 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1088 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001089 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001090
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001091 // Decode the second character of the format string.
1092 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001093 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Gabor Greifaee5dc12010-06-24 10:42:46 +00001094 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1095 Value *V = B.CreateTrunc(CI->getArgOperand(2),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001096 Type::getInt8Ty(*Context), "char");
Gabor Greifaee5dc12010-06-24 10:42:46 +00001097 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001098 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001099 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001100 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001101 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001102
Owen Andersoneed707b2009-07-24 23:12:02 +00001103 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001104 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001105
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001106 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001107 // These optimizations require TargetData.
1108 if (!TD) return 0;
1109
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001110 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001111 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001112
Gabor Greifaee5dc12010-06-24 10:42:46 +00001113 Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001114 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001115 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001116 "leninc");
Gabor Greifa3997812010-07-22 10:37:47 +00001117 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(2),
1118 IncLen, 1, false, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001119
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001120 // The sprintf result is the unincremented number of bytes in the string.
1121 return B.CreateIntCast(Len, CI->getType(), false);
1122 }
1123 return 0;
1124 }
1125};
1126
1127//===---------------------------------------===//
1128// 'fwrite' Optimizations
1129
Chris Lattner3e8b6632009-09-02 06:11:42 +00001130struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001131 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001132 // Require a pointer, an integer, an integer, a pointer, returning integer.
1133 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001134 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1135 !FT->getParamType(1)->isIntegerTy() ||
1136 !FT->getParamType(2)->isIntegerTy() ||
1137 !FT->getParamType(3)->isPointerTy() ||
1138 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001139 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001140
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001141 // Get the element size and count.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001142 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1143 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001144 if (!SizeC || !CountC) return 0;
1145 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001146
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001147 // If this is writing zero records, remove the call (it's a noop).
1148 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001149 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001150
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001151 // If this is writing one byte, turn it into fputc.
1152 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001153 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1154 EmitFPutC(Char, CI->getArgOperand(3), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001155 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001156 }
1157
1158 return 0;
1159 }
1160};
1161
1162//===---------------------------------------===//
1163// 'fputs' Optimizations
1164
Chris Lattner3e8b6632009-09-02 06:11:42 +00001165struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001166 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001167 // These optimizations require TargetData.
1168 if (!TD) return 0;
1169
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001170 // Require two pointers. Also, we can't optimize if return value is used.
1171 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001172 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1173 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001174 !CI->use_empty())
1175 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001176
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001177 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001178 uint64_t Len = GetStringLength(CI->getArgOperand(0));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001179 if (!Len) return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001180 EmitFWrite(CI->getArgOperand(0),
Owen Anderson1d0be152009-08-13 21:58:54 +00001181 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001182 CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001183 return CI; // Known to have no uses (see above).
1184 }
1185};
1186
1187//===---------------------------------------===//
1188// 'fprintf' Optimizations
1189
Chris Lattner3e8b6632009-09-02 06:11:42 +00001190struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001191 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001192 // Require two fixed paramters as pointers and integer result.
1193 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001194 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1195 !FT->getParamType(1)->isPointerTy() ||
1196 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001197 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001198
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001199 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001200 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001201 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001202 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001203
1204 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001205 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001206 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1207 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001208 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001209
1210 // These optimizations require TargetData.
1211 if (!TD) return 0;
1212
Gabor Greifaee5dc12010-06-24 10:42:46 +00001213 EmitFWrite(CI->getArgOperand(1),
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001214 ConstantInt::get(TD->getIntPtrType(*Context),
1215 FormatStr.size()),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001216 CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001217 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001218 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001219
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001220 // The remaining optimizations require the format string to be "%s" or "%c"
1221 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001222 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1223 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001224 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001225
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001226 // Decode the second character of the format string.
1227 if (FormatStr[1] == 'c') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001228 // fprintf(F, "%c", chr) --> fputc(chr, F)
1229 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1230 EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001231 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001232 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001233
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001234 if (FormatStr[1] == 's') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001235 // fprintf(F, "%s", str) --> fputs(str, F)
1236 if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001237 return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001238 EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001239 return CI;
1240 }
1241 return 0;
1242 }
1243};
1244
Bill Wendlingac178222008-05-05 21:37:59 +00001245} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001246
1247//===----------------------------------------------------------------------===//
1248// SimplifyLibCalls Pass Implementation
1249//===----------------------------------------------------------------------===//
1250
1251namespace {
1252 /// This pass optimizes well known library functions from libc and libm.
1253 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001254 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001255 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001256 // String and Memory LibCall Optimizations
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001257 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrRChrOpt StrRChr;
1258 StrCmpOpt StrCmp; StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
Evan Cheng0289b412010-03-23 15:48:04 +00001259 StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001260 StrToOpt StrTo; StrStrOpt StrStr;
1261 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001262 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001263 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001264 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001265 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1266 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001267 // Formatting and IO Optimizations
1268 SPrintFOpt SPrintF; PrintFOpt PrintF;
1269 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001270
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001271 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001272 public:
1273 static char ID; // Pass identification
Owen Anderson90c579d2010-08-06 18:33:48 +00001274 SimplifyLibCalls() : FunctionPass(ID), StrCpy(false), StrCpyChk(true) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001275 void InitOptimizations();
1276 bool runOnFunction(Function &F);
1277
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001278 void setDoesNotAccessMemory(Function &F);
1279 void setOnlyReadsMemory(Function &F);
1280 void setDoesNotThrow(Function &F);
1281 void setDoesNotCapture(Function &F, unsigned n);
1282 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001283 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001284
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001285 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001286 }
1287 };
1288 char SimplifyLibCalls::ID = 0;
1289} // end anonymous namespace.
1290
Owen Andersond13db2c2010-07-21 22:09:45 +00001291INITIALIZE_PASS(SimplifyLibCalls, "simplify-libcalls",
1292 "Simplify well-known library calls", false, false);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001293
1294// Public interface to the Simplify LibCalls pass.
1295FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001296 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001297}
1298
1299/// Optimizations - Populate the Optimizations map with all the optimizations
1300/// we know.
1301void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001302 // String and Memory LibCall Optimizations
1303 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001304 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001305 Optimizations["strchr"] = &StrChr;
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001306 Optimizations["strrchr"] = &StrRChr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001307 Optimizations["strcmp"] = &StrCmp;
1308 Optimizations["strncmp"] = &StrNCmp;
1309 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001310 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001311 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001312 Optimizations["strtol"] = &StrTo;
1313 Optimizations["strtod"] = &StrTo;
1314 Optimizations["strtof"] = &StrTo;
1315 Optimizations["strtoul"] = &StrTo;
1316 Optimizations["strtoll"] = &StrTo;
1317 Optimizations["strtold"] = &StrTo;
1318 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001319 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001320 Optimizations["memcmp"] = &MemCmp;
1321 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001322 Optimizations["memmove"] = &MemMove;
1323 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001324
Evan Cheng0289b412010-03-23 15:48:04 +00001325 // _chk variants of String and Memory LibCall Optimizations.
Evan Cheng0289b412010-03-23 15:48:04 +00001326 Optimizations["__strcpy_chk"] = &StrCpyChk;
1327
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001328 // Math Library Optimizations
1329 Optimizations["powf"] = &Pow;
1330 Optimizations["pow"] = &Pow;
1331 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001332 Optimizations["llvm.pow.f32"] = &Pow;
1333 Optimizations["llvm.pow.f64"] = &Pow;
1334 Optimizations["llvm.pow.f80"] = &Pow;
1335 Optimizations["llvm.pow.f128"] = &Pow;
1336 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001337 Optimizations["exp2l"] = &Exp2;
1338 Optimizations["exp2"] = &Exp2;
1339 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001340 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1341 Optimizations["llvm.exp2.f128"] = &Exp2;
1342 Optimizations["llvm.exp2.f80"] = &Exp2;
1343 Optimizations["llvm.exp2.f64"] = &Exp2;
1344 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001345
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001346#ifdef HAVE_FLOORF
1347 Optimizations["floor"] = &UnaryDoubleFP;
1348#endif
1349#ifdef HAVE_CEILF
1350 Optimizations["ceil"] = &UnaryDoubleFP;
1351#endif
1352#ifdef HAVE_ROUNDF
1353 Optimizations["round"] = &UnaryDoubleFP;
1354#endif
1355#ifdef HAVE_RINTF
1356 Optimizations["rint"] = &UnaryDoubleFP;
1357#endif
1358#ifdef HAVE_NEARBYINTF
1359 Optimizations["nearbyint"] = &UnaryDoubleFP;
1360#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001361
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001362 // Integer Optimizations
1363 Optimizations["ffs"] = &FFS;
1364 Optimizations["ffsl"] = &FFS;
1365 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001366 Optimizations["abs"] = &Abs;
1367 Optimizations["labs"] = &Abs;
1368 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001369 Optimizations["isdigit"] = &IsDigit;
1370 Optimizations["isascii"] = &IsAscii;
1371 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001372
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001373 // Formatting and IO Optimizations
1374 Optimizations["sprintf"] = &SPrintF;
1375 Optimizations["printf"] = &PrintF;
1376 Optimizations["fwrite"] = &FWrite;
1377 Optimizations["fputs"] = &FPuts;
1378 Optimizations["fprintf"] = &FPrintF;
1379}
1380
1381
1382/// runOnFunction - Top level algorithm.
1383///
1384bool SimplifyLibCalls::runOnFunction(Function &F) {
1385 if (Optimizations.empty())
1386 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001387
Dan Gohmanf14d9192009-08-18 00:48:13 +00001388 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001389
Owen Andersone922c022009-07-22 00:24:57 +00001390 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001391
1392 bool Changed = false;
1393 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1394 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1395 // Ignore non-calls.
1396 CallInst *CI = dyn_cast<CallInst>(I++);
1397 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001398
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001399 // Ignore indirect calls and calls to non-external functions.
1400 Function *Callee = CI->getCalledFunction();
1401 if (Callee == 0 || !Callee->isDeclaration() ||
1402 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1403 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001404
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001405 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001406 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1407 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001408
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001409 // Set the builder to the instruction after the call.
1410 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001411
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001412 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001413 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001414 if (Result == 0) continue;
1415
David Greene6a6b90e2010-01-05 01:27:21 +00001416 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1417 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001418
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001419 // Something changed!
1420 Changed = true;
1421 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001422
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001423 // Inspect the instruction after the call (which was potentially just
1424 // added) next.
1425 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001426
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001427 if (CI != Result && !CI->use_empty()) {
1428 CI->replaceAllUsesWith(Result);
1429 if (!Result->hasName())
1430 Result->takeName(CI);
1431 }
1432 CI->eraseFromParent();
1433 }
1434 }
1435 return Changed;
1436}
1437
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001438// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001439
1440void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1441 if (!F.doesNotAccessMemory()) {
1442 F.setDoesNotAccessMemory();
1443 ++NumAnnotated;
1444 Modified = true;
1445 }
1446}
1447void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1448 if (!F.onlyReadsMemory()) {
1449 F.setOnlyReadsMemory();
1450 ++NumAnnotated;
1451 Modified = true;
1452 }
1453}
1454void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1455 if (!F.doesNotThrow()) {
1456 F.setDoesNotThrow();
1457 ++NumAnnotated;
1458 Modified = true;
1459 }
1460}
1461void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1462 if (!F.doesNotCapture(n)) {
1463 F.setDoesNotCapture(n);
1464 ++NumAnnotated;
1465 Modified = true;
1466 }
1467}
1468void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1469 if (!F.doesNotAlias(n)) {
1470 F.setDoesNotAlias(n);
1471 ++NumAnnotated;
1472 Modified = true;
1473 }
1474}
1475
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001476/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001477///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001478bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001479 Modified = false;
1480 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1481 Function &F = *I;
1482 if (!F.isDeclaration())
1483 continue;
1484
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001485 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001486 continue;
1487
1488 const FunctionType *FTy = F.getFunctionType();
1489
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001490 StringRef Name = F.getName();
1491 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001492 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001493 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001494 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001495 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001496 continue;
1497 setOnlyReadsMemory(F);
1498 setDoesNotThrow(F);
1499 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001500 } else if (Name == "strchr" ||
1501 Name == "strrchr") {
1502 if (FTy->getNumParams() != 2 ||
1503 !FTy->getParamType(0)->isPointerTy() ||
1504 !FTy->getParamType(1)->isIntegerTy())
1505 continue;
1506 setOnlyReadsMemory(F);
1507 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001508 } else if (Name == "strcpy" ||
1509 Name == "stpcpy" ||
1510 Name == "strcat" ||
1511 Name == "strtol" ||
1512 Name == "strtod" ||
1513 Name == "strtof" ||
1514 Name == "strtoul" ||
1515 Name == "strtoll" ||
1516 Name == "strtold" ||
1517 Name == "strncat" ||
1518 Name == "strncpy" ||
1519 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001520 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001521 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001522 continue;
1523 setDoesNotThrow(F);
1524 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001525 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001526 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001527 !FTy->getParamType(0)->isPointerTy() ||
1528 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001529 continue;
1530 setDoesNotThrow(F);
1531 setDoesNotCapture(F, 1);
1532 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001533 } else if (Name == "strcmp" ||
1534 Name == "strspn" ||
1535 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001536 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001537 Name == "strcoll" ||
1538 Name == "strcasecmp" ||
1539 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001540 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001541 !FTy->getParamType(0)->isPointerTy() ||
1542 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001543 continue;
1544 setOnlyReadsMemory(F);
1545 setDoesNotThrow(F);
1546 setDoesNotCapture(F, 1);
1547 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001548 } else if (Name == "strstr" ||
1549 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001550 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001551 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001552 continue;
1553 setOnlyReadsMemory(F);
1554 setDoesNotThrow(F);
1555 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001556 } else if (Name == "strtok" ||
1557 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001558 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001559 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001560 continue;
1561 setDoesNotThrow(F);
1562 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001563 } else if (Name == "scanf" ||
1564 Name == "setbuf" ||
1565 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001566 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001567 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001568 continue;
1569 setDoesNotThrow(F);
1570 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001571 } else if (Name == "strdup" ||
1572 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001573 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001574 !FTy->getReturnType()->isPointerTy() ||
1575 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001576 continue;
1577 setDoesNotThrow(F);
1578 setDoesNotAlias(F, 0);
1579 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001580 } else if (Name == "stat" ||
1581 Name == "sscanf" ||
1582 Name == "sprintf" ||
1583 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001584 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001585 !FTy->getParamType(0)->isPointerTy() ||
1586 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001587 continue;
1588 setDoesNotThrow(F);
1589 setDoesNotCapture(F, 1);
1590 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001591 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001592 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001593 !FTy->getParamType(0)->isPointerTy() ||
1594 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001595 continue;
1596 setDoesNotThrow(F);
1597 setDoesNotCapture(F, 1);
1598 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001599 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001600 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001601 !FTy->getParamType(1)->isPointerTy() ||
1602 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001603 continue;
1604 setDoesNotThrow(F);
1605 setDoesNotCapture(F, 2);
1606 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001607 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001608 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001609 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001610 continue;
1611 // May throw; "system" is a valid pthread cancellation point.
1612 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001613 }
1614 break;
1615 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001616 if (Name == "malloc") {
1617 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001618 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001619 continue;
1620 setDoesNotThrow(F);
1621 setDoesNotAlias(F, 0);
1622 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001623 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001624 !FTy->getParamType(0)->isPointerTy() ||
1625 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001626 continue;
1627 setOnlyReadsMemory(F);
1628 setDoesNotThrow(F);
1629 setDoesNotCapture(F, 1);
1630 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001631 } else if (Name == "memchr" ||
1632 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001633 if (FTy->getNumParams() != 3)
1634 continue;
1635 setOnlyReadsMemory(F);
1636 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001637 } else if (Name == "modf" ||
1638 Name == "modff" ||
1639 Name == "modfl" ||
1640 Name == "memcpy" ||
1641 Name == "memccpy" ||
1642 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001643 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001644 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001645 continue;
1646 setDoesNotThrow(F);
1647 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001648 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001649 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001650 continue;
1651 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001652 } else if (Name == "mkdir" ||
1653 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001654 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001655 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001656 continue;
1657 setDoesNotThrow(F);
1658 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001659 }
1660 break;
1661 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001662 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001663 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001664 !FTy->getParamType(0)->isPointerTy() ||
1665 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001666 continue;
1667 setDoesNotThrow(F);
1668 setDoesNotAlias(F, 0);
1669 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001670 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001671 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001672 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001673 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001674 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001675 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001676 } else if (Name == "rmdir" ||
1677 Name == "rewind" ||
1678 Name == "remove" ||
1679 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001680 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001681 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001682 continue;
1683 setDoesNotThrow(F);
1684 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001685 } else if (Name == "rename" ||
1686 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001687 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001688 !FTy->getParamType(0)->isPointerTy() ||
1689 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001690 continue;
1691 setDoesNotThrow(F);
1692 setDoesNotCapture(F, 1);
1693 setDoesNotCapture(F, 2);
1694 }
1695 break;
1696 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001697 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001698 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001699 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001700 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001701 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001702 setDoesNotCapture(F, 2);
1703 }
1704 break;
1705 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001706 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001707 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001708 !FTy->getParamType(0)->isPointerTy() ||
1709 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001710 continue;
1711 setDoesNotThrow(F);
1712 setDoesNotCapture(F, 1);
1713 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001714 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001715 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001716 !FTy->getParamType(0)->isPointerTy() ||
1717 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001718 continue;
1719 setDoesNotThrow(F);
1720 setOnlyReadsMemory(F);
1721 setDoesNotCapture(F, 1);
1722 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001723 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001724 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001725 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001726 continue;
1727 setDoesNotThrow(F);
1728 setDoesNotCapture(F, 1);
1729 }
1730 break;
1731 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001732 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001733 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001734 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001735 continue;
1736 setDoesNotThrow(F);
1737 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001738 } else if (Name == "chmod" ||
1739 Name == "chown" ||
1740 Name == "ctermid" ||
1741 Name == "clearerr" ||
1742 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001743 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001744 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001745 continue;
1746 setDoesNotThrow(F);
1747 setDoesNotCapture(F, 1);
1748 }
1749 break;
1750 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001751 if (Name == "atoi" ||
1752 Name == "atol" ||
1753 Name == "atof" ||
1754 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001755 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001756 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001757 continue;
1758 setDoesNotThrow(F);
1759 setOnlyReadsMemory(F);
1760 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001761 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001762 if (FTy->getNumParams() != 2 ||
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 setDoesNotCapture(F, 1);
1767 }
1768 break;
1769 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001770 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001771 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001772 !FTy->getReturnType()->isPointerTy() ||
1773 !FTy->getParamType(0)->isPointerTy() ||
1774 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001775 continue;
1776 setDoesNotThrow(F);
1777 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001778 setDoesNotCapture(F, 1);
1779 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001780 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001781 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001782 !FTy->getReturnType()->isPointerTy() ||
1783 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001784 continue;
1785 setDoesNotThrow(F);
1786 setDoesNotAlias(F, 0);
1787 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001788 } else if (Name == "feof" ||
1789 Name == "free" ||
1790 Name == "fseek" ||
1791 Name == "ftell" ||
1792 Name == "fgetc" ||
1793 Name == "fseeko" ||
1794 Name == "ftello" ||
1795 Name == "fileno" ||
1796 Name == "fflush" ||
1797 Name == "fclose" ||
1798 Name == "fsetpos" ||
1799 Name == "flockfile" ||
1800 Name == "funlockfile" ||
1801 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001802 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001803 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001804 continue;
1805 setDoesNotThrow(F);
1806 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001807 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001808 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001809 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001810 continue;
1811 setDoesNotThrow(F);
1812 setDoesNotCapture(F, 1);
1813 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001814 } else if (Name == "fputc" ||
1815 Name == "fstat" ||
1816 Name == "frexp" ||
1817 Name == "frexpf" ||
1818 Name == "frexpl" ||
1819 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001820 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001821 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001822 continue;
1823 setDoesNotThrow(F);
1824 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001825 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001826 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001827 !FTy->getParamType(0)->isPointerTy() ||
1828 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001829 continue;
1830 setDoesNotThrow(F);
1831 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001832 } else if (Name == "fread" ||
1833 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001834 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001835 !FTy->getParamType(0)->isPointerTy() ||
1836 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001837 continue;
1838 setDoesNotThrow(F);
1839 setDoesNotCapture(F, 1);
1840 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001841 } else if (Name == "fputs" ||
1842 Name == "fscanf" ||
1843 Name == "fprintf" ||
1844 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001845 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001846 !FTy->getParamType(0)->isPointerTy() ||
1847 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001848 continue;
1849 setDoesNotThrow(F);
1850 setDoesNotCapture(F, 1);
1851 setDoesNotCapture(F, 2);
1852 }
1853 break;
1854 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001855 if (Name == "getc" ||
1856 Name == "getlogin_r" ||
1857 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001858 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001859 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001860 continue;
1861 setDoesNotThrow(F);
1862 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001863 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001864 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001865 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001866 continue;
1867 setDoesNotThrow(F);
1868 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001869 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001870 } else if (Name == "gets" ||
1871 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001872 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001873 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001874 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001875 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001876 continue;
1877 setDoesNotThrow(F);
1878 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001879 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001880 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001881 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001882 continue;
1883 setDoesNotThrow(F);
1884 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001885 }
1886 break;
1887 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001888 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001889 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001890 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001891 continue;
1892 setDoesNotThrow(F);
1893 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001894 } else if (Name == "uname" ||
1895 Name == "unlink" ||
1896 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001897 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001898 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001899 continue;
1900 setDoesNotThrow(F);
1901 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001902 } else if (Name == "utime" ||
1903 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001904 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001905 !FTy->getParamType(0)->isPointerTy() ||
1906 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001907 continue;
1908 setDoesNotThrow(F);
1909 setDoesNotCapture(F, 1);
1910 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001911 }
1912 break;
1913 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001914 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001915 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001916 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001917 continue;
1918 setDoesNotThrow(F);
1919 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001920 } else if (Name == "puts" ||
1921 Name == "printf" ||
1922 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001923 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001924 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001925 continue;
1926 setDoesNotThrow(F);
1927 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001928 } else if (Name == "pread" ||
1929 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001930 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001931 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001932 continue;
1933 // May throw; these are valid pthread cancellation points.
1934 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001935 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001936 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001937 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001938 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001939 !FTy->getReturnType()->isPointerTy() ||
1940 !FTy->getParamType(0)->isPointerTy() ||
1941 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001942 continue;
1943 setDoesNotThrow(F);
1944 setDoesNotAlias(F, 0);
1945 setDoesNotCapture(F, 1);
1946 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001947 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001948 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001949 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001950 continue;
1951 setDoesNotThrow(F);
1952 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001953 }
1954 break;
1955 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001956 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001957 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001958 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001959 continue;
1960 setDoesNotThrow(F);
1961 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001962 } else if (Name == "vsscanf" ||
1963 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001964 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001965 !FTy->getParamType(1)->isPointerTy() ||
1966 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001967 continue;
1968 setDoesNotThrow(F);
1969 setDoesNotCapture(F, 1);
1970 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001971 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00001972 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001973 continue;
1974 setDoesNotThrow(F);
1975 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001976 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001977 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001978 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001979 continue;
1980 setDoesNotThrow(F);
1981 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001982 } else if (Name == "vfprintf" ||
1983 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001984 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001985 !FTy->getParamType(0)->isPointerTy() ||
1986 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001987 continue;
1988 setDoesNotThrow(F);
1989 setDoesNotCapture(F, 1);
1990 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001991 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001992 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001993 !FTy->getParamType(0)->isPointerTy() ||
1994 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001995 continue;
1996 setDoesNotThrow(F);
1997 setDoesNotCapture(F, 1);
1998 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001999 }
2000 break;
2001 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002002 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002003 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002004 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002005 continue;
2006 // May throw; "open" is a valid pthread cancellation point.
2007 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002008 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002009 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002010 !FTy->getReturnType()->isPointerTy() ||
2011 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002012 continue;
2013 setDoesNotThrow(F);
2014 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002015 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002016 }
2017 break;
2018 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002019 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00002020 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002021 continue;
2022 setDoesNotThrow(F);
2023 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002024 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002025 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002026 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002027 continue;
2028 setDoesNotThrow(F);
2029 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002030 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002031 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002032 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002033 if (Name == "htonl" ||
2034 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002035 setDoesNotThrow(F);
2036 setDoesNotAccessMemory(F);
2037 }
2038 break;
2039 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002040 if (Name == "ntohl" ||
2041 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002042 setDoesNotThrow(F);
2043 setDoesNotAccessMemory(F);
2044 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002045 break;
2046 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002047 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002048 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002049 !FTy->getParamType(0)->isPointerTy() ||
2050 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002051 continue;
2052 setDoesNotThrow(F);
2053 setDoesNotCapture(F, 1);
2054 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002055 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002056 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002057 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002058 continue;
2059 setDoesNotThrow(F);
2060 setDoesNotCapture(F, 1);
2061 }
2062 break;
2063 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002064 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002065 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002066 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002067 continue;
2068 // May throw; places call through function pointer.
2069 setDoesNotCapture(F, 4);
2070 }
2071 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002072 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002073 if (Name == "__strdup" ||
2074 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002075 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002076 !FTy->getReturnType()->isPointerTy() ||
2077 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002078 continue;
2079 setDoesNotThrow(F);
2080 setDoesNotAlias(F, 0);
2081 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002082 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002083 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002084 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002085 continue;
2086 setDoesNotThrow(F);
2087 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002088 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002089 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002090 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002091 continue;
2092 setDoesNotThrow(F);
2093 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002094 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002095 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002096 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002097 continue;
2098 setDoesNotThrow(F);
2099 setDoesNotCapture(F, 2);
2100 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002101 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002102 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002103 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002104 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002105 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002106 continue;
2107 setDoesNotThrow(F);
2108 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002109 } else if (Name == "\1stat64" ||
2110 Name == "\1lstat64" ||
2111 Name == "\1statvfs64" ||
2112 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002113 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002114 !FTy->getParamType(0)->isPointerTy() ||
2115 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002116 continue;
2117 setDoesNotThrow(F);
2118 setDoesNotCapture(F, 1);
2119 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002120 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002121 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002122 !FTy->getReturnType()->isPointerTy() ||
2123 !FTy->getParamType(0)->isPointerTy() ||
2124 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002125 continue;
2126 setDoesNotThrow(F);
2127 setDoesNotAlias(F, 0);
2128 setDoesNotCapture(F, 1);
2129 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002130 } else if (Name == "\1fseeko64" ||
2131 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002132 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002133 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002134 continue;
2135 setDoesNotThrow(F);
2136 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002137 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002138 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002139 continue;
2140 setDoesNotThrow(F);
2141 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002142 } else if (Name == "\1fstat64" ||
2143 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002144 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002145 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002146 continue;
2147 setDoesNotThrow(F);
2148 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002149 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002150 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002151 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002152 continue;
2153 // May throw; "open" is a valid pthread cancellation point.
2154 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002155 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002156 break;
2157 }
2158 }
2159 return Modified;
2160}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002161
2162// TODO:
2163// Additional cases that we need to add to this file:
2164//
2165// cbrt:
2166// * cbrt(expN(X)) -> expN(x/3)
2167// * cbrt(sqrt(x)) -> pow(x,1/6)
2168// * cbrt(sqrt(x)) -> pow(x,1/9)
2169//
2170// cos, cosf, cosl:
2171// * cos(-x) -> cos(x)
2172//
2173// exp, expf, expl:
2174// * exp(log(x)) -> x
2175//
2176// log, logf, logl:
2177// * log(exp(x)) -> x
2178// * log(x**y) -> y*log(x)
2179// * log(exp(y)) -> y*log(e)
2180// * log(exp2(y)) -> y*log(2)
2181// * log(exp10(y)) -> y*log(10)
2182// * log(sqrt(x)) -> 0.5*log(x)
2183// * log(pow(x,y)) -> y*log(x)
2184//
2185// lround, lroundf, lroundl:
2186// * lround(cnst) -> cnst'
2187//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002188// pow, powf, powl:
2189// * pow(exp(x),y) -> exp(x*y)
2190// * pow(sqrt(x),y) -> pow(x,y*0.5)
2191// * pow(pow(x,y),z)-> pow(x,y*z)
2192//
2193// puts:
Dan Gohman2511bd02010-08-04 01:16:35 +00002194// * puts("") -> putchar('\n')
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002195//
2196// round, roundf, roundl:
2197// * round(cnst) -> cnst'
2198//
2199// signbit:
2200// * signbit(cnst) -> cnst'
2201// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2202//
2203// sqrt, sqrtf, sqrtl:
2204// * sqrt(expN(x)) -> expN(x*0.5)
2205// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2206// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2207//
2208// stpcpy:
2209// * stpcpy(str, "literal") ->
2210// llvm.memcpy(str,"literal",strlen("literal")+1,1)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002211//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002212// strpbrk:
2213// * strpbrk(s,a) -> offset_in_for(s,a)
2214// (if s and a are both constant strings)
2215// * strpbrk(s,"") -> 0
2216// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2217//
2218// strspn, strcspn:
2219// * strspn(s,a) -> const_int (if both args are constant)
2220// * strspn("",a) -> 0
2221// * strspn(s,"") -> 0
2222// * strcspn(s,a) -> const_int (if both args are constant)
2223// * strcspn("",a) -> 0
2224// * strcspn(s,"") -> strlen(a)
2225//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002226// tan, tanf, tanl:
2227// * tan(atan(x)) -> x
2228//
2229// trunc, truncf, truncl:
2230// * trunc(cnst) -> cnst'
2231//
2232//