blob: 43c6f6dda25c3e6b1a5d4020c63f1cf99a375ae8 [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) ||
Benjamin Kramer4c756792010-09-30 11:21:59 +0000226 FT->getParamType(0) != FT->getReturnType() ||
227 !FT->getParamType(1)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000228 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000229
Gabor Greifaee5dc12010-06-24 10:42:46 +0000230 Value *SrcStr = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000231
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000232 // If the second operand is non-constant, see if we can compute the length
233 // of the input string and turn this into memchr.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000234 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000235 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000236 // These optimizations require TargetData.
237 if (!TD) return 0;
238
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000239 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000240 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000241 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000242
Gabor Greifaee5dc12010-06-24 10:42:46 +0000243 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000244 ConstantInt::get(TD->getIntPtrType(*Context), Len),
245 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000246 }
247
248 // Otherwise, the character is a constant, see if the first argument is
249 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000250 std::string Str;
251 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000252 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000253
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000254 // strchr can find the nul character.
255 Str += '\0';
Eric Christopher37c8b862009-10-07 21:14:25 +0000256
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000257 // Compute the offset.
Benjamin Kramere2609902010-09-29 22:29:12 +0000258 size_t I = Str.find(CharC->getSExtValue());
259 if (I == std::string::npos) // Didn't find the char. strchr returns null.
260 return Constant::getNullValue(CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000261
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000262 // strchr(s+n,c) -> gep(s+n+i,c)
Benjamin Kramere2609902010-09-29 22:29:12 +0000263 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000264 return B.CreateGEP(SrcStr, Idx, "strchr");
265 }
266};
267
268//===---------------------------------------===//
Benjamin Kramer06f25cf2010-09-29 21:50:51 +0000269// 'strrchr' Optimizations
270
271struct StrRChrOpt : public LibCallOptimization {
272 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
273 // Verify the "strrchr" function prototype.
274 const FunctionType *FT = Callee->getFunctionType();
275 if (FT->getNumParams() != 2 ||
276 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Benjamin Kramer4c756792010-09-30 11:21:59 +0000277 FT->getParamType(0) != FT->getReturnType() ||
278 !FT->getParamType(1)->isIntegerTy(32))
Benjamin Kramer06f25cf2010-09-29 21:50:51 +0000279 return 0;
280
281 Value *SrcStr = CI->getArgOperand(0);
282 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
283
284 // Cannot fold anything if we're not looking for a constant.
285 if (!CharC)
286 return 0;
287
288 std::string Str;
289 if (!GetConstantStringInfo(SrcStr, Str)) {
290 // strrchr(s, 0) -> strchr(s, 0)
291 if (TD && CharC->isZero())
292 return EmitStrChr(SrcStr, '\0', B, TD);
293 return 0;
294 }
295
296 // strrchr can find the nul character.
297 Str += '\0';
298
299 // Compute the offset.
300 size_t I = Str.rfind(CharC->getSExtValue());
301 if (I == std::string::npos) // Didn't find the char. Return null.
302 return Constant::getNullValue(CI->getType());
303
304 // strrchr(s+n,c) -> gep(s+n+i,c)
305 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
306 return B.CreateGEP(SrcStr, Idx, "strrchr");
307 }
308};
309
310//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000311// 'strcmp' Optimizations
312
Chris Lattner3e8b6632009-09-02 06:11:42 +0000313struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000314 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000315 // Verify the "strcmp" function prototype.
316 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000317 if (FT->getNumParams() != 2 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000318 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000319 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000320 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000321 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000322
Gabor Greifaee5dc12010-06-24 10:42:46 +0000323 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000324 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000325 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000326
Bill Wendling0582ae92009-03-13 04:39:26 +0000327 std::string Str1, Str2;
328 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
329 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000330
Bill Wendling0582ae92009-03-13 04:39:26 +0000331 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000332 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000333
Bill Wendling0582ae92009-03-13 04:39:26 +0000334 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000335 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000336
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000337 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000338 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000339 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000340 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000341
342 // strcmp(P, "x") -> memcmp(P, "x", 2)
343 uint64_t Len1 = GetStringLength(Str1P);
344 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000345 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000346 // These optimizations require TargetData.
347 if (!TD) return 0;
348
Nick Lewycky13a09e22008-12-21 00:19:21 +0000349 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000350 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000351 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000352 }
353
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000354 return 0;
355 }
356};
357
358//===---------------------------------------===//
359// 'strncmp' Optimizations
360
Chris Lattner3e8b6632009-09-02 06:11:42 +0000361struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000362 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000363 // Verify the "strncmp" function prototype.
364 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000365 if (FT->getNumParams() != 3 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000366 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000367 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000368 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000369 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000370 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000371
Gabor Greifaee5dc12010-06-24 10:42:46 +0000372 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000373 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000374 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000375
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000376 // Get the length argument if it is constant.
377 uint64_t Length;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000378 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000379 Length = LengthArg->getZExtValue();
380 else
381 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000382
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000383 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000384 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000385
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000386 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000387 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD);
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000388
Bill Wendling0582ae92009-03-13 04:39:26 +0000389 std::string Str1, Str2;
390 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
391 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000392
Bill Wendling0582ae92009-03-13 04:39:26 +0000393 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000394 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000395
Bill Wendling0582ae92009-03-13 04:39:26 +0000396 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000397 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000398
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000399 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000400 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000401 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000402 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000403 return 0;
404 }
405};
406
407
408//===---------------------------------------===//
409// 'strcpy' Optimizations
410
Chris Lattner3e8b6632009-09-02 06:11:42 +0000411struct StrCpyOpt : public LibCallOptimization {
Evan Chengeb8c6452010-03-24 20:19:04 +0000412 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
413
414 StrCpyOpt(bool c) : OptChkCall(c) {}
415
Eric Christopher7a61d702008-08-08 19:39:37 +0000416 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000417 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000418 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000419 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000420 if (FT->getNumParams() != NumParams ||
421 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000422 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000423 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000424 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000425
Gabor Greifaee5dc12010-06-24 10:42:46 +0000426 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000427 if (Dst == Src) // strcpy(x,x) -> x
428 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000429
Dan Gohmanf14d9192009-08-18 00:48:13 +0000430 // These optimizations require TargetData.
431 if (!TD) return 0;
432
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000433 // See if we can get the length of the input string.
434 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000435 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000436
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000437 // We have enough information to now generate the memcpy call to do the
438 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000439 if (OptChkCall)
440 EmitMemCpyChk(Dst, Src,
441 ConstantInt::get(TD->getIntPtrType(*Context), Len),
Gabor Greifaee5dc12010-06-24 10:42:46 +0000442 CI->getArgOperand(2), B, TD);
Evan Cheng0289b412010-03-23 15:48:04 +0000443 else
444 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000445 ConstantInt::get(TD->getIntPtrType(*Context), Len),
446 1, false, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000447 return Dst;
448 }
449};
450
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000451//===---------------------------------------===//
452// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000453
Chris Lattner3e8b6632009-09-02 06:11:42 +0000454struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000455 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
456 const FunctionType *FT = Callee->getFunctionType();
457 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
458 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000459 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000460 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000461 return 0;
462
Gabor Greifaee5dc12010-06-24 10:42:46 +0000463 Value *Dst = CI->getArgOperand(0);
464 Value *Src = CI->getArgOperand(1);
465 Value *LenOp = CI->getArgOperand(2);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000466
467 // See if we can get the length of the input string.
468 uint64_t SrcLen = GetStringLength(Src);
469 if (SrcLen == 0) return 0;
470 --SrcLen;
471
472 if (SrcLen == 0) {
473 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Mon P Wang20adc9d2010-04-04 03:10:48 +0000474 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'),
475 LenOp, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000476 return Dst;
477 }
478
479 uint64_t Len;
480 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
481 Len = LengthArg->getZExtValue();
482 else
483 return 0;
484
485 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
486
Dan Gohmanf14d9192009-08-18 00:48:13 +0000487 // These optimizations require TargetData.
488 if (!TD) return 0;
489
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000490 // Let strncpy handle the zero padding
491 if (Len > SrcLen+1) return 0;
492
493 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000494 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000495 ConstantInt::get(TD->getIntPtrType(*Context), Len),
496 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000497
498 return Dst;
499 }
500};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000501
502//===---------------------------------------===//
503// 'strlen' Optimizations
504
Chris Lattner3e8b6632009-09-02 06:11:42 +0000505struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000506 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000507 const FunctionType *FT = Callee->getFunctionType();
508 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000509 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000510 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000511 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000512
Gabor Greifaee5dc12010-06-24 10:42:46 +0000513 Value *Src = CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000514
515 // Constant folding: strlen("xyz") -> 3
516 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000517 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000518
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000519 // strlen(x) != 0 --> *x != 0
520 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000521 if (IsOnlyUsedInZeroEqualityComparison(CI))
522 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
523 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000524 }
525};
526
Benjamin Kramer05f585e2010-09-29 23:52:12 +0000527
528//===---------------------------------------===//
529// 'strpbrk' Optimizations
530
531struct StrPBrkOpt : public LibCallOptimization {
532 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
533 const FunctionType *FT = Callee->getFunctionType();
534 if (FT->getNumParams() != 2 ||
535 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
536 FT->getParamType(1) != FT->getParamType(0) ||
537 FT->getReturnType() != FT->getParamType(0))
538 return 0;
539
540 std::string S1, S2;
541 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
542 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
543
544 // strpbrk(s, "") -> NULL
545 // strpbrk("", s) -> NULL
546 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
547 return Constant::getNullValue(CI->getType());
548
549 // Constant folding.
550 if (HasS1 && HasS2) {
551 size_t I = S1.find_first_of(S2);
552 if (I == std::string::npos) // No match.
553 return Constant::getNullValue(CI->getType());
554
555 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
556 return B.CreateGEP(CI->getArgOperand(0), Idx, "strpbrk");
557 }
558
559 // strpbrk(s, "a") -> strchr(s, 'a')
560 if (TD && HasS2 && S2.size() == 1)
561 return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD);
562
563 return 0;
564 }
565};
566
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000567//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000568// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000569
Chris Lattner3e8b6632009-09-02 06:11:42 +0000570struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000571 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
572 const FunctionType *FT = Callee->getFunctionType();
573 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000574 !FT->getParamType(0)->isPointerTy() ||
575 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000576 return 0;
577
Gabor Greifaee5dc12010-06-24 10:42:46 +0000578 Value *EndPtr = CI->getArgOperand(1);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000579 if (isa<ConstantPointerNull>(EndPtr)) {
580 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000581 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000582 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000583
584 return 0;
585 }
586};
587
Chris Lattner24604112009-12-16 09:32:05 +0000588//===---------------------------------------===//
Benjamin Kramer9510a252010-09-30 00:58:35 +0000589// 'strspn' Optimizations
590
591struct StrSpnOpt : public LibCallOptimization {
592 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
593 const FunctionType *FT = Callee->getFunctionType();
594 if (FT->getNumParams() != 2 ||
595 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
596 FT->getParamType(1) != FT->getParamType(0) ||
597 !FT->getReturnType()->isIntegerTy())
598 return 0;
599
600 std::string S1, S2;
601 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
602 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
603
604 // strspn(s, "") -> 0
605 // strspn("", s) -> 0
606 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
607 return Constant::getNullValue(CI->getType());
608
609 // Constant folding.
610 if (HasS1 && HasS2)
611 return ConstantInt::get(CI->getType(), strspn(S1.c_str(), S2.c_str()));
612
613 return 0;
614 }
615};
616
617//===---------------------------------------===//
618// 'strcspn' Optimizations
619
620struct StrCSpnOpt : public LibCallOptimization {
621 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
622 const FunctionType *FT = Callee->getFunctionType();
623 if (FT->getNumParams() != 2 ||
624 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
625 FT->getParamType(1) != FT->getParamType(0) ||
626 !FT->getReturnType()->isIntegerTy())
627 return 0;
628
629 std::string S1, S2;
630 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
631 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
632
633 // strcspn("", s) -> 0
634 if (HasS1 && S1.empty())
635 return Constant::getNullValue(CI->getType());
636
637 // Constant folding.
638 if (HasS1 && HasS2)
639 return ConstantInt::get(CI->getType(), strcspn(S1.c_str(), S2.c_str()));
640
641 // strcspn(s, "") -> strlen(s)
642 if (TD && HasS2 && S2.empty())
643 return EmitStrLen(CI->getArgOperand(0), B, TD);
644
645 return 0;
646 }
647};
648
649//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000650// 'strstr' Optimizations
651
652struct StrStrOpt : public LibCallOptimization {
653 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
654 const FunctionType *FT = Callee->getFunctionType();
655 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000656 !FT->getParamType(0)->isPointerTy() ||
657 !FT->getParamType(1)->isPointerTy() ||
658 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000659 return 0;
660
661 // fold strstr(x, x) -> x.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000662 if (CI->getArgOperand(0) == CI->getArgOperand(1))
663 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000664
Benjamin Kramer386e9182010-06-15 21:34:25 +0000665 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000666 if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
667 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD);
668 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000669 StrLen, B, TD);
670 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
671 UI != UE; ) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000672 ICmpInst *Old = cast<ICmpInst>(*UI++);
Benjamin Kramer386e9182010-06-15 21:34:25 +0000673 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
674 ConstantInt::getNullValue(StrNCmp->getType()),
675 "cmp");
676 Old->replaceAllUsesWith(Cmp);
677 Old->eraseFromParent();
678 }
679 return CI;
680 }
681
Chris Lattner24604112009-12-16 09:32:05 +0000682 // See if either input string is a constant string.
683 std::string SearchStr, ToFindStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000684 bool HasStr1 = GetConstantStringInfo(CI->getArgOperand(0), SearchStr);
685 bool HasStr2 = GetConstantStringInfo(CI->getArgOperand(1), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000686
Chris Lattner24604112009-12-16 09:32:05 +0000687 // fold strstr(x, "") -> x.
688 if (HasStr2 && ToFindStr.empty())
Gabor Greifaee5dc12010-06-24 10:42:46 +0000689 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000690
Chris Lattner24604112009-12-16 09:32:05 +0000691 // If both strings are known, constant fold it.
692 if (HasStr1 && HasStr2) {
693 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000694
Chris Lattner24604112009-12-16 09:32:05 +0000695 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
696 return Constant::getNullValue(CI->getType());
697
698 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000699 Value *Result = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner24604112009-12-16 09:32:05 +0000700 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
701 return B.CreateBitCast(Result, CI->getType());
702 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000703
Chris Lattner24604112009-12-16 09:32:05 +0000704 // fold strstr(x, "y") -> strchr(x, 'y').
705 if (HasStr2 && ToFindStr.size() == 1)
Gabor Greifa3997812010-07-22 10:37:47 +0000706 return B.CreateBitCast(EmitStrChr(CI->getArgOperand(0),
707 ToFindStr[0], B, TD), CI->getType());
Chris Lattner24604112009-12-16 09:32:05 +0000708 return 0;
709 }
710};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000711
Nick Lewycky4c498412009-02-13 15:31:46 +0000712
713//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000714// 'memcmp' Optimizations
715
Chris Lattner3e8b6632009-09-02 06:11:42 +0000716struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000717 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000718 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000719 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
720 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000721 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000722 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000723
Gabor Greifaee5dc12010-06-24 10:42:46 +0000724 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000725
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000726 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000727 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000728
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000729 // Make sure we have a constant length.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000730 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000731 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000732 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000733
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000734 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000735 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000736
Benjamin Kramer48aefe12010-05-25 22:53:43 +0000737 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
738 if (Len == 1) {
739 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
740 CI->getType(), "lhsv");
741 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
742 CI->getType(), "rhsv");
Benjamin Kramer1464c1d2010-05-26 09:45:04 +0000743 return B.CreateSub(LHSV, RHSV, "chardiff");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000744 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000745
Benjamin Kramer992a6372009-11-05 17:44:22 +0000746 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
747 std::string LHSStr, RHSStr;
748 if (GetConstantStringInfo(LHS, LHSStr) &&
749 GetConstantStringInfo(RHS, RHSStr)) {
750 // Make sure we're not reading out-of-bounds memory.
751 if (Len > LHSStr.length() || Len > RHSStr.length())
752 return 0;
753 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
754 return ConstantInt::get(CI->getType(), Ret);
755 }
756
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000757 return 0;
758 }
759};
760
761//===---------------------------------------===//
762// 'memcpy' Optimizations
763
Chris Lattner3e8b6632009-09-02 06:11:42 +0000764struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000765 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000766 // These optimizations require TargetData.
767 if (!TD) return 0;
768
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000769 const FunctionType *FT = Callee->getFunctionType();
770 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000771 !FT->getParamType(0)->isPointerTy() ||
772 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000773 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000774 return 0;
775
776 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000777 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
778 CI->getArgOperand(2), 1, false, B, TD);
779 return CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000780 }
781};
782
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000783//===---------------------------------------===//
784// 'memmove' Optimizations
785
Chris Lattner3e8b6632009-09-02 06:11:42 +0000786struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000787 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000788 // These optimizations require TargetData.
789 if (!TD) return 0;
790
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000791 const FunctionType *FT = Callee->getFunctionType();
792 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000793 !FT->getParamType(0)->isPointerTy() ||
794 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000795 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000796 return 0;
797
798 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000799 EmitMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
800 CI->getArgOperand(2), 1, false, B, TD);
801 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000802 }
803};
804
805//===---------------------------------------===//
806// 'memset' Optimizations
807
Chris Lattner3e8b6632009-09-02 06:11:42 +0000808struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000809 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000810 // These optimizations require TargetData.
811 if (!TD) return 0;
812
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000813 const FunctionType *FT = Callee->getFunctionType();
814 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000815 !FT->getParamType(0)->isPointerTy() ||
816 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000817 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000818 return 0;
819
820 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Gabor Greifa3997812010-07-22 10:37:47 +0000821 Value *Val = B.CreateIntCast(CI->getArgOperand(1),
822 Type::getInt8Ty(*Context), false);
Gabor Greifaee5dc12010-06-24 10:42:46 +0000823 EmitMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), false, B, TD);
824 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000825 }
826};
827
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000828//===----------------------------------------------------------------------===//
829// Math Library Optimizations
830//===----------------------------------------------------------------------===//
831
832//===---------------------------------------===//
833// 'pow*' Optimizations
834
Chris Lattner3e8b6632009-09-02 06:11:42 +0000835struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000836 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000837 const FunctionType *FT = Callee->getFunctionType();
838 // Just make sure this has 2 arguments of the same FP type, which match the
839 // result type.
840 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
841 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000842 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000843 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000844
Gabor Greifaee5dc12010-06-24 10:42:46 +0000845 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000846 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
847 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
848 return Op1C;
849 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000850 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000851 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000852
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000853 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
854 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000855
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000856 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000857 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000858
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000859 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000860 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
861 // This is faster than calling pow, and still handles negative zero
862 // and negative infinite correctly.
863 // TODO: In fast-math mode, this could be just sqrt(x).
864 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000865 Value *Inf = ConstantFP::getInfinity(CI->getType());
866 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000867 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
868 Callee->getAttributes());
869 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
870 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000871 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
872 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
873 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000874 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000875
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000876 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
877 return Op1;
878 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000879 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000880 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000881 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000882 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000883 return 0;
884 }
885};
886
887//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000888// 'exp2' Optimizations
889
Chris Lattner3e8b6632009-09-02 06:11:42 +0000890struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000891 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000892 const FunctionType *FT = Callee->getFunctionType();
893 // Just make sure this has 1 argument of FP type, which matches the
894 // result type.
895 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000896 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000897 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000898
Gabor Greifaee5dc12010-06-24 10:42:46 +0000899 Value *Op = CI->getArgOperand(0);
Chris Lattnere818f772008-05-02 18:43:35 +0000900 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
901 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
902 Value *LdExpArg = 0;
903 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
904 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000905 LdExpArg = B.CreateSExt(OpC->getOperand(0),
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000906 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000907 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
908 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000909 LdExpArg = B.CreateZExt(OpC->getOperand(0),
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000910 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000911 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000912
Chris Lattnere818f772008-05-02 18:43:35 +0000913 if (LdExpArg) {
914 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000915 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000916 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000917 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000918 Name = "ldexp";
919 else
920 Name = "ldexpl";
921
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000922 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000923 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000924 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000925
926 Module *M = Caller->getParent();
927 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000928 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000929 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000930 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
931 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
932 CI->setCallingConv(F->getCallingConv());
933
934 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000935 }
936 return 0;
937 }
938};
Chris Lattnere818f772008-05-02 18:43:35 +0000939
940//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000941// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
942
Chris Lattner3e8b6632009-09-02 06:11:42 +0000943struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000944 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000945 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000946 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
947 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000948 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000949
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000950 // If this is something like 'floor((double)floatval)', convert to floorf.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000951 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000952 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000953 return 0;
954
955 // floor((double)floatval) -> (double)floorf(floatval)
956 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000957 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
958 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000959 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000960 }
961};
962
963//===----------------------------------------------------------------------===//
964// Integer Optimizations
965//===----------------------------------------------------------------------===//
966
967//===---------------------------------------===//
968// 'ffs*' Optimizations
969
Chris Lattner3e8b6632009-09-02 06:11:42 +0000970struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000971 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000972 const FunctionType *FT = Callee->getFunctionType();
973 // Just make sure this has 2 arguments of the same FP type, which match the
974 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000975 if (FT->getNumParams() != 1 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000976 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000977 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000978 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000979
Gabor Greifaee5dc12010-06-24 10:42:46 +0000980 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000981
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000982 // Constant fold.
983 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
984 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000985 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000986 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000987 CI->getValue().countTrailingZeros()+1);
988 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000989
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000990 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
991 const Type *ArgType = Op->getType();
992 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
993 Intrinsic::cttz, &ArgType, 1);
994 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000995 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000996 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000997
Owen Andersona7235ea2009-07-31 20:28:14 +0000998 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000999 return B.CreateSelect(Cond, V,
Nick Lewycky10d2f4d2010-07-06 03:53:43 +00001000 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001001 }
1002};
1003
1004//===---------------------------------------===//
1005// 'isdigit' Optimizations
1006
Chris Lattner3e8b6632009-09-02 06:11:42 +00001007struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001008 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001009 const FunctionType *FT = Callee->getFunctionType();
1010 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +00001011 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001012 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001013 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001014
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001015 // isdigit(c) -> (c-'0') <u 10
Gabor Greifaee5dc12010-06-24 10:42:46 +00001016 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001017 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001018 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001019 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001020 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001021 return B.CreateZExt(Op, CI->getType());
1022 }
1023};
1024
1025//===---------------------------------------===//
1026// 'isascii' Optimizations
1027
Chris Lattner3e8b6632009-09-02 06:11:42 +00001028struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001029 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001030 const FunctionType *FT = Callee->getFunctionType();
1031 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +00001032 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001033 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001034 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001035
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001036 // isascii(c) -> c <u 128
Gabor Greifaee5dc12010-06-24 10:42:46 +00001037 Value *Op = CI->getArgOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +00001038 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001039 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001040 return B.CreateZExt(Op, CI->getType());
1041 }
1042};
Eric Christopher37c8b862009-10-07 21:14:25 +00001043
Chris Lattner313f0e62008-06-09 08:26:51 +00001044//===---------------------------------------===//
1045// 'abs', 'labs', 'llabs' Optimizations
1046
Chris Lattner3e8b6632009-09-02 06:11:42 +00001047struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001048 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001049 const FunctionType *FT = Callee->getFunctionType();
1050 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +00001051 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +00001052 FT->getParamType(0) != FT->getReturnType())
1053 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001054
Chris Lattner313f0e62008-06-09 08:26:51 +00001055 // abs(x) -> x >s -1 ? x : -x
Gabor Greifaee5dc12010-06-24 10:42:46 +00001056 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001057 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001058 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001059 "ispos");
1060 Value *Neg = B.CreateNeg(Op, "neg");
1061 return B.CreateSelect(Pos, Op, Neg);
1062 }
1063};
Eric Christopher37c8b862009-10-07 21:14:25 +00001064
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001065
1066//===---------------------------------------===//
1067// 'toascii' Optimizations
1068
Chris Lattner3e8b6632009-09-02 06:11:42 +00001069struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001070 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001071 const FunctionType *FT = Callee->getFunctionType();
1072 // We require i32(i32)
1073 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001074 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001075 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001076
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001077 // isascii(c) -> c & 0x7f
Gabor Greifaee5dc12010-06-24 10:42:46 +00001078 return B.CreateAnd(CI->getArgOperand(0),
Owen Andersoneed707b2009-07-24 23:12:02 +00001079 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001080 }
1081};
1082
1083//===----------------------------------------------------------------------===//
1084// Formatting and IO Optimizations
1085//===----------------------------------------------------------------------===//
1086
1087//===---------------------------------------===//
1088// 'printf' Optimizations
1089
Chris Lattner3e8b6632009-09-02 06:11:42 +00001090struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001091 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001092 // Require one fixed pointer argument and an integer/void result.
1093 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001094 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1095 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001096 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001097 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001098
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001099 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001100 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001101 if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001102 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001103
1104 // Empty format string -> noop.
1105 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001106 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001107 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001108
Chris Lattner74965f22009-11-09 04:57:04 +00001109 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1110 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001111 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001112 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +00001113 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +00001114 if (CI->use_empty()) return CI;
1115 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001116 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001117
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001118 // printf("foo\n") --> puts("foo")
1119 if (FormatStr[FormatStr.size()-1] == '\n' &&
1120 FormatStr.find('%') == std::string::npos) { // no format characters.
1121 // Create a string literal with no \n on it. We expect the constant merge
1122 // pass to be run after this pass, to merge duplicate strings.
1123 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001124 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001125 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1126 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +00001127 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001128 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001129 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001130 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001131
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001132 // Optimize specific format strings.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001133 // printf("%c", chr) --> putchar(chr)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001134 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001135 CI->getArgOperand(1)->getType()->isIntegerTy()) {
1136 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001137
Chris Lattner74965f22009-11-09 04:57:04 +00001138 if (CI->use_empty()) return CI;
1139 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001140 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001141
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001142 // printf("%s\n", str) --> puts(str)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001143 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001144 CI->getArgOperand(1)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001145 CI->use_empty()) {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001146 EmitPutS(CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001147 return CI;
1148 }
1149 return 0;
1150 }
1151};
1152
1153//===---------------------------------------===//
1154// 'sprintf' Optimizations
1155
Chris Lattner3e8b6632009-09-02 06:11:42 +00001156struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001157 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001158 // Require two fixed pointer arguments and an integer result.
1159 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001160 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1161 !FT->getParamType(1)->isPointerTy() ||
1162 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001163 return 0;
1164
1165 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001166 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001167 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001168 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001169
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001170 // If we just have a format string (nothing else crazy) transform it.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001171 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001172 // Make sure there's no % in the constant array. We could try to handle
1173 // %% -> % in the future if we cared.
1174 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1175 if (FormatStr[i] == '%')
1176 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001177
1178 // These optimizations require TargetData.
1179 if (!TD) return 0;
1180
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001181 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
Gabor Greifa3997812010-07-22 10:37:47 +00001182 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), // Copy the
1183 ConstantInt::get(TD->getIntPtrType(*Context), // nul byte.
1184 FormatStr.size() + 1), 1, false, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001185 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001186 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001187
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001188 // The remaining optimizations require the format string to be "%s" or "%c"
1189 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001190 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1191 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001192 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001193
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001194 // Decode the second character of the format string.
1195 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001196 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Gabor Greifaee5dc12010-06-24 10:42:46 +00001197 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1198 Value *V = B.CreateTrunc(CI->getArgOperand(2),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001199 Type::getInt8Ty(*Context), "char");
Gabor Greifaee5dc12010-06-24 10:42:46 +00001200 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001201 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001202 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001203 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001204 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001205
Owen Andersoneed707b2009-07-24 23:12:02 +00001206 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001207 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001208
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001209 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001210 // These optimizations require TargetData.
1211 if (!TD) return 0;
1212
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001213 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001214 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001215
Gabor Greifaee5dc12010-06-24 10:42:46 +00001216 Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001217 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001218 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001219 "leninc");
Gabor Greifa3997812010-07-22 10:37:47 +00001220 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(2),
1221 IncLen, 1, false, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001222
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001223 // The sprintf result is the unincremented number of bytes in the string.
1224 return B.CreateIntCast(Len, CI->getType(), false);
1225 }
1226 return 0;
1227 }
1228};
1229
1230//===---------------------------------------===//
1231// 'fwrite' Optimizations
1232
Chris Lattner3e8b6632009-09-02 06:11:42 +00001233struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001234 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001235 // Require a pointer, an integer, an integer, a pointer, returning integer.
1236 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001237 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1238 !FT->getParamType(1)->isIntegerTy() ||
1239 !FT->getParamType(2)->isIntegerTy() ||
1240 !FT->getParamType(3)->isPointerTy() ||
1241 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001242 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001243
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001244 // Get the element size and count.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001245 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1246 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001247 if (!SizeC || !CountC) return 0;
1248 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001249
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001250 // If this is writing zero records, remove the call (it's a noop).
1251 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001252 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001253
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001254 // If this is writing one byte, turn it into fputc.
1255 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001256 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1257 EmitFPutC(Char, CI->getArgOperand(3), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001258 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001259 }
1260
1261 return 0;
1262 }
1263};
1264
1265//===---------------------------------------===//
1266// 'fputs' Optimizations
1267
Chris Lattner3e8b6632009-09-02 06:11:42 +00001268struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001269 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001270 // These optimizations require TargetData.
1271 if (!TD) return 0;
1272
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001273 // Require two pointers. Also, we can't optimize if return value is used.
1274 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001275 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1276 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001277 !CI->use_empty())
1278 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001279
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001280 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001281 uint64_t Len = GetStringLength(CI->getArgOperand(0));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001282 if (!Len) return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001283 EmitFWrite(CI->getArgOperand(0),
Owen Anderson1d0be152009-08-13 21:58:54 +00001284 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001285 CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001286 return CI; // Known to have no uses (see above).
1287 }
1288};
1289
1290//===---------------------------------------===//
1291// 'fprintf' Optimizations
1292
Chris Lattner3e8b6632009-09-02 06:11:42 +00001293struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001294 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001295 // Require two fixed paramters as pointers and integer result.
1296 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001297 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1298 !FT->getParamType(1)->isPointerTy() ||
1299 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001300 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001301
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001302 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001303 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001304 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001305 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001306
1307 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001308 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001309 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1310 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001311 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001312
1313 // These optimizations require TargetData.
1314 if (!TD) return 0;
1315
Gabor Greifaee5dc12010-06-24 10:42:46 +00001316 EmitFWrite(CI->getArgOperand(1),
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001317 ConstantInt::get(TD->getIntPtrType(*Context),
1318 FormatStr.size()),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001319 CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001320 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001321 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001322
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001323 // The remaining optimizations require the format string to be "%s" or "%c"
1324 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001325 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1326 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001327 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001328
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001329 // Decode the second character of the format string.
1330 if (FormatStr[1] == 'c') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001331 // fprintf(F, "%c", chr) --> fputc(chr, F)
1332 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1333 EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001334 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001335 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001336
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001337 if (FormatStr[1] == 's') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001338 // fprintf(F, "%s", str) --> fputs(str, F)
1339 if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001340 return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001341 EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001342 return CI;
1343 }
1344 return 0;
1345 }
1346};
1347
Anders Carlsson303023d2010-11-30 06:19:18 +00001348//===---------------------------------------===//
1349// 'puts' Optimizations
1350
1351struct PutsOpt : public LibCallOptimization {
1352 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1353 // Require one fixed pointer argument and an integer/void result.
1354 const FunctionType *FT = Callee->getFunctionType();
1355 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1356 !(FT->getReturnType()->isIntegerTy() ||
1357 FT->getReturnType()->isVoidTy()))
1358 return 0;
1359
1360 // Check for a constant string.
1361 std::string Str;
1362 if (!GetConstantStringInfo(CI->getArgOperand(0), Str))
1363 return 0;
1364
1365 if (Str.empty()) {
1366 // puts("") -> putchar('\n')
1367 Value *Res = EmitPutChar(B.getInt32('\n'), B, TD);
1368 if (CI->use_empty()) return CI;
1369 return B.CreateIntCast(Res, CI->getType(), true);
1370 }
1371
1372 return 0;
1373 }
1374};
1375
Bill Wendlingac178222008-05-05 21:37:59 +00001376} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001377
1378//===----------------------------------------------------------------------===//
1379// SimplifyLibCalls Pass Implementation
1380//===----------------------------------------------------------------------===//
1381
1382namespace {
1383 /// This pass optimizes well known library functions from libc and libm.
1384 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001385 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001386 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001387 // String and Memory LibCall Optimizations
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001388 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrRChrOpt StrRChr;
1389 StrCmpOpt StrCmp; StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
Benjamin Kramer05f585e2010-09-29 23:52:12 +00001390 StrNCpyOpt StrNCpy; StrLenOpt StrLen; StrPBrkOpt StrPBrk;
Benjamin Kramer9510a252010-09-30 00:58:35 +00001391 StrToOpt StrTo; StrSpnOpt StrSpn; StrCSpnOpt StrCSpn; StrStrOpt StrStr;
Chris Lattner24604112009-12-16 09:32:05 +00001392 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001393 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001394 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001395 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001396 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1397 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001398 // Formatting and IO Optimizations
1399 SPrintFOpt SPrintF; PrintFOpt PrintF;
1400 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Anders Carlsson303023d2010-11-30 06:19:18 +00001401 PutsOpt Puts;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001402
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001403 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001404 public:
1405 static char ID; // Pass identification
Owen Anderson081c34b2010-10-19 17:21:58 +00001406 SimplifyLibCalls() : FunctionPass(ID), StrCpy(false), StrCpyChk(true) {
1407 initializeSimplifyLibCallsPass(*PassRegistry::getPassRegistry());
1408 }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001409 void InitOptimizations();
1410 bool runOnFunction(Function &F);
1411
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001412 void setDoesNotAccessMemory(Function &F);
1413 void setOnlyReadsMemory(Function &F);
1414 void setDoesNotThrow(Function &F);
1415 void setDoesNotCapture(Function &F, unsigned n);
1416 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001417 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001418
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001419 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001420 }
1421 };
1422 char SimplifyLibCalls::ID = 0;
1423} // end anonymous namespace.
1424
Owen Andersond13db2c2010-07-21 22:09:45 +00001425INITIALIZE_PASS(SimplifyLibCalls, "simplify-libcalls",
Owen Andersonce665bd2010-10-07 22:25:06 +00001426 "Simplify well-known library calls", false, false)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001427
1428// Public interface to the Simplify LibCalls pass.
1429FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001430 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001431}
1432
1433/// Optimizations - Populate the Optimizations map with all the optimizations
1434/// we know.
1435void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001436 // String and Memory LibCall Optimizations
1437 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001438 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001439 Optimizations["strchr"] = &StrChr;
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001440 Optimizations["strrchr"] = &StrRChr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001441 Optimizations["strcmp"] = &StrCmp;
1442 Optimizations["strncmp"] = &StrNCmp;
1443 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001444 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001445 Optimizations["strlen"] = &StrLen;
Benjamin Kramer05f585e2010-09-29 23:52:12 +00001446 Optimizations["strpbrk"] = &StrPBrk;
Nick Lewycky4c498412009-02-13 15:31:46 +00001447 Optimizations["strtol"] = &StrTo;
1448 Optimizations["strtod"] = &StrTo;
1449 Optimizations["strtof"] = &StrTo;
1450 Optimizations["strtoul"] = &StrTo;
1451 Optimizations["strtoll"] = &StrTo;
1452 Optimizations["strtold"] = &StrTo;
1453 Optimizations["strtoull"] = &StrTo;
Benjamin Kramer9510a252010-09-30 00:58:35 +00001454 Optimizations["strspn"] = &StrSpn;
1455 Optimizations["strcspn"] = &StrCSpn;
Chris Lattner24604112009-12-16 09:32:05 +00001456 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001457 Optimizations["memcmp"] = &MemCmp;
1458 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001459 Optimizations["memmove"] = &MemMove;
1460 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001461
Evan Cheng0289b412010-03-23 15:48:04 +00001462 // _chk variants of String and Memory LibCall Optimizations.
Evan Cheng0289b412010-03-23 15:48:04 +00001463 Optimizations["__strcpy_chk"] = &StrCpyChk;
1464
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001465 // Math Library Optimizations
1466 Optimizations["powf"] = &Pow;
1467 Optimizations["pow"] = &Pow;
1468 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001469 Optimizations["llvm.pow.f32"] = &Pow;
1470 Optimizations["llvm.pow.f64"] = &Pow;
1471 Optimizations["llvm.pow.f80"] = &Pow;
1472 Optimizations["llvm.pow.f128"] = &Pow;
1473 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001474 Optimizations["exp2l"] = &Exp2;
1475 Optimizations["exp2"] = &Exp2;
1476 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001477 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1478 Optimizations["llvm.exp2.f128"] = &Exp2;
1479 Optimizations["llvm.exp2.f80"] = &Exp2;
1480 Optimizations["llvm.exp2.f64"] = &Exp2;
1481 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001482
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001483#ifdef HAVE_FLOORF
1484 Optimizations["floor"] = &UnaryDoubleFP;
1485#endif
1486#ifdef HAVE_CEILF
1487 Optimizations["ceil"] = &UnaryDoubleFP;
1488#endif
1489#ifdef HAVE_ROUNDF
1490 Optimizations["round"] = &UnaryDoubleFP;
1491#endif
1492#ifdef HAVE_RINTF
1493 Optimizations["rint"] = &UnaryDoubleFP;
1494#endif
1495#ifdef HAVE_NEARBYINTF
1496 Optimizations["nearbyint"] = &UnaryDoubleFP;
1497#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001498
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001499 // Integer Optimizations
1500 Optimizations["ffs"] = &FFS;
1501 Optimizations["ffsl"] = &FFS;
1502 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001503 Optimizations["abs"] = &Abs;
1504 Optimizations["labs"] = &Abs;
1505 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001506 Optimizations["isdigit"] = &IsDigit;
1507 Optimizations["isascii"] = &IsAscii;
1508 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001509
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001510 // Formatting and IO Optimizations
1511 Optimizations["sprintf"] = &SPrintF;
1512 Optimizations["printf"] = &PrintF;
1513 Optimizations["fwrite"] = &FWrite;
1514 Optimizations["fputs"] = &FPuts;
1515 Optimizations["fprintf"] = &FPrintF;
Anders Carlsson303023d2010-11-30 06:19:18 +00001516 Optimizations["puts"] = &Puts;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001517}
1518
1519
1520/// runOnFunction - Top level algorithm.
1521///
1522bool SimplifyLibCalls::runOnFunction(Function &F) {
1523 if (Optimizations.empty())
1524 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001525
Dan Gohmanf14d9192009-08-18 00:48:13 +00001526 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001527
Owen Andersone922c022009-07-22 00:24:57 +00001528 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001529
1530 bool Changed = false;
1531 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1532 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1533 // Ignore non-calls.
1534 CallInst *CI = dyn_cast<CallInst>(I++);
1535 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001536
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001537 // Ignore indirect calls and calls to non-external functions.
1538 Function *Callee = CI->getCalledFunction();
1539 if (Callee == 0 || !Callee->isDeclaration() ||
1540 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1541 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001542
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001543 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001544 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1545 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001546
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001547 // Set the builder to the instruction after the call.
1548 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001549
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001550 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001551 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001552 if (Result == 0) continue;
1553
David Greene6a6b90e2010-01-05 01:27:21 +00001554 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1555 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001556
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001557 // Something changed!
1558 Changed = true;
1559 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001560
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001561 // Inspect the instruction after the call (which was potentially just
1562 // added) next.
1563 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001564
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001565 if (CI != Result && !CI->use_empty()) {
1566 CI->replaceAllUsesWith(Result);
1567 if (!Result->hasName())
1568 Result->takeName(CI);
1569 }
1570 CI->eraseFromParent();
1571 }
1572 }
1573 return Changed;
1574}
1575
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001576// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001577
1578void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1579 if (!F.doesNotAccessMemory()) {
1580 F.setDoesNotAccessMemory();
1581 ++NumAnnotated;
1582 Modified = true;
1583 }
1584}
1585void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1586 if (!F.onlyReadsMemory()) {
1587 F.setOnlyReadsMemory();
1588 ++NumAnnotated;
1589 Modified = true;
1590 }
1591}
1592void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1593 if (!F.doesNotThrow()) {
1594 F.setDoesNotThrow();
1595 ++NumAnnotated;
1596 Modified = true;
1597 }
1598}
1599void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1600 if (!F.doesNotCapture(n)) {
1601 F.setDoesNotCapture(n);
1602 ++NumAnnotated;
1603 Modified = true;
1604 }
1605}
1606void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1607 if (!F.doesNotAlias(n)) {
1608 F.setDoesNotAlias(n);
1609 ++NumAnnotated;
1610 Modified = true;
1611 }
1612}
1613
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001614/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001615///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001616bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001617 Modified = false;
1618 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1619 Function &F = *I;
1620 if (!F.isDeclaration())
1621 continue;
1622
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001623 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001624 continue;
1625
1626 const FunctionType *FTy = F.getFunctionType();
1627
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001628 StringRef Name = F.getName();
1629 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001630 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001631 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001632 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001633 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001634 continue;
1635 setOnlyReadsMemory(F);
1636 setDoesNotThrow(F);
1637 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001638 } else if (Name == "strchr" ||
1639 Name == "strrchr") {
1640 if (FTy->getNumParams() != 2 ||
1641 !FTy->getParamType(0)->isPointerTy() ||
1642 !FTy->getParamType(1)->isIntegerTy())
1643 continue;
1644 setOnlyReadsMemory(F);
1645 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001646 } else if (Name == "strcpy" ||
1647 Name == "stpcpy" ||
1648 Name == "strcat" ||
1649 Name == "strtol" ||
1650 Name == "strtod" ||
1651 Name == "strtof" ||
1652 Name == "strtoul" ||
1653 Name == "strtoll" ||
1654 Name == "strtold" ||
1655 Name == "strncat" ||
1656 Name == "strncpy" ||
1657 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001658 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001659 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001660 continue;
1661 setDoesNotThrow(F);
1662 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001663 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001664 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001665 !FTy->getParamType(0)->isPointerTy() ||
1666 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001667 continue;
1668 setDoesNotThrow(F);
1669 setDoesNotCapture(F, 1);
1670 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001671 } else if (Name == "strcmp" ||
1672 Name == "strspn" ||
1673 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001674 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001675 Name == "strcoll" ||
1676 Name == "strcasecmp" ||
1677 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001678 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001679 !FTy->getParamType(0)->isPointerTy() ||
1680 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001681 continue;
1682 setOnlyReadsMemory(F);
1683 setDoesNotThrow(F);
1684 setDoesNotCapture(F, 1);
1685 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001686 } else if (Name == "strstr" ||
1687 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001688 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001689 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001690 continue;
1691 setOnlyReadsMemory(F);
1692 setDoesNotThrow(F);
1693 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001694 } else if (Name == "strtok" ||
1695 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001696 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001697 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001698 continue;
1699 setDoesNotThrow(F);
1700 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001701 } else if (Name == "scanf" ||
1702 Name == "setbuf" ||
1703 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001704 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001705 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001706 continue;
1707 setDoesNotThrow(F);
1708 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001709 } else if (Name == "strdup" ||
1710 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001711 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001712 !FTy->getReturnType()->isPointerTy() ||
1713 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001714 continue;
1715 setDoesNotThrow(F);
1716 setDoesNotAlias(F, 0);
1717 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001718 } else if (Name == "stat" ||
1719 Name == "sscanf" ||
1720 Name == "sprintf" ||
1721 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001722 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001723 !FTy->getParamType(0)->isPointerTy() ||
1724 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001725 continue;
1726 setDoesNotThrow(F);
1727 setDoesNotCapture(F, 1);
1728 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001729 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001730 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001731 !FTy->getParamType(0)->isPointerTy() ||
1732 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001733 continue;
1734 setDoesNotThrow(F);
1735 setDoesNotCapture(F, 1);
1736 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001737 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001738 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001739 !FTy->getParamType(1)->isPointerTy() ||
1740 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001741 continue;
1742 setDoesNotThrow(F);
1743 setDoesNotCapture(F, 2);
1744 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001745 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001746 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001747 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001748 continue;
1749 // May throw; "system" is a valid pthread cancellation point.
1750 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001751 }
1752 break;
1753 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001754 if (Name == "malloc") {
1755 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001756 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001757 continue;
1758 setDoesNotThrow(F);
1759 setDoesNotAlias(F, 0);
1760 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001761 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001762 !FTy->getParamType(0)->isPointerTy() ||
1763 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001764 continue;
1765 setOnlyReadsMemory(F);
1766 setDoesNotThrow(F);
1767 setDoesNotCapture(F, 1);
1768 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001769 } else if (Name == "memchr" ||
1770 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001771 if (FTy->getNumParams() != 3)
1772 continue;
1773 setOnlyReadsMemory(F);
1774 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001775 } else if (Name == "modf" ||
1776 Name == "modff" ||
1777 Name == "modfl" ||
1778 Name == "memcpy" ||
1779 Name == "memccpy" ||
1780 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001781 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001782 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001783 continue;
1784 setDoesNotThrow(F);
1785 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001786 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001787 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001788 continue;
1789 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001790 } else if (Name == "mkdir" ||
1791 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001792 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001793 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001794 continue;
1795 setDoesNotThrow(F);
1796 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001797 }
1798 break;
1799 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001800 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001801 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001802 !FTy->getParamType(0)->isPointerTy() ||
1803 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001804 continue;
1805 setDoesNotThrow(F);
1806 setDoesNotAlias(F, 0);
1807 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001808 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001809 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001810 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001811 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001812 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001813 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001814 } else if (Name == "rmdir" ||
1815 Name == "rewind" ||
1816 Name == "remove" ||
1817 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001818 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001819 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001820 continue;
1821 setDoesNotThrow(F);
1822 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001823 } else if (Name == "rename" ||
1824 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001825 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001826 !FTy->getParamType(0)->isPointerTy() ||
1827 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001828 continue;
1829 setDoesNotThrow(F);
1830 setDoesNotCapture(F, 1);
1831 setDoesNotCapture(F, 2);
1832 }
1833 break;
1834 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001835 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001836 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001837 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001838 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001839 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001840 setDoesNotCapture(F, 2);
1841 }
1842 break;
1843 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001844 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001845 if (FTy->getNumParams() != 3 ||
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);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001852 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001853 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001854 !FTy->getParamType(0)->isPointerTy() ||
1855 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001856 continue;
1857 setDoesNotThrow(F);
1858 setOnlyReadsMemory(F);
1859 setDoesNotCapture(F, 1);
1860 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001861 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001862 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001863 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001864 continue;
1865 setDoesNotThrow(F);
1866 setDoesNotCapture(F, 1);
1867 }
1868 break;
1869 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001870 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001871 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001872 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001873 continue;
1874 setDoesNotThrow(F);
1875 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001876 } else if (Name == "chmod" ||
1877 Name == "chown" ||
1878 Name == "ctermid" ||
1879 Name == "clearerr" ||
1880 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001881 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001882 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001883 continue;
1884 setDoesNotThrow(F);
1885 setDoesNotCapture(F, 1);
1886 }
1887 break;
1888 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001889 if (Name == "atoi" ||
1890 Name == "atol" ||
1891 Name == "atof" ||
1892 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001893 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001894 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001895 continue;
1896 setDoesNotThrow(F);
1897 setOnlyReadsMemory(F);
1898 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001899 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001900 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001901 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001902 continue;
1903 setDoesNotThrow(F);
1904 setDoesNotCapture(F, 1);
1905 }
1906 break;
1907 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001908 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001909 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001910 !FTy->getReturnType()->isPointerTy() ||
1911 !FTy->getParamType(0)->isPointerTy() ||
1912 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001913 continue;
1914 setDoesNotThrow(F);
1915 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001916 setDoesNotCapture(F, 1);
1917 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001918 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001919 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001920 !FTy->getReturnType()->isPointerTy() ||
1921 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001922 continue;
1923 setDoesNotThrow(F);
1924 setDoesNotAlias(F, 0);
1925 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001926 } else if (Name == "feof" ||
1927 Name == "free" ||
1928 Name == "fseek" ||
1929 Name == "ftell" ||
1930 Name == "fgetc" ||
1931 Name == "fseeko" ||
1932 Name == "ftello" ||
1933 Name == "fileno" ||
1934 Name == "fflush" ||
1935 Name == "fclose" ||
1936 Name == "fsetpos" ||
1937 Name == "flockfile" ||
1938 Name == "funlockfile" ||
1939 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001940 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001941 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001942 continue;
1943 setDoesNotThrow(F);
1944 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001945 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001946 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001947 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001948 continue;
1949 setDoesNotThrow(F);
1950 setDoesNotCapture(F, 1);
1951 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001952 } else if (Name == "fputc" ||
1953 Name == "fstat" ||
1954 Name == "frexp" ||
1955 Name == "frexpf" ||
1956 Name == "frexpl" ||
1957 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001958 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001959 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001960 continue;
1961 setDoesNotThrow(F);
1962 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001963 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001964 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001965 !FTy->getParamType(0)->isPointerTy() ||
1966 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001967 continue;
1968 setDoesNotThrow(F);
1969 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001970 } else if (Name == "fread" ||
1971 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001972 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001973 !FTy->getParamType(0)->isPointerTy() ||
1974 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001975 continue;
1976 setDoesNotThrow(F);
1977 setDoesNotCapture(F, 1);
1978 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001979 } else if (Name == "fputs" ||
1980 Name == "fscanf" ||
1981 Name == "fprintf" ||
1982 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001983 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001984 !FTy->getParamType(0)->isPointerTy() ||
1985 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001986 continue;
1987 setDoesNotThrow(F);
1988 setDoesNotCapture(F, 1);
1989 setDoesNotCapture(F, 2);
1990 }
1991 break;
1992 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001993 if (Name == "getc" ||
1994 Name == "getlogin_r" ||
1995 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001996 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001997 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001998 continue;
1999 setDoesNotThrow(F);
2000 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002001 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002002 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002003 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002004 continue;
2005 setDoesNotThrow(F);
2006 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002007 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002008 } else if (Name == "gets" ||
2009 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002010 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002011 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002012 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002013 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002014 continue;
2015 setDoesNotThrow(F);
2016 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002017 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002018 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002019 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002020 continue;
2021 setDoesNotThrow(F);
2022 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002023 }
2024 break;
2025 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002026 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002027 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002028 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002029 continue;
2030 setDoesNotThrow(F);
2031 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002032 } else if (Name == "uname" ||
2033 Name == "unlink" ||
2034 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002035 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002036 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002037 continue;
2038 setDoesNotThrow(F);
2039 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002040 } else if (Name == "utime" ||
2041 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002042 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002043 !FTy->getParamType(0)->isPointerTy() ||
2044 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002045 continue;
2046 setDoesNotThrow(F);
2047 setDoesNotCapture(F, 1);
2048 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002049 }
2050 break;
2051 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002052 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002053 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002054 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002055 continue;
2056 setDoesNotThrow(F);
2057 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002058 } else if (Name == "puts" ||
2059 Name == "printf" ||
2060 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002061 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002062 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002063 continue;
2064 setDoesNotThrow(F);
2065 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002066 } else if (Name == "pread" ||
2067 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002068 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002069 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002070 continue;
2071 // May throw; these are valid pthread cancellation points.
2072 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002073 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002074 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002075 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002076 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002077 !FTy->getReturnType()->isPointerTy() ||
2078 !FTy->getParamType(0)->isPointerTy() ||
2079 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002080 continue;
2081 setDoesNotThrow(F);
2082 setDoesNotAlias(F, 0);
2083 setDoesNotCapture(F, 1);
2084 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002085 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002086 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002087 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002088 continue;
2089 setDoesNotThrow(F);
2090 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002091 }
2092 break;
2093 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002094 if (Name == "vscanf") {
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 Lewycky0f8df9a2009-01-04 20:27:34 +00002097 continue;
2098 setDoesNotThrow(F);
2099 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002100 } else if (Name == "vsscanf" ||
2101 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002102 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002103 !FTy->getParamType(1)->isPointerTy() ||
2104 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002105 continue;
2106 setDoesNotThrow(F);
2107 setDoesNotCapture(F, 1);
2108 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002109 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00002110 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002111 continue;
2112 setDoesNotThrow(F);
2113 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002114 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002115 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002116 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002117 continue;
2118 setDoesNotThrow(F);
2119 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002120 } else if (Name == "vfprintf" ||
2121 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002122 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002123 !FTy->getParamType(0)->isPointerTy() ||
2124 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002125 continue;
2126 setDoesNotThrow(F);
2127 setDoesNotCapture(F, 1);
2128 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002129 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002130 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002131 !FTy->getParamType(0)->isPointerTy() ||
2132 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002133 continue;
2134 setDoesNotThrow(F);
2135 setDoesNotCapture(F, 1);
2136 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002137 }
2138 break;
2139 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002140 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002141 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002142 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002143 continue;
2144 // May throw; "open" is a valid pthread cancellation point.
2145 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002146 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002147 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002148 !FTy->getReturnType()->isPointerTy() ||
2149 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002150 continue;
2151 setDoesNotThrow(F);
2152 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002153 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002154 }
2155 break;
2156 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002157 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00002158 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002159 continue;
2160 setDoesNotThrow(F);
2161 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002162 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002163 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002164 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002165 continue;
2166 setDoesNotThrow(F);
2167 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002168 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002169 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002170 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002171 if (Name == "htonl" ||
2172 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002173 setDoesNotThrow(F);
2174 setDoesNotAccessMemory(F);
2175 }
2176 break;
2177 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002178 if (Name == "ntohl" ||
2179 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002180 setDoesNotThrow(F);
2181 setDoesNotAccessMemory(F);
2182 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002183 break;
2184 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002185 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002186 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002187 !FTy->getParamType(0)->isPointerTy() ||
2188 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002189 continue;
2190 setDoesNotThrow(F);
2191 setDoesNotCapture(F, 1);
2192 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002193 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002194 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002195 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002196 continue;
2197 setDoesNotThrow(F);
2198 setDoesNotCapture(F, 1);
2199 }
2200 break;
2201 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002202 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002203 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002204 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002205 continue;
2206 // May throw; places call through function pointer.
2207 setDoesNotCapture(F, 4);
2208 }
2209 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002210 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002211 if (Name == "__strdup" ||
2212 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002213 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002214 !FTy->getReturnType()->isPointerTy() ||
2215 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002216 continue;
2217 setDoesNotThrow(F);
2218 setDoesNotAlias(F, 0);
2219 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002220 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002221 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002222 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002223 continue;
2224 setDoesNotThrow(F);
2225 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002226 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002227 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002228 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002229 continue;
2230 setDoesNotThrow(F);
2231 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002232 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002233 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002234 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002235 continue;
2236 setDoesNotThrow(F);
2237 setDoesNotCapture(F, 2);
2238 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002239 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002240 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002241 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002242 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002243 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002244 continue;
2245 setDoesNotThrow(F);
2246 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002247 } else if (Name == "\1stat64" ||
2248 Name == "\1lstat64" ||
2249 Name == "\1statvfs64" ||
2250 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002251 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002252 !FTy->getParamType(0)->isPointerTy() ||
2253 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002254 continue;
2255 setDoesNotThrow(F);
2256 setDoesNotCapture(F, 1);
2257 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002258 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002259 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002260 !FTy->getReturnType()->isPointerTy() ||
2261 !FTy->getParamType(0)->isPointerTy() ||
2262 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002263 continue;
2264 setDoesNotThrow(F);
2265 setDoesNotAlias(F, 0);
2266 setDoesNotCapture(F, 1);
2267 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002268 } else if (Name == "\1fseeko64" ||
2269 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002270 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002271 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002272 continue;
2273 setDoesNotThrow(F);
2274 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002275 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002276 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002277 continue;
2278 setDoesNotThrow(F);
2279 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002280 } else if (Name == "\1fstat64" ||
2281 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002282 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002283 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002284 continue;
2285 setDoesNotThrow(F);
2286 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002287 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002288 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002289 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002290 continue;
2291 // May throw; "open" is a valid pthread cancellation point.
2292 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002293 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002294 break;
2295 }
2296 }
2297 return Modified;
2298}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002299
2300// TODO:
2301// Additional cases that we need to add to this file:
2302//
2303// cbrt:
2304// * cbrt(expN(X)) -> expN(x/3)
2305// * cbrt(sqrt(x)) -> pow(x,1/6)
2306// * cbrt(sqrt(x)) -> pow(x,1/9)
2307//
2308// cos, cosf, cosl:
2309// * cos(-x) -> cos(x)
2310//
2311// exp, expf, expl:
2312// * exp(log(x)) -> x
2313//
2314// log, logf, logl:
2315// * log(exp(x)) -> x
2316// * log(x**y) -> y*log(x)
2317// * log(exp(y)) -> y*log(e)
2318// * log(exp2(y)) -> y*log(2)
2319// * log(exp10(y)) -> y*log(10)
2320// * log(sqrt(x)) -> 0.5*log(x)
2321// * log(pow(x,y)) -> y*log(x)
2322//
2323// lround, lroundf, lroundl:
2324// * lround(cnst) -> cnst'
2325//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002326// pow, powf, powl:
2327// * pow(exp(x),y) -> exp(x*y)
2328// * pow(sqrt(x),y) -> pow(x,y*0.5)
2329// * pow(pow(x,y),z)-> pow(x,y*z)
2330//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002331// round, roundf, roundl:
2332// * round(cnst) -> cnst'
2333//
2334// signbit:
2335// * signbit(cnst) -> cnst'
2336// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2337//
2338// sqrt, sqrtf, sqrtl:
2339// * sqrt(expN(x)) -> expN(x*0.5)
2340// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2341// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2342//
2343// stpcpy:
2344// * stpcpy(str, "literal") ->
2345// llvm.memcpy(str,"literal",strlen("literal")+1,1)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002346//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002347// tan, tanf, tanl:
2348// * tan(atan(x)) -> x
2349//
2350// trunc, truncf, truncl:
2351// * trunc(cnst) -> cnst'
2352//
2353//