blob: fc23c420cc8fa86ce9c8f6185aae83dc50aab38a [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"
20#include "llvm/Intrinsics.h"
Owen Andersonfa5cbd62009-07-03 19:42:02 +000021#include "llvm/LLVMContext.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000022#include "llvm/Module.h"
23#include "llvm/Pass.h"
24#include "llvm/Support/IRBuilder.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000025#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/ADT/SmallPtrSet.h"
28#include "llvm/ADT/StringMap.h"
29#include "llvm/ADT/Statistic.h"
Daniel Dunbar473955f2009-07-29 22:00:43 +000030#include "llvm/ADT/STLExtras.h"
Chris Lattner56b4f2b2008-05-01 06:39:12 +000031#include "llvm/Support/Debug.h"
Daniel Dunbarf0443c12009-07-26 08:34:35 +000032#include "llvm/Support/raw_ostream.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000033#include "llvm/Config/config.h"
34using namespace llvm;
35
36STATISTIC(NumSimplified, "Number of library calls simplified");
Nick Lewycky0f8df9a2009-01-04 20:27:34 +000037STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000038
39//===----------------------------------------------------------------------===//
40// Optimizer Base Class
41//===----------------------------------------------------------------------===//
42
43/// This class is the abstract base class for the set of optimizations that
44/// corresponds to one library call.
45namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +000046class LibCallOptimization {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000047protected:
48 Function *Caller;
49 const TargetData *TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000050 LLVMContext* Context;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000051public:
52 LibCallOptimization() { }
53 virtual ~LibCallOptimization() {}
54
55 /// CallOptimizer - This pure virtual method is implemented by base classes to
56 /// do various optimizations. If this returns null then no transformation was
57 /// performed. If it returns CI, then it transformed the call and CI is to be
58 /// deleted. If it returns something else, replace CI with the new value and
59 /// delete CI.
Eric Christopher37c8b862009-10-07 21:14:25 +000060 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
Eric Christopher7a61d702008-08-08 19:39:37 +000061 =0;
Eric Christopher37c8b862009-10-07 21:14:25 +000062
Dan Gohmanf14d9192009-08-18 00:48:13 +000063 Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000064 Caller = CI->getParent()->getParent();
Dan Gohmanf14d9192009-08-18 00:48:13 +000065 this->TD = TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000066 if (CI->getCalledFunction())
Owen Andersone922c022009-07-22 00:24:57 +000067 Context = &CI->getCalledFunction()->getContext();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000068 return CallOptimizer(CI->getCalledFunction(), CI, B);
69 }
70
71 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Eric Christopher7a61d702008-08-08 19:39:37 +000072 Value *CastToCStr(Value *V, IRBuilder<> &B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000073
74 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
75 /// specified pointer. Ptr is required to be some pointer type, and the
76 /// return value has 'intptr_t' type.
Eric Christopher7a61d702008-08-08 19:39:37 +000077 Value *EmitStrLen(Value *Ptr, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +000078
Chris Lattner24604112009-12-16 09:32:05 +000079 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
80 /// specified pointer and character. Ptr is required to be some pointer type,
81 /// and the return value has 'i8*' type.
82 Value *EmitStrChr(Value *Ptr, char C, IRBuilder<> &B);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +000083
Eric Christopher7672d042010-01-23 05:29:06 +000084 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
85 /// specified pointer arguments.
86 Value *EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B);
87
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000088 /// EmitMemCpy - Emit a call to the memcpy function to the builder. This
89 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
Eric Christopher37c8b862009-10-07 21:14:25 +000090 Value *EmitMemCpy(Value *Dst, Value *Src, Value *Len,
Eric Christopher7a61d702008-08-08 19:39:37 +000091 unsigned Align, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +000092
Eric Christopher80bf1d52009-11-21 01:01:30 +000093 /// EmitMemMove - Emit a call to the memmove function to the builder. This
94 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
95 Value *EmitMemMove(Value *Dst, Value *Src, Value *Len,
96 unsigned Align, IRBuilder<> &B);
97
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000098 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
99 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
Eric Christopher7a61d702008-08-08 19:39:37 +0000100 Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000101
102 /// EmitMemCmp - Emit a call to the memcmp function.
103 Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B);
104
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000105 /// EmitMemSet - Emit a call to the memset function
106 Value *EmitMemSet(Value *Dst, Value *Val, Value *Len, IRBuilder<> &B);
107
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000108 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name'
109 /// (e.g. 'floor'). This function is known to take a single of type matching
110 /// 'Op' and returns one value with the same type. If 'Op' is a long double,
111 /// 'l' is added as the suffix of name, if 'Op' is a float, we add a 'f'
112 /// suffix.
Dan Gohman79cb8402009-09-25 23:10:17 +0000113 Value *EmitUnaryFloatFnCall(Value *Op, const char *Name, IRBuilder<> &B,
114 const AttrListPtr &Attrs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000115
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000116 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
117 /// is an integer.
Chris Lattner74965f22009-11-09 04:57:04 +0000118 Value *EmitPutChar(Value *Char, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000119
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000120 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
121 /// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000122 void EmitPutS(Value *Str, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000123
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000124 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
125 /// an i32, and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000126 void EmitFPutC(Value *Char, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000127
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000128 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
129 /// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000130 void EmitFPutS(Value *Str, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000131
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000132 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
133 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000134 void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000135
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000136};
137} // End anonymous namespace.
138
139/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Eric Christopher7a61d702008-08-08 19:39:37 +0000140Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) {
Chris Lattnerbf796322009-12-02 06:05:42 +0000141 return B.CreateBitCast(V, Type::getInt8PtrTy(*Context), "cstr");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000142}
143
144/// EmitStrLen - Emit a call to the strlen function to the builder, for the
145/// specified pointer. This always returns an integer value of size intptr_t.
Eric Christopher7a61d702008-08-08 19:39:37 +0000146Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000147 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000148 AttributeWithIndex AWI[2];
149 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
150 AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
151 Attribute::NoUnwind);
152
153 Constant *StrLen =M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000154 TD->getIntPtrType(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000155 Type::getInt8PtrTy(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000156 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000157 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
158 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
159 CI->setCallingConv(F->getCallingConv());
160
161 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000162}
163
Chris Lattner24604112009-12-16 09:32:05 +0000164/// EmitStrChr - Emit a call to the strchr function to the builder, for the
165/// specified pointer and character. Ptr is required to be some pointer type,
166/// and the return value has 'i8*' type.
167Value *LibCallOptimization::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B) {
168 Module *M = Caller->getParent();
169 AttributeWithIndex AWI =
170 AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000171
Chris Lattner24604112009-12-16 09:32:05 +0000172 const Type *I8Ptr = Type::getInt8PtrTy(*Context);
173 const Type *I32Ty = Type::getInt32Ty(*Context);
174 Constant *StrChr = M->getOrInsertFunction("strchr", AttrListPtr::get(&AWI, 1),
175 I8Ptr, I8Ptr, I32Ty, NULL);
176 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
177 ConstantInt::get(I32Ty, C), "strchr");
178 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
179 CI->setCallingConv(F->getCallingConv());
180 return CI;
181}
182
Eric Christopher7672d042010-01-23 05:29:06 +0000183/// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
184/// specified pointer arguments.
185Value *LibCallOptimization::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B) {
186 Module *M = Caller->getParent();
187 AttributeWithIndex AWI[2];
188 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
189 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
190 const Type *I8Ptr = Type::getInt8PtrTy(*Context);
191 Value *StrCpy = M->getOrInsertFunction("strcpy", AttrListPtr::get(AWI, 2),
192 I8Ptr, I8Ptr, I8Ptr, NULL);
193 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
194 "strcpy");
195 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
196 CI->setCallingConv(F->getCallingConv());
197 return CI;
198}
Chris Lattner24604112009-12-16 09:32:05 +0000199
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000200/// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
201/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
202Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
Eric Christopher7a61d702008-08-08 19:39:37 +0000203 unsigned Align, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000204 Module *M = Caller->getParent();
Chris Lattnerbf796322009-12-02 06:05:42 +0000205 const Type *Ty = Len->getType();
206 Value *MemCpy = Intrinsic::getDeclaration(M, Intrinsic::memcpy, &Ty, 1);
207 Dst = CastToCStr(Dst, B);
208 Src = CastToCStr(Src, B);
209 return B.CreateCall4(MemCpy, Dst, Src, Len,
Owen Anderson1d0be152009-08-13 21:58:54 +0000210 ConstantInt::get(Type::getInt32Ty(*Context), Align));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000211}
212
Chris Lattnerbf796322009-12-02 06:05:42 +0000213/// EmitMemMove - Emit a call to the memmove function to the builder. This
Eric Christopher80bf1d52009-11-21 01:01:30 +0000214/// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
215Value *LibCallOptimization::EmitMemMove(Value *Dst, Value *Src, Value *Len,
216 unsigned Align, IRBuilder<> &B) {
217 Module *M = Caller->getParent();
Chris Lattnerbf796322009-12-02 06:05:42 +0000218 const Type *Ty = TD->getIntPtrType(*Context);
219 Value *MemMove = Intrinsic::getDeclaration(M, Intrinsic::memmove, &Ty, 1);
220 Dst = CastToCStr(Dst, B);
221 Src = CastToCStr(Src, B);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000222 Value *A = ConstantInt::get(Type::getInt32Ty(*Context), Align);
Chris Lattnerbf796322009-12-02 06:05:42 +0000223 return B.CreateCall4(MemMove, Dst, Src, Len, A);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000224}
225
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000226/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
227/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
228Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
Eric Christopher7a61d702008-08-08 19:39:37 +0000229 Value *Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000230 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000231 AttributeWithIndex AWI;
232 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
233
234 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
Eric Christopher37c8b862009-10-07 21:14:25 +0000235 Type::getInt8PtrTy(*Context),
236 Type::getInt8PtrTy(*Context),
237 Type::getInt32Ty(*Context),
238 TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000239 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000240 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
241
242 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
243 CI->setCallingConv(F->getCallingConv());
244
245 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000246}
247
Nick Lewycky13a09e22008-12-21 00:19:21 +0000248/// EmitMemCmp - Emit a call to the memcmp function.
249Value *LibCallOptimization::EmitMemCmp(Value *Ptr1, Value *Ptr2,
250 Value *Len, IRBuilder<> &B) {
251 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000252 AttributeWithIndex AWI[3];
253 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
254 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
255 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
256 Attribute::NoUnwind);
257
258 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000259 Type::getInt32Ty(*Context),
Mikhail Glushenkov0ecbdeb2010-01-06 09:20:39 +0000260 Type::getInt8PtrTy(*Context),
261 Type::getInt8PtrTy(*Context),
Owen Anderson1d0be152009-08-13 21:58:54 +0000262 TD->getIntPtrType(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000263 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
264 Len, "memcmp");
265
266 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
267 CI->setCallingConv(F->getCallingConv());
268
269 return CI;
Nick Lewycky13a09e22008-12-21 00:19:21 +0000270}
271
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000272/// EmitMemSet - Emit a call to the memset function
273Value *LibCallOptimization::EmitMemSet(Value *Dst, Value *Val,
274 Value *Len, IRBuilder<> &B) {
275 Module *M = Caller->getParent();
276 Intrinsic::ID IID = Intrinsic::memset;
277 const Type *Tys[1];
278 Tys[0] = Len->getType();
279 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000280 Value *Align = ConstantInt::get(Type::getInt32Ty(*Context), 1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000281 return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
282}
283
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000284/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
285/// 'floor'). This function is known to take a single of type matching 'Op' and
286/// returns one value with the same type. If 'Op' is a long double, 'l' is
287/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
288Value *LibCallOptimization::EmitUnaryFloatFnCall(Value *Op, const char *Name,
Dan Gohman79cb8402009-09-25 23:10:17 +0000289 IRBuilder<> &B,
290 const AttrListPtr &Attrs) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000291 char NameBuffer[20];
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000292 if (!Op->getType()->isDoubleTy()) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000293 // If we need to add a suffix, copy into NameBuffer.
294 unsigned NameLen = strlen(Name);
295 assert(NameLen < sizeof(NameBuffer)-2);
296 memcpy(NameBuffer, Name, NameLen);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000297 if (Op->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000298 NameBuffer[NameLen] = 'f'; // floorf
299 else
300 NameBuffer[NameLen] = 'l'; // floorl
301 NameBuffer[NameLen+1] = 0;
302 Name = NameBuffer;
303 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000304
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000305 Module *M = Caller->getParent();
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000306 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000307 Op->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000308 CallInst *CI = B.CreateCall(Callee, Op, Name);
Dan Gohman79cb8402009-09-25 23:10:17 +0000309 CI->setAttributes(Attrs);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000310 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
311 CI->setCallingConv(F->getCallingConv());
312
313 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000314}
315
316/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
317/// is an integer.
Chris Lattner74965f22009-11-09 04:57:04 +0000318Value *LibCallOptimization::EmitPutChar(Value *Char, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000319 Module *M = Caller->getParent();
Owen Anderson1d0be152009-08-13 21:58:54 +0000320 Value *PutChar = M->getOrInsertFunction("putchar", Type::getInt32Ty(*Context),
321 Type::getInt32Ty(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000322 CallInst *CI = B.CreateCall(PutChar,
Eric Christopher37c8b862009-10-07 21:14:25 +0000323 B.CreateIntCast(Char,
324 Type::getInt32Ty(*Context),
Duncan Sandsf63c4102009-11-16 12:32:28 +0000325 /*isSigned*/true,
Eric Christopher37c8b862009-10-07 21:14:25 +0000326 "chari"),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000327 "putchar");
328
329 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
330 CI->setCallingConv(F->getCallingConv());
Chris Lattner74965f22009-11-09 04:57:04 +0000331 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000332}
333
334/// EmitPutS - Emit a call to the puts function. This assumes that Str is
335/// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000336void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000337 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000338 AttributeWithIndex AWI[2];
339 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
340 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
341
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000342 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000343 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000344 Type::getInt8PtrTy(*Context),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000345 NULL);
346 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
347 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
348 CI->setCallingConv(F->getCallingConv());
349
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000350}
351
352/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
353/// an integer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000354void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000355 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000356 AttributeWithIndex AWI[2];
357 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
358 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
359 Constant *F;
360 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000361 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
362 Type::getInt32Ty(*Context),
363 Type::getInt32Ty(*Context), File->getType(),
364 NULL);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000365 else
Eric Christopher37c8b862009-10-07 21:14:25 +0000366 F = M->getOrInsertFunction("fputc",
367 Type::getInt32Ty(*Context),
368 Type::getInt32Ty(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000369 File->getType(), NULL);
Duncan Sandsf63c4102009-11-16 12:32:28 +0000370 Char = B.CreateIntCast(Char, Type::getInt32Ty(*Context), /*isSigned*/true,
371 "chari");
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000372 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
373
374 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
375 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000376}
377
378/// EmitFPutS - Emit a call to the puts function. Str is required to be a
379/// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000380void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000381 Module *M = Caller->getParent();
Nick Lewycky225f7472009-02-15 22:47:25 +0000382 AttributeWithIndex AWI[3];
383 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
384 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
385 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000386 Constant *F;
387 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000388 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
389 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000390 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000391 File->getType(), NULL);
392 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000393 F = M->getOrInsertFunction("fputs", Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000394 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000395 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000396 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
397
398 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
399 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000400}
401
402/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
403/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
404void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Eric Christopher7a61d702008-08-08 19:39:37 +0000405 IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000406 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000407 AttributeWithIndex AWI[3];
408 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
409 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
410 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
411 Constant *F;
412 if (isa<PointerType>(File->getType()))
413 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000414 TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000415 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000416 TD->getIntPtrType(*Context),
417 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000418 File->getType(), NULL);
419 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000420 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000421 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000422 TD->getIntPtrType(*Context),
423 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000424 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000425 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Owen Anderson1d0be152009-08-13 21:58:54 +0000426 ConstantInt::get(TD->getIntPtrType(*Context), 1), File);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000427
428 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
429 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000430}
431
432//===----------------------------------------------------------------------===//
433// Helper Functions
434//===----------------------------------------------------------------------===//
435
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000436/// GetStringLengthH - If we can compute the length of the string pointed to by
437/// the specified pointer, return 'len+1'. If we can't, return 0.
438static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
439 // Look through noop bitcast instructions.
440 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
441 return GetStringLengthH(BCI->getOperand(0), PHIs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000442
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000443 // If this is a PHI node, there are two cases: either we have already seen it
444 // or we haven't.
445 if (PHINode *PN = dyn_cast<PHINode>(V)) {
446 if (!PHIs.insert(PN))
447 return ~0ULL; // already in the set.
Eric Christopher37c8b862009-10-07 21:14:25 +0000448
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000449 // If it was new, see if all the input strings are the same length.
450 uint64_t LenSoFar = ~0ULL;
451 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
452 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
453 if (Len == 0) return 0; // Unknown length -> unknown.
Eric Christopher37c8b862009-10-07 21:14:25 +0000454
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000455 if (Len == ~0ULL) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +0000456
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000457 if (Len != LenSoFar && LenSoFar != ~0ULL)
458 return 0; // Disagree -> unknown.
459 LenSoFar = Len;
460 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000461
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000462 // Success, all agree.
463 return LenSoFar;
464 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000465
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000466 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
467 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
468 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
469 if (Len1 == 0) return 0;
470 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
471 if (Len2 == 0) return 0;
472 if (Len1 == ~0ULL) return Len2;
473 if (Len2 == ~0ULL) return Len1;
474 if (Len1 != Len2) return 0;
475 return Len1;
476 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000477
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000478 // If the value is not a GEP instruction nor a constant expression with a
479 // GEP instruction, then return unknown.
480 User *GEP = 0;
481 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
482 GEP = GEPI;
483 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
484 if (CE->getOpcode() != Instruction::GetElementPtr)
485 return 0;
486 GEP = CE;
487 } else {
488 return 0;
489 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000490
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000491 // Make sure the GEP has exactly three arguments.
492 if (GEP->getNumOperands() != 3)
493 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000494
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000495 // Check to make sure that the first operand of the GEP is an integer and
496 // has value 0 so that we are sure we're indexing into the initializer.
497 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
498 if (!Idx->isZero())
499 return 0;
500 } else
501 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000502
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000503 // If the second index isn't a ConstantInt, then this is a variable index
504 // into the array. If this occurs, we can't say anything meaningful about
505 // the string.
506 uint64_t StartIdx = 0;
507 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
508 StartIdx = CI->getZExtValue();
509 else
510 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000511
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000512 // The GEP instruction, constant or instruction, must reference a global
513 // variable that is a constant and is initialized. The referenced constant
514 // initializer is the array that we'll use for optimization.
515 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
Dan Gohman107f41f2009-08-19 00:11:12 +0000516 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
517 GV->mayBeOverridden())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000518 return 0;
519 Constant *GlobalInit = GV->getInitializer();
Eric Christopher37c8b862009-10-07 21:14:25 +0000520
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000521 // Handle the ConstantAggregateZero case, which is a degenerate case. The
522 // initializer is constant zero so the length of the string must be zero.
523 if (isa<ConstantAggregateZero>(GlobalInit))
524 return 1; // Len = 0 offset by 1.
Eric Christopher37c8b862009-10-07 21:14:25 +0000525
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000526 // Must be a Constant Array
527 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +0000528 if (!Array || !Array->getType()->getElementType()->isInteger(8))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000529 return false;
Eric Christopher37c8b862009-10-07 21:14:25 +0000530
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000531 // Get the number of elements in the array
532 uint64_t NumElts = Array->getType()->getNumElements();
Eric Christopher37c8b862009-10-07 21:14:25 +0000533
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000534 // Traverse the constant array from StartIdx (derived above) which is
535 // the place the GEP refers to in the array.
536 for (unsigned i = StartIdx; i != NumElts; ++i) {
537 Constant *Elt = Array->getOperand(i);
538 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
539 if (!CI) // This array isn't suitable, non-int initializer.
540 return 0;
541 if (CI->isZero())
542 return i-StartIdx+1; // We found end of string, success!
543 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000544
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000545 return 0; // The array isn't null terminated, conservatively return 'unknown'.
546}
547
548/// GetStringLength - If we can compute the length of the string pointed to by
549/// the specified pointer, return 'len+1'. If we can't, return 0.
550static uint64_t GetStringLength(Value *V) {
551 if (!isa<PointerType>(V->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000552
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000553 SmallPtrSet<PHINode*, 32> PHIs;
554 uint64_t Len = GetStringLengthH(V, PHIs);
555 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
556 // an empty string as a length.
557 return Len == ~0ULL ? 1 : Len;
558}
559
560/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +0000561/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000562static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
563 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
564 UI != E; ++UI) {
565 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
566 if (IC->isEquality())
567 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
568 if (C->isNullValue())
569 continue;
570 // Unknown instruction.
571 return false;
572 }
573 return true;
574}
575
576//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000577// String and Memory LibCall Optimizations
578//===----------------------------------------------------------------------===//
579
580//===---------------------------------------===//
581// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000582namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000583struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000584 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000585 // Verify the "strcat" function prototype.
586 const FunctionType *FT = Callee->getFunctionType();
587 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000588 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000589 FT->getParamType(0) != FT->getReturnType() ||
590 FT->getParamType(1) != FT->getReturnType())
591 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000592
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000593 // Extract some information from the instruction
594 Value *Dst = CI->getOperand(1);
595 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000596
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000597 // See if we can get the length of the input string.
598 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000599 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000600 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000601
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000602 // Handle the simple, do-nothing case: strcat(x, "") -> x
603 if (Len == 0)
604 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000605
606 // These optimizations require TargetData.
607 if (!TD) return 0;
608
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000609 EmitStrLenMemCpy(Src, Dst, Len, B);
610 return Dst;
611 }
612
613 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000614 // We need to find the end of the destination string. That's where the
615 // memory is to be moved to. We just generate a call to strlen.
616 Value *DstLen = EmitStrLen(Dst, B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000617
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000618 // Now that we have the destination's length, we must index into the
619 // destination's pointer to get the actual memcpy destination (end of
620 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000621 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000622
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000623 // We have enough information to now generate the memcpy call to do the
624 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000625 EmitMemCpy(CpyDst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000626 ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000627 }
628};
629
630//===---------------------------------------===//
631// 'strncat' Optimizations
632
Chris Lattner3e8b6632009-09-02 06:11:42 +0000633struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000634 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
635 // Verify the "strncat" function prototype.
636 const FunctionType *FT = Callee->getFunctionType();
637 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000638 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000639 FT->getParamType(0) != FT->getReturnType() ||
640 FT->getParamType(1) != FT->getReturnType() ||
641 !isa<IntegerType>(FT->getParamType(2)))
642 return 0;
643
644 // Extract some information from the instruction
645 Value *Dst = CI->getOperand(1);
646 Value *Src = CI->getOperand(2);
647 uint64_t Len;
648
649 // We don't do anything if length is not constant
650 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
651 Len = LengthArg->getZExtValue();
652 else
653 return 0;
654
655 // See if we can get the length of the input string.
656 uint64_t SrcLen = GetStringLength(Src);
657 if (SrcLen == 0) return 0;
658 --SrcLen; // Unbias length.
659
660 // Handle the simple, do-nothing cases:
661 // strncat(x, "", c) -> x
662 // strncat(x, c, 0) -> x
663 if (SrcLen == 0 || Len == 0) return Dst;
664
Dan Gohmanf14d9192009-08-18 00:48:13 +0000665 // These optimizations require TargetData.
666 if (!TD) return 0;
667
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000668 // We don't optimize this case
669 if (Len < SrcLen) return 0;
670
671 // strncat(x, s, c) -> strcat(x, s)
672 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000673 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000674 return Dst;
675 }
676};
677
678//===---------------------------------------===//
679// 'strchr' Optimizations
680
Chris Lattner3e8b6632009-09-02 06:11:42 +0000681struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000682 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000683 // Verify the "strchr" function prototype.
684 const FunctionType *FT = Callee->getFunctionType();
685 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000686 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000687 FT->getParamType(0) != FT->getReturnType())
688 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000689
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000690 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000691
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000692 // If the second operand is non-constant, see if we can compute the length
693 // of the input string and turn this into memchr.
694 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
695 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000696 // These optimizations require TargetData.
697 if (!TD) return 0;
698
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000699 uint64_t Len = GetStringLength(SrcStr);
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +0000700 if (Len == 0 || !FT->getParamType(1)->isInteger(32)) // memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000701 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000702
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000703 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Owen Anderson1d0be152009-08-13 21:58:54 +0000704 ConstantInt::get(TD->getIntPtrType(*Context), Len), B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000705 }
706
707 // Otherwise, the character is a constant, see if the first argument is
708 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000709 std::string Str;
710 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000711 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000712
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000713 // strchr can find the nul character.
714 Str += '\0';
715 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000716
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000717 // Compute the offset.
718 uint64_t i = 0;
719 while (1) {
720 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000721 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000722 // Did we find our match?
723 if (Str[i] == CharValue)
724 break;
725 ++i;
726 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000727
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000728 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000729 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000730 return B.CreateGEP(SrcStr, Idx, "strchr");
731 }
732};
733
734//===---------------------------------------===//
735// 'strcmp' Optimizations
736
Chris Lattner3e8b6632009-09-02 06:11:42 +0000737struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000738 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000739 // Verify the "strcmp" function prototype.
740 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000741 if (FT->getNumParams() != 2 ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +0000742 !FT->getReturnType()->isInteger(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000743 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000744 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000745 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000746
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000747 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
748 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000749 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000750
Bill Wendling0582ae92009-03-13 04:39:26 +0000751 std::string Str1, Str2;
752 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
753 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000754
Bill Wendling0582ae92009-03-13 04:39:26 +0000755 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000756 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000757
Bill Wendling0582ae92009-03-13 04:39:26 +0000758 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000759 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000760
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000761 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000762 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000763 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000764 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000765
766 // strcmp(P, "x") -> memcmp(P, "x", 2)
767 uint64_t Len1 = GetStringLength(Str1P);
768 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000769 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000770 // These optimizations require TargetData.
771 if (!TD) return 0;
772
Nick Lewycky13a09e22008-12-21 00:19:21 +0000773 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000774 ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattner849832c2009-06-19 04:17:36 +0000775 std::min(Len1, Len2)), B);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000776 }
777
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000778 return 0;
779 }
780};
781
782//===---------------------------------------===//
783// 'strncmp' Optimizations
784
Chris Lattner3e8b6632009-09-02 06:11:42 +0000785struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000786 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000787 // Verify the "strncmp" function prototype.
788 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000789 if (FT->getNumParams() != 3 ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +0000790 !FT->getReturnType()->isInteger(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000791 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000792 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000793 !isa<IntegerType>(FT->getParamType(2)))
794 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000795
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000796 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
797 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000798 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000799
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000800 // Get the length argument if it is constant.
801 uint64_t Length;
802 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
803 Length = LengthArg->getZExtValue();
804 else
805 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000806
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000807 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000808 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000809
Bill Wendling0582ae92009-03-13 04:39:26 +0000810 std::string Str1, Str2;
811 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
812 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000813
Bill Wendling0582ae92009-03-13 04:39:26 +0000814 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000815 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000816
Bill Wendling0582ae92009-03-13 04:39:26 +0000817 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000818 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000819
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000820 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000821 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000822 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000823 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000824 return 0;
825 }
826};
827
828
829//===---------------------------------------===//
830// 'strcpy' Optimizations
831
Chris Lattner3e8b6632009-09-02 06:11:42 +0000832struct StrCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000833 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000834 // Verify the "strcpy" function prototype.
835 const FunctionType *FT = Callee->getFunctionType();
836 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
837 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000838 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000839 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000840
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000841 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
842 if (Dst == Src) // strcpy(x,x) -> x
843 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000844
Dan Gohmanf14d9192009-08-18 00:48:13 +0000845 // These optimizations require TargetData.
846 if (!TD) return 0;
847
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000848 // See if we can get the length of the input string.
849 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000850 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000851
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000852 // We have enough information to now generate the memcpy call to do the
853 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000854 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000855 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000856 return Dst;
857 }
858};
859
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000860//===---------------------------------------===//
861// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000862
Chris Lattner3e8b6632009-09-02 06:11:42 +0000863struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000864 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
865 const FunctionType *FT = Callee->getFunctionType();
866 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
867 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000868 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000869 !isa<IntegerType>(FT->getParamType(2)))
870 return 0;
871
872 Value *Dst = CI->getOperand(1);
873 Value *Src = CI->getOperand(2);
874 Value *LenOp = CI->getOperand(3);
875
876 // See if we can get the length of the input string.
877 uint64_t SrcLen = GetStringLength(Src);
878 if (SrcLen == 0) return 0;
879 --SrcLen;
880
881 if (SrcLen == 0) {
882 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000883 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
884 B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000885 return Dst;
886 }
887
888 uint64_t Len;
889 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
890 Len = LengthArg->getZExtValue();
891 else
892 return 0;
893
894 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
895
Dan Gohmanf14d9192009-08-18 00:48:13 +0000896 // These optimizations require TargetData.
897 if (!TD) return 0;
898
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000899 // Let strncpy handle the zero padding
900 if (Len > SrcLen+1) return 0;
901
902 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000903 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000904 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000905
906 return Dst;
907 }
908};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000909
910//===---------------------------------------===//
911// 'strlen' Optimizations
912
Chris Lattner3e8b6632009-09-02 06:11:42 +0000913struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000914 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000915 const FunctionType *FT = Callee->getFunctionType();
916 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000917 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000918 !isa<IntegerType>(FT->getReturnType()))
919 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000920
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000921 Value *Src = CI->getOperand(1);
922
923 // Constant folding: strlen("xyz") -> 3
924 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000925 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000926
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000927 // strlen(x) != 0 --> *x != 0
928 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000929 if (IsOnlyUsedInZeroEqualityComparison(CI))
930 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
931 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000932 }
933};
934
935//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000936// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000937
Chris Lattner3e8b6632009-09-02 06:11:42 +0000938struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000939 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
940 const FunctionType *FT = Callee->getFunctionType();
941 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
942 !isa<PointerType>(FT->getParamType(0)) ||
943 !isa<PointerType>(FT->getParamType(1)))
944 return 0;
945
946 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000947 if (isa<ConstantPointerNull>(EndPtr)) {
948 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000949 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000950 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000951
952 return 0;
953 }
954};
955
Chris Lattner24604112009-12-16 09:32:05 +0000956//===---------------------------------------===//
957// 'strstr' Optimizations
958
959struct StrStrOpt : public LibCallOptimization {
960 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
961 const FunctionType *FT = Callee->getFunctionType();
962 if (FT->getNumParams() != 2 ||
963 !isa<PointerType>(FT->getParamType(0)) ||
964 !isa<PointerType>(FT->getParamType(1)) ||
965 !isa<PointerType>(FT->getReturnType()))
966 return 0;
967
968 // fold strstr(x, x) -> x.
969 if (CI->getOperand(1) == CI->getOperand(2))
970 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000971
Chris Lattner24604112009-12-16 09:32:05 +0000972 // See if either input string is a constant string.
973 std::string SearchStr, ToFindStr;
974 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
975 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000976
Chris Lattner24604112009-12-16 09:32:05 +0000977 // fold strstr(x, "") -> x.
978 if (HasStr2 && ToFindStr.empty())
979 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000980
Chris Lattner24604112009-12-16 09:32:05 +0000981 // If both strings are known, constant fold it.
982 if (HasStr1 && HasStr2) {
983 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000984
Chris Lattner24604112009-12-16 09:32:05 +0000985 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
986 return Constant::getNullValue(CI->getType());
987
988 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
989 Value *Result = CastToCStr(CI->getOperand(1), B);
990 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
991 return B.CreateBitCast(Result, CI->getType());
992 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000993
Chris Lattner24604112009-12-16 09:32:05 +0000994 // fold strstr(x, "y") -> strchr(x, 'y').
995 if (HasStr2 && ToFindStr.size() == 1)
996 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B),
997 CI->getType());
998 return 0;
999 }
1000};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001001
Nick Lewycky4c498412009-02-13 15:31:46 +00001002
1003//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001004// 'memcmp' Optimizations
1005
Chris Lattner3e8b6632009-09-02 06:11:42 +00001006struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001007 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001008 const FunctionType *FT = Callee->getFunctionType();
1009 if (FT->getNumParams() != 3 || !isa<PointerType>(FT->getParamType(0)) ||
1010 !isa<PointerType>(FT->getParamType(1)) ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001011 !FT->getReturnType()->isInteger(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001012 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +00001013
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001014 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +00001015
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001016 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +00001017 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +00001018
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001019 // Make sure we have a constant length.
1020 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001021 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001022 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +00001023
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001024 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +00001025 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001026
1027 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
1028 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
1029 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +00001030 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001031 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +00001032
Benjamin Kramer992a6372009-11-05 17:44:22 +00001033 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
1034 std::string LHSStr, RHSStr;
1035 if (GetConstantStringInfo(LHS, LHSStr) &&
1036 GetConstantStringInfo(RHS, RHSStr)) {
1037 // Make sure we're not reading out-of-bounds memory.
1038 if (Len > LHSStr.length() || Len > RHSStr.length())
1039 return 0;
1040 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
1041 return ConstantInt::get(CI->getType(), Ret);
1042 }
1043
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001044 return 0;
1045 }
1046};
1047
1048//===---------------------------------------===//
1049// 'memcpy' Optimizations
1050
Chris Lattner3e8b6632009-09-02 06:11:42 +00001051struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001052 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001053 // These optimizations require TargetData.
1054 if (!TD) return 0;
1055
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001056 const FunctionType *FT = Callee->getFunctionType();
1057 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1058 !isa<PointerType>(FT->getParamType(0)) ||
1059 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001060 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001061 return 0;
1062
1063 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
1064 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
1065 return CI->getOperand(1);
1066 }
1067};
1068
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001069//===---------------------------------------===//
1070// 'memmove' Optimizations
1071
Chris Lattner3e8b6632009-09-02 06:11:42 +00001072struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001073 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001074 // These optimizations require TargetData.
1075 if (!TD) return 0;
1076
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001077 const FunctionType *FT = Callee->getFunctionType();
1078 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1079 !isa<PointerType>(FT->getParamType(0)) ||
1080 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001081 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001082 return 0;
1083
1084 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopher80bf1d52009-11-21 01:01:30 +00001085 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001086 return CI->getOperand(1);
1087 }
1088};
1089
1090//===---------------------------------------===//
1091// 'memset' Optimizations
1092
Chris Lattner3e8b6632009-09-02 06:11:42 +00001093struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001094 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001095 // These optimizations require TargetData.
1096 if (!TD) return 0;
1097
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001098 const FunctionType *FT = Callee->getFunctionType();
1099 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1100 !isa<PointerType>(FT->getParamType(0)) ||
Eli Friedman62bb4132009-07-18 08:34:51 +00001101 !isa<IntegerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001102 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001103 return 0;
1104
1105 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +00001106 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1107 false);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001108 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001109 return CI->getOperand(1);
1110 }
1111};
1112
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001113//===----------------------------------------------------------------------===//
Eric Christopher80bf1d52009-11-21 01:01:30 +00001114// Object Size Checking Optimizations
1115//===----------------------------------------------------------------------===//
1116
1117//===---------------------------------------===//
Eric Christopher80bf1d52009-11-21 01:01:30 +00001118// 'memcpy_chk' Optimizations
1119
1120struct MemCpyChkOpt : public LibCallOptimization {
1121 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1122 // These optimizations require TargetData.
1123 if (!TD) return 0;
1124
1125 const FunctionType *FT = Callee->getFunctionType();
1126 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1127 !isa<PointerType>(FT->getParamType(0)) ||
1128 !isa<PointerType>(FT->getParamType(1)) ||
Eric Christopherf734be22009-12-22 01:23:51 +00001129 !isa<IntegerType>(FT->getParamType(3)) ||
1130 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eric Christopher80bf1d52009-11-21 01:01:30 +00001131 return 0;
1132
1133 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1134 if (!SizeCI)
1135 return 0;
1136 if (SizeCI->isAllOnesValue()) {
1137 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
1138 return CI->getOperand(1);
1139 }
1140
1141 return 0;
1142 }
1143};
1144
1145//===---------------------------------------===//
1146// 'memset_chk' Optimizations
1147
1148struct MemSetChkOpt : public LibCallOptimization {
1149 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1150 // These optimizations require TargetData.
1151 if (!TD) return 0;
1152
1153 const FunctionType *FT = Callee->getFunctionType();
1154 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1155 !isa<PointerType>(FT->getParamType(0)) ||
1156 !isa<IntegerType>(FT->getParamType(1)) ||
Eric Christopherf734be22009-12-22 01:23:51 +00001157 !isa<IntegerType>(FT->getParamType(3)) ||
Eric Christopher80bf1d52009-11-21 01:01:30 +00001158 FT->getParamType(2) != TD->getIntPtrType(*Context))
1159 return 0;
1160
1161 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1162 if (!SizeCI)
1163 return 0;
1164 if (SizeCI->isAllOnesValue()) {
1165 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1166 false);
1167 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
1168 return CI->getOperand(1);
1169 }
1170
1171 return 0;
1172 }
1173};
1174
1175//===---------------------------------------===//
1176// 'memmove_chk' Optimizations
1177
1178struct MemMoveChkOpt : public LibCallOptimization {
1179 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1180 // These optimizations require TargetData.
1181 if (!TD) return 0;
1182
1183 const FunctionType *FT = Callee->getFunctionType();
1184 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1185 !isa<PointerType>(FT->getParamType(0)) ||
1186 !isa<PointerType>(FT->getParamType(1)) ||
Eric Christopherf734be22009-12-22 01:23:51 +00001187 !isa<IntegerType>(FT->getParamType(3)) ||
Eric Christopher80bf1d52009-11-21 01:01:30 +00001188 FT->getParamType(2) != TD->getIntPtrType(*Context))
1189 return 0;
1190
1191 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1192 if (!SizeCI)
1193 return 0;
1194 if (SizeCI->isAllOnesValue()) {
1195 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
1196 1, B);
1197 return CI->getOperand(1);
1198 }
1199
1200 return 0;
1201 }
1202};
1203
Eric Christopher7672d042010-01-23 05:29:06 +00001204struct StrCpyChkOpt : public LibCallOptimization {
1205 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Eric Christopher7672d042010-01-23 05:29:06 +00001206 const FunctionType *FT = Callee->getFunctionType();
1207 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1208 !isa<PointerType>(FT->getParamType(0)) ||
1209 !isa<PointerType>(FT->getParamType(1)) ||
1210 !isa<IntegerType>(FT->getParamType(2)))
1211 return 0;
1212
1213 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
1214 if (!SizeCI)
1215 return 0;
1216
Eric Christopher407e47c2010-01-29 01:37:11 +00001217 // If a) we don't have any length information, or b) we know this will
1218 // fit then just lower to a plain strcpy. Otherwise we'll keep our
1219 // strcpy_chk call which may fail at runtime if the size is too long.
1220 // TODO: It might be nice to get a maximum length out of the possible
1221 // string lengths for varying.
1222 if (SizeCI->isAllOnesValue() ||
1223 SizeCI->getZExtValue() >= GetStringLength(CI->getOperand(2)))
Eric Christopher7672d042010-01-23 05:29:06 +00001224 return EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B);
1225
1226 return 0;
1227 }
1228};
1229
1230
Eric Christopher80bf1d52009-11-21 01:01:30 +00001231//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001232// Math Library Optimizations
1233//===----------------------------------------------------------------------===//
1234
1235//===---------------------------------------===//
1236// 'pow*' Optimizations
1237
Chris Lattner3e8b6632009-09-02 06:11:42 +00001238struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001239 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001240 const FunctionType *FT = Callee->getFunctionType();
1241 // Just make sure this has 2 arguments of the same FP type, which match the
1242 // result type.
1243 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1244 FT->getParamType(0) != FT->getParamType(1) ||
1245 !FT->getParamType(0)->isFloatingPoint())
1246 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001247
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001248 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
1249 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1250 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
1251 return Op1C;
1252 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +00001253 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001254 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001255
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001256 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1257 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001258
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001259 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001260 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001261
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001262 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +00001263 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1264 // This is faster than calling pow, and still handles negative zero
1265 // and negative infinite correctly.
1266 // TODO: In fast-math mode, this could be just sqrt(x).
1267 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +00001268 Value *Inf = ConstantFP::getInfinity(CI->getType());
1269 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +00001270 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1271 Callee->getAttributes());
1272 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1273 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +00001274 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
1275 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
1276 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001277 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001278
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001279 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1280 return Op1;
1281 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001282 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001283 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001284 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001285 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001286 return 0;
1287 }
1288};
1289
1290//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +00001291// 'exp2' Optimizations
1292
Chris Lattner3e8b6632009-09-02 06:11:42 +00001293struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001294 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +00001295 const FunctionType *FT = Callee->getFunctionType();
1296 // Just make sure this has 1 argument of FP type, which matches the
1297 // result type.
1298 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1299 !FT->getParamType(0)->isFloatingPoint())
1300 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001301
Chris Lattnere818f772008-05-02 18:43:35 +00001302 Value *Op = CI->getOperand(1);
1303 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1304 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1305 Value *LdExpArg = 0;
1306 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1307 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001308 LdExpArg = B.CreateSExt(OpC->getOperand(0),
1309 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001310 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1311 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001312 LdExpArg = B.CreateZExt(OpC->getOperand(0),
1313 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001314 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001315
Chris Lattnere818f772008-05-02 18:43:35 +00001316 if (LdExpArg) {
1317 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001318 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001319 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001320 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001321 Name = "ldexp";
1322 else
1323 Name = "ldexpl";
1324
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001325 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001326 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001327 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +00001328
1329 Module *M = Caller->getParent();
1330 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +00001331 Op->getType(),
1332 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001333 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1334 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1335 CI->setCallingConv(F->getCallingConv());
1336
1337 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +00001338 }
1339 return 0;
1340 }
1341};
Chris Lattnere818f772008-05-02 18:43:35 +00001342
1343//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001344// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1345
Chris Lattner3e8b6632009-09-02 06:11:42 +00001346struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001347 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001348 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001349 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1350 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001351 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001352
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001353 // If this is something like 'floor((double)floatval)', convert to floorf.
1354 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001355 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001356 return 0;
1357
1358 // floor((double)floatval) -> (double)floorf(floatval)
1359 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +00001360 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
1361 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +00001362 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001363 }
1364};
1365
1366//===----------------------------------------------------------------------===//
1367// Integer Optimizations
1368//===----------------------------------------------------------------------===//
1369
1370//===---------------------------------------===//
1371// 'ffs*' Optimizations
1372
Chris Lattner3e8b6632009-09-02 06:11:42 +00001373struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001374 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001375 const FunctionType *FT = Callee->getFunctionType();
1376 // Just make sure this has 2 arguments of the same FP type, which match the
1377 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +00001378 if (FT->getNumParams() != 1 ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001379 !FT->getReturnType()->isInteger(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001380 !isa<IntegerType>(FT->getParamType(0)))
1381 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001382
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001383 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001384
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001385 // Constant fold.
1386 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1387 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +00001388 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +00001389 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001390 CI->getValue().countTrailingZeros()+1);
1391 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001392
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001393 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1394 const Type *ArgType = Op->getType();
1395 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1396 Intrinsic::cttz, &ArgType, 1);
1397 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +00001398 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +00001399 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001400
Owen Andersona7235ea2009-07-31 20:28:14 +00001401 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001402 return B.CreateSelect(Cond, V,
1403 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001404 }
1405};
1406
1407//===---------------------------------------===//
1408// 'isdigit' Optimizations
1409
Chris Lattner3e8b6632009-09-02 06:11:42 +00001410struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001411 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001412 const FunctionType *FT = Callee->getFunctionType();
1413 // We require integer(i32)
1414 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001415 !FT->getParamType(0)->isInteger(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001416 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001417
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001418 // isdigit(c) -> (c-'0') <u 10
1419 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001420 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001421 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001422 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001423 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001424 return B.CreateZExt(Op, CI->getType());
1425 }
1426};
1427
1428//===---------------------------------------===//
1429// 'isascii' Optimizations
1430
Chris Lattner3e8b6632009-09-02 06:11:42 +00001431struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001432 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001433 const FunctionType *FT = Callee->getFunctionType();
1434 // We require integer(i32)
1435 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001436 !FT->getParamType(0)->isInteger(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001437 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001438
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001439 // isascii(c) -> c <u 128
1440 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001441 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001442 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001443 return B.CreateZExt(Op, CI->getType());
1444 }
1445};
Eric Christopher37c8b862009-10-07 21:14:25 +00001446
Chris Lattner313f0e62008-06-09 08:26:51 +00001447//===---------------------------------------===//
1448// 'abs', 'labs', 'llabs' Optimizations
1449
Chris Lattner3e8b6632009-09-02 06:11:42 +00001450struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001451 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001452 const FunctionType *FT = Callee->getFunctionType();
1453 // We require integer(integer) where the types agree.
1454 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
1455 FT->getParamType(0) != FT->getReturnType())
1456 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001457
Chris Lattner313f0e62008-06-09 08:26:51 +00001458 // abs(x) -> x >s -1 ? x : -x
1459 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001460 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001461 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001462 "ispos");
1463 Value *Neg = B.CreateNeg(Op, "neg");
1464 return B.CreateSelect(Pos, Op, Neg);
1465 }
1466};
Eric Christopher37c8b862009-10-07 21:14:25 +00001467
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001468
1469//===---------------------------------------===//
1470// 'toascii' Optimizations
1471
Chris Lattner3e8b6632009-09-02 06:11:42 +00001472struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001473 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001474 const FunctionType *FT = Callee->getFunctionType();
1475 // We require i32(i32)
1476 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001477 !FT->getParamType(0)->isInteger(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001478 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001479
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001480 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001481 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +00001482 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001483 }
1484};
1485
1486//===----------------------------------------------------------------------===//
1487// Formatting and IO Optimizations
1488//===----------------------------------------------------------------------===//
1489
1490//===---------------------------------------===//
1491// 'printf' Optimizations
1492
Chris Lattner3e8b6632009-09-02 06:11:42 +00001493struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001494 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001495 // Require one fixed pointer argument and an integer/void result.
1496 const FunctionType *FT = Callee->getFunctionType();
1497 if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
1498 !(isa<IntegerType>(FT->getReturnType()) ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001499 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001500 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001501
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001502 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001503 std::string FormatStr;
1504 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1505 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001506
1507 // Empty format string -> noop.
1508 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001509 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001510 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001511
Chris Lattner74965f22009-11-09 04:57:04 +00001512 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1513 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001514 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001515 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
1516 FormatStr[0]), B);
1517 if (CI->use_empty()) return CI;
1518 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001519 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001520
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001521 // printf("foo\n") --> puts("foo")
1522 if (FormatStr[FormatStr.size()-1] == '\n' &&
1523 FormatStr.find('%') == std::string::npos) { // no format characters.
1524 // Create a string literal with no \n on it. We expect the constant merge
1525 // pass to be run after this pass, to merge duplicate strings.
1526 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001527 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001528 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1529 GlobalVariable::InternalLinkage, C, "str");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001530 EmitPutS(C, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001531 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001532 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001533 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001534
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001535 // Optimize specific format strings.
1536 // printf("%c", chr) --> putchar(*(i8*)dst)
1537 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
1538 isa<IntegerType>(CI->getOperand(2)->getType())) {
Chris Lattner74965f22009-11-09 04:57:04 +00001539 Value *Res = EmitPutChar(CI->getOperand(2), B);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001540
Chris Lattner74965f22009-11-09 04:57:04 +00001541 if (CI->use_empty()) return CI;
1542 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001543 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001544
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001545 // printf("%s\n", str) --> puts(str)
1546 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
1547 isa<PointerType>(CI->getOperand(2)->getType()) &&
1548 CI->use_empty()) {
1549 EmitPutS(CI->getOperand(2), B);
1550 return CI;
1551 }
1552 return 0;
1553 }
1554};
1555
1556//===---------------------------------------===//
1557// 'sprintf' Optimizations
1558
Chris Lattner3e8b6632009-09-02 06:11:42 +00001559struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001560 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001561 // Require two fixed pointer arguments and an integer result.
1562 const FunctionType *FT = Callee->getFunctionType();
1563 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1564 !isa<PointerType>(FT->getParamType(1)) ||
1565 !isa<IntegerType>(FT->getReturnType()))
1566 return 0;
1567
1568 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001569 std::string FormatStr;
1570 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1571 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001572
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001573 // If we just have a format string (nothing else crazy) transform it.
1574 if (CI->getNumOperands() == 3) {
1575 // Make sure there's no % in the constant array. We could try to handle
1576 // %% -> % in the future if we cared.
1577 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1578 if (FormatStr[i] == '%')
1579 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001580
1581 // These optimizations require TargetData.
1582 if (!TD) return 0;
1583
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001584 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1585 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001586 ConstantInt::get
1587 (TD->getIntPtrType(*Context), FormatStr.size()+1),1,B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001588 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001589 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001590
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001591 // The remaining optimizations require the format string to be "%s" or "%c"
1592 // and have an extra operand.
1593 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1594 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001595
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001596 // Decode the second character of the format string.
1597 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001598 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001599 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001600 Value *V = B.CreateTrunc(CI->getOperand(3),
1601 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001602 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1603 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001604 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1605 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001606 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001607
Owen Andersoneed707b2009-07-24 23:12:02 +00001608 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001609 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001610
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001611 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001612 // These optimizations require TargetData.
1613 if (!TD) return 0;
1614
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001615 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1616 if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
1617
1618 Value *Len = EmitStrLen(CI->getOperand(3), B);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001619 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001620 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001621 "leninc");
1622 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001623
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001624 // The sprintf result is the unincremented number of bytes in the string.
1625 return B.CreateIntCast(Len, CI->getType(), false);
1626 }
1627 return 0;
1628 }
1629};
1630
1631//===---------------------------------------===//
1632// 'fwrite' Optimizations
1633
Chris Lattner3e8b6632009-09-02 06:11:42 +00001634struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001635 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001636 // Require a pointer, an integer, an integer, a pointer, returning integer.
1637 const FunctionType *FT = Callee->getFunctionType();
1638 if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
1639 !isa<IntegerType>(FT->getParamType(1)) ||
1640 !isa<IntegerType>(FT->getParamType(2)) ||
1641 !isa<PointerType>(FT->getParamType(3)) ||
1642 !isa<IntegerType>(FT->getReturnType()))
1643 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001644
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001645 // Get the element size and count.
1646 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1647 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1648 if (!SizeC || !CountC) return 0;
1649 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001650
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001651 // If this is writing zero records, remove the call (it's a noop).
1652 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001653 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001654
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001655 // If this is writing one byte, turn it into fputc.
1656 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1657 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1658 EmitFPutC(Char, CI->getOperand(4), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001659 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001660 }
1661
1662 return 0;
1663 }
1664};
1665
1666//===---------------------------------------===//
1667// 'fputs' Optimizations
1668
Chris Lattner3e8b6632009-09-02 06:11:42 +00001669struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001670 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001671 // These optimizations require TargetData.
1672 if (!TD) return 0;
1673
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001674 // Require two pointers. Also, we can't optimize if return value is used.
1675 const FunctionType *FT = Callee->getFunctionType();
1676 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1677 !isa<PointerType>(FT->getParamType(1)) ||
1678 !CI->use_empty())
1679 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001680
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001681 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1682 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001683 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001684 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001685 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001686 CI->getOperand(2), B);
1687 return CI; // Known to have no uses (see above).
1688 }
1689};
1690
1691//===---------------------------------------===//
1692// 'fprintf' Optimizations
1693
Chris Lattner3e8b6632009-09-02 06:11:42 +00001694struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001695 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001696 // Require two fixed paramters as pointers and integer result.
1697 const FunctionType *FT = Callee->getFunctionType();
1698 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1699 !isa<PointerType>(FT->getParamType(1)) ||
1700 !isa<IntegerType>(FT->getReturnType()))
1701 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001702
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001703 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001704 std::string FormatStr;
1705 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1706 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001707
1708 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1709 if (CI->getNumOperands() == 3) {
1710 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1711 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001712 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001713
1714 // These optimizations require TargetData.
1715 if (!TD) return 0;
1716
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001717 EmitFWrite(CI->getOperand(2),
1718 ConstantInt::get(TD->getIntPtrType(*Context),
1719 FormatStr.size()),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001720 CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001721 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001722 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001723
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001724 // The remaining optimizations require the format string to be "%s" or "%c"
1725 // and have an extra operand.
1726 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1727 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001728
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001729 // Decode the second character of the format string.
1730 if (FormatStr[1] == 'c') {
1731 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1732 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1733 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001734 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001735 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001736
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001737 if (FormatStr[1] == 's') {
1738 // fprintf(F, "%s", str) -> fputs(str, F)
1739 if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
1740 return 0;
1741 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1742 return CI;
1743 }
1744 return 0;
1745 }
1746};
1747
Bill Wendlingac178222008-05-05 21:37:59 +00001748} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001749
1750//===----------------------------------------------------------------------===//
1751// SimplifyLibCalls Pass Implementation
1752//===----------------------------------------------------------------------===//
1753
1754namespace {
1755 /// This pass optimizes well known library functions from libc and libm.
1756 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001757 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001758 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001759 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001760 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1761 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001762 StrToOpt StrTo; StrStrOpt StrStr;
1763 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001764 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001765 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001766 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001767 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1768 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001769 // Formatting and IO Optimizations
1770 SPrintFOpt SPrintF; PrintFOpt PrintF;
1771 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001772
1773 // Object Size Checking
Eric Christopher80bf1d52009-11-21 01:01:30 +00001774 MemCpyChkOpt MemCpyChk; MemSetChkOpt MemSetChk; MemMoveChkOpt MemMoveChk;
Eric Christopher7672d042010-01-23 05:29:06 +00001775 StrCpyChkOpt StrCpyChk;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001776
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001777 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001778 public:
1779 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001780 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001781
1782 void InitOptimizations();
1783 bool runOnFunction(Function &F);
1784
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001785 void setDoesNotAccessMemory(Function &F);
1786 void setOnlyReadsMemory(Function &F);
1787 void setDoesNotThrow(Function &F);
1788 void setDoesNotCapture(Function &F, unsigned n);
1789 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001790 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001791
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001792 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001793 }
1794 };
1795 char SimplifyLibCalls::ID = 0;
1796} // end anonymous namespace.
1797
1798static RegisterPass<SimplifyLibCalls>
1799X("simplify-libcalls", "Simplify well-known library calls");
1800
1801// Public interface to the Simplify LibCalls pass.
1802FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001803 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001804}
1805
1806/// Optimizations - Populate the Optimizations map with all the optimizations
1807/// we know.
1808void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001809 // String and Memory LibCall Optimizations
1810 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001811 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001812 Optimizations["strchr"] = &StrChr;
1813 Optimizations["strcmp"] = &StrCmp;
1814 Optimizations["strncmp"] = &StrNCmp;
1815 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001816 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001817 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001818 Optimizations["strtol"] = &StrTo;
1819 Optimizations["strtod"] = &StrTo;
1820 Optimizations["strtof"] = &StrTo;
1821 Optimizations["strtoul"] = &StrTo;
1822 Optimizations["strtoll"] = &StrTo;
1823 Optimizations["strtold"] = &StrTo;
1824 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001825 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001826 Optimizations["memcmp"] = &MemCmp;
1827 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001828 Optimizations["memmove"] = &MemMove;
1829 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001830
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001831 // Math Library Optimizations
1832 Optimizations["powf"] = &Pow;
1833 Optimizations["pow"] = &Pow;
1834 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001835 Optimizations["llvm.pow.f32"] = &Pow;
1836 Optimizations["llvm.pow.f64"] = &Pow;
1837 Optimizations["llvm.pow.f80"] = &Pow;
1838 Optimizations["llvm.pow.f128"] = &Pow;
1839 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001840 Optimizations["exp2l"] = &Exp2;
1841 Optimizations["exp2"] = &Exp2;
1842 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001843 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1844 Optimizations["llvm.exp2.f128"] = &Exp2;
1845 Optimizations["llvm.exp2.f80"] = &Exp2;
1846 Optimizations["llvm.exp2.f64"] = &Exp2;
1847 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001848
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001849#ifdef HAVE_FLOORF
1850 Optimizations["floor"] = &UnaryDoubleFP;
1851#endif
1852#ifdef HAVE_CEILF
1853 Optimizations["ceil"] = &UnaryDoubleFP;
1854#endif
1855#ifdef HAVE_ROUNDF
1856 Optimizations["round"] = &UnaryDoubleFP;
1857#endif
1858#ifdef HAVE_RINTF
1859 Optimizations["rint"] = &UnaryDoubleFP;
1860#endif
1861#ifdef HAVE_NEARBYINTF
1862 Optimizations["nearbyint"] = &UnaryDoubleFP;
1863#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001864
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001865 // Integer Optimizations
1866 Optimizations["ffs"] = &FFS;
1867 Optimizations["ffsl"] = &FFS;
1868 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001869 Optimizations["abs"] = &Abs;
1870 Optimizations["labs"] = &Abs;
1871 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001872 Optimizations["isdigit"] = &IsDigit;
1873 Optimizations["isascii"] = &IsAscii;
1874 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001875
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001876 // Formatting and IO Optimizations
1877 Optimizations["sprintf"] = &SPrintF;
1878 Optimizations["printf"] = &PrintF;
1879 Optimizations["fwrite"] = &FWrite;
1880 Optimizations["fputs"] = &FPuts;
1881 Optimizations["fprintf"] = &FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001882
1883 // Object Size Checking
Eric Christopher80bf1d52009-11-21 01:01:30 +00001884 Optimizations["__memcpy_chk"] = &MemCpyChk;
1885 Optimizations["__memset_chk"] = &MemSetChk;
1886 Optimizations["__memmove_chk"] = &MemMoveChk;
Eric Christopher7672d042010-01-23 05:29:06 +00001887 Optimizations["__strcpy_chk"] = &StrCpyChk;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001888}
1889
1890
1891/// runOnFunction - Top level algorithm.
1892///
1893bool SimplifyLibCalls::runOnFunction(Function &F) {
1894 if (Optimizations.empty())
1895 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001896
Dan Gohmanf14d9192009-08-18 00:48:13 +00001897 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001898
Owen Andersone922c022009-07-22 00:24:57 +00001899 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001900
1901 bool Changed = false;
1902 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1903 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1904 // Ignore non-calls.
1905 CallInst *CI = dyn_cast<CallInst>(I++);
1906 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001907
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001908 // Ignore indirect calls and calls to non-external functions.
1909 Function *Callee = CI->getCalledFunction();
1910 if (Callee == 0 || !Callee->isDeclaration() ||
1911 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1912 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001913
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001914 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001915 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1916 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001917
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001918 // Set the builder to the instruction after the call.
1919 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001920
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001921 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001922 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001923 if (Result == 0) continue;
1924
David Greene6a6b90e2010-01-05 01:27:21 +00001925 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1926 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001927
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001928 // Something changed!
1929 Changed = true;
1930 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001931
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001932 // Inspect the instruction after the call (which was potentially just
1933 // added) next.
1934 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001935
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001936 if (CI != Result && !CI->use_empty()) {
1937 CI->replaceAllUsesWith(Result);
1938 if (!Result->hasName())
1939 Result->takeName(CI);
1940 }
1941 CI->eraseFromParent();
1942 }
1943 }
1944 return Changed;
1945}
1946
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001947// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001948
1949void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1950 if (!F.doesNotAccessMemory()) {
1951 F.setDoesNotAccessMemory();
1952 ++NumAnnotated;
1953 Modified = true;
1954 }
1955}
1956void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1957 if (!F.onlyReadsMemory()) {
1958 F.setOnlyReadsMemory();
1959 ++NumAnnotated;
1960 Modified = true;
1961 }
1962}
1963void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1964 if (!F.doesNotThrow()) {
1965 F.setDoesNotThrow();
1966 ++NumAnnotated;
1967 Modified = true;
1968 }
1969}
1970void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1971 if (!F.doesNotCapture(n)) {
1972 F.setDoesNotCapture(n);
1973 ++NumAnnotated;
1974 Modified = true;
1975 }
1976}
1977void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1978 if (!F.doesNotAlias(n)) {
1979 F.setDoesNotAlias(n);
1980 ++NumAnnotated;
1981 Modified = true;
1982 }
1983}
1984
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001985/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001986///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001987bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001988 Modified = false;
1989 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1990 Function &F = *I;
1991 if (!F.isDeclaration())
1992 continue;
1993
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001994 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001995 continue;
1996
1997 const FunctionType *FTy = F.getFunctionType();
1998
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001999 StringRef Name = F.getName();
2000 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002001 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002002 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002003 if (FTy->getNumParams() != 1 ||
2004 !isa<PointerType>(FTy->getParamType(0)))
2005 continue;
2006 setOnlyReadsMemory(F);
2007 setDoesNotThrow(F);
2008 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002009 } else if (Name == "strcpy" ||
2010 Name == "stpcpy" ||
2011 Name == "strcat" ||
2012 Name == "strtol" ||
2013 Name == "strtod" ||
2014 Name == "strtof" ||
2015 Name == "strtoul" ||
2016 Name == "strtoll" ||
2017 Name == "strtold" ||
2018 Name == "strncat" ||
2019 Name == "strncpy" ||
2020 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002021 if (FTy->getNumParams() < 2 ||
2022 !isa<PointerType>(FTy->getParamType(1)))
2023 continue;
2024 setDoesNotThrow(F);
2025 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002026 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002027 if (FTy->getNumParams() != 3 ||
2028 !isa<PointerType>(FTy->getParamType(0)) ||
2029 !isa<PointerType>(FTy->getParamType(1)))
2030 continue;
2031 setDoesNotThrow(F);
2032 setDoesNotCapture(F, 1);
2033 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002034 } else if (Name == "strcmp" ||
2035 Name == "strspn" ||
2036 Name == "strncmp" ||
2037 Name ==" strcspn" ||
2038 Name == "strcoll" ||
2039 Name == "strcasecmp" ||
2040 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002041 if (FTy->getNumParams() < 2 ||
2042 !isa<PointerType>(FTy->getParamType(0)) ||
2043 !isa<PointerType>(FTy->getParamType(1)))
2044 continue;
2045 setOnlyReadsMemory(F);
2046 setDoesNotThrow(F);
2047 setDoesNotCapture(F, 1);
2048 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002049 } else if (Name == "strstr" ||
2050 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002051 if (FTy->getNumParams() != 2 ||
2052 !isa<PointerType>(FTy->getParamType(1)))
2053 continue;
2054 setOnlyReadsMemory(F);
2055 setDoesNotThrow(F);
2056 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002057 } else if (Name == "strtok" ||
2058 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002059 if (FTy->getNumParams() < 2 ||
2060 !isa<PointerType>(FTy->getParamType(1)))
2061 continue;
2062 setDoesNotThrow(F);
2063 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002064 } else if (Name == "scanf" ||
2065 Name == "setbuf" ||
2066 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002067 if (FTy->getNumParams() < 1 ||
2068 !isa<PointerType>(FTy->getParamType(0)))
2069 continue;
2070 setDoesNotThrow(F);
2071 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002072 } else if (Name == "strdup" ||
2073 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00002074 if (FTy->getNumParams() < 1 ||
2075 !isa<PointerType>(FTy->getReturnType()) ||
2076 !isa<PointerType>(FTy->getParamType(0)))
2077 continue;
2078 setDoesNotThrow(F);
2079 setDoesNotAlias(F, 0);
2080 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002081 } else if (Name == "stat" ||
2082 Name == "sscanf" ||
2083 Name == "sprintf" ||
2084 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002085 if (FTy->getNumParams() < 2 ||
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002086 !isa<PointerType>(FTy->getParamType(0)) ||
2087 !isa<PointerType>(FTy->getParamType(1)))
2088 continue;
2089 setDoesNotThrow(F);
2090 setDoesNotCapture(F, 1);
2091 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002092 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002093 if (FTy->getNumParams() != 3 ||
2094 !isa<PointerType>(FTy->getParamType(0)) ||
2095 !isa<PointerType>(FTy->getParamType(2)))
2096 continue;
2097 setDoesNotThrow(F);
2098 setDoesNotCapture(F, 1);
2099 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002100 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002101 if (FTy->getNumParams() != 3 ||
2102 !isa<PointerType>(FTy->getParamType(1)) ||
2103 !isa<PointerType>(FTy->getParamType(2)))
2104 continue;
2105 setDoesNotThrow(F);
2106 setDoesNotCapture(F, 2);
2107 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002108 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002109 if (FTy->getNumParams() != 1 ||
2110 !isa<PointerType>(FTy->getParamType(0)))
2111 continue;
2112 // May throw; "system" is a valid pthread cancellation point.
2113 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002114 }
2115 break;
2116 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00002117 if (Name == "malloc") {
2118 if (FTy->getNumParams() != 1 ||
2119 !isa<PointerType>(FTy->getReturnType()))
2120 continue;
2121 setDoesNotThrow(F);
2122 setDoesNotAlias(F, 0);
2123 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002124 if (FTy->getNumParams() != 3 ||
2125 !isa<PointerType>(FTy->getParamType(0)) ||
2126 !isa<PointerType>(FTy->getParamType(1)))
2127 continue;
2128 setOnlyReadsMemory(F);
2129 setDoesNotThrow(F);
2130 setDoesNotCapture(F, 1);
2131 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002132 } else if (Name == "memchr" ||
2133 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002134 if (FTy->getNumParams() != 3)
2135 continue;
2136 setOnlyReadsMemory(F);
2137 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002138 } else if (Name == "modf" ||
2139 Name == "modff" ||
2140 Name == "modfl" ||
2141 Name == "memcpy" ||
2142 Name == "memccpy" ||
2143 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002144 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002145 !isa<PointerType>(FTy->getParamType(1)))
2146 continue;
2147 setDoesNotThrow(F);
2148 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002149 } else if (Name == "memalign") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002150 if (!isa<PointerType>(FTy->getReturnType()))
2151 continue;
2152 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002153 } else if (Name == "mkdir" ||
2154 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002155 if (FTy->getNumParams() == 0 ||
2156 !isa<PointerType>(FTy->getParamType(0)))
2157 continue;
2158 setDoesNotThrow(F);
2159 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002160 }
2161 break;
2162 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002163 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002164 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002165 !isa<PointerType>(FTy->getParamType(0)) ||
2166 !isa<PointerType>(FTy->getReturnType()))
2167 continue;
2168 setDoesNotThrow(F);
2169 setDoesNotAlias(F, 0);
2170 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002171 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002172 if (FTy->getNumParams() != 3 ||
2173 !isa<PointerType>(FTy->getParamType(1)))
2174 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002175 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002176 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002177 } else if (Name == "rmdir" ||
2178 Name == "rewind" ||
2179 Name == "remove" ||
2180 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002181 if (FTy->getNumParams() < 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002182 !isa<PointerType>(FTy->getParamType(0)))
2183 continue;
2184 setDoesNotThrow(F);
2185 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002186 } else if (Name == "rename" ||
2187 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002188 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002189 !isa<PointerType>(FTy->getParamType(0)) ||
2190 !isa<PointerType>(FTy->getParamType(1)))
2191 continue;
2192 setDoesNotThrow(F);
2193 setDoesNotCapture(F, 1);
2194 setDoesNotCapture(F, 2);
2195 }
2196 break;
2197 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002198 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002199 if (FTy->getNumParams() != 3 ||
2200 !isa<PointerType>(FTy->getParamType(1)))
2201 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002202 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002203 setDoesNotCapture(F, 2);
2204 }
2205 break;
2206 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002207 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002208 if (FTy->getNumParams() != 3 ||
2209 !isa<PointerType>(FTy->getParamType(0)) ||
2210 !isa<PointerType>(FTy->getParamType(1)))
2211 continue;
2212 setDoesNotThrow(F);
2213 setDoesNotCapture(F, 1);
2214 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002215 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002216 if (FTy->getNumParams() != 3 ||
2217 !isa<PointerType>(FTy->getParamType(0)) ||
2218 !isa<PointerType>(FTy->getParamType(1)))
2219 continue;
2220 setDoesNotThrow(F);
2221 setOnlyReadsMemory(F);
2222 setDoesNotCapture(F, 1);
2223 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002224 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002225 if (FTy->getNumParams() != 2 ||
2226 !isa<PointerType>(FTy->getParamType(0)))
2227 continue;
2228 setDoesNotThrow(F);
2229 setDoesNotCapture(F, 1);
2230 }
2231 break;
2232 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002233 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002234 if (FTy->getNumParams() != 2 ||
2235 !isa<PointerType>(FTy->getReturnType()))
2236 continue;
2237 setDoesNotThrow(F);
2238 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002239 } else if (Name == "chmod" ||
2240 Name == "chown" ||
2241 Name == "ctermid" ||
2242 Name == "clearerr" ||
2243 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002244 if (FTy->getNumParams() == 0 ||
2245 !isa<PointerType>(FTy->getParamType(0)))
2246 continue;
2247 setDoesNotThrow(F);
2248 setDoesNotCapture(F, 1);
2249 }
2250 break;
2251 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002252 if (Name == "atoi" ||
2253 Name == "atol" ||
2254 Name == "atof" ||
2255 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002256 if (FTy->getNumParams() != 1 ||
2257 !isa<PointerType>(FTy->getParamType(0)))
2258 continue;
2259 setDoesNotThrow(F);
2260 setOnlyReadsMemory(F);
2261 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002262 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002263 if (FTy->getNumParams() != 2 ||
2264 !isa<PointerType>(FTy->getParamType(0)))
2265 continue;
2266 setDoesNotThrow(F);
2267 setDoesNotCapture(F, 1);
2268 }
2269 break;
2270 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002271 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002272 if (FTy->getNumParams() != 2 ||
2273 !isa<PointerType>(FTy->getReturnType()) ||
2274 !isa<PointerType>(FTy->getParamType(0)) ||
2275 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002276 continue;
2277 setDoesNotThrow(F);
2278 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002279 setDoesNotCapture(F, 1);
2280 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002281 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002282 if (FTy->getNumParams() != 2 ||
2283 !isa<PointerType>(FTy->getReturnType()) ||
2284 !isa<PointerType>(FTy->getParamType(1)))
2285 continue;
2286 setDoesNotThrow(F);
2287 setDoesNotAlias(F, 0);
2288 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002289 } else if (Name == "feof" ||
2290 Name == "free" ||
2291 Name == "fseek" ||
2292 Name == "ftell" ||
2293 Name == "fgetc" ||
2294 Name == "fseeko" ||
2295 Name == "ftello" ||
2296 Name == "fileno" ||
2297 Name == "fflush" ||
2298 Name == "fclose" ||
2299 Name == "fsetpos" ||
2300 Name == "flockfile" ||
2301 Name == "funlockfile" ||
2302 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002303 if (FTy->getNumParams() == 0 ||
2304 !isa<PointerType>(FTy->getParamType(0)))
2305 continue;
2306 setDoesNotThrow(F);
2307 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002308 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002309 if (FTy->getNumParams() != 1 ||
2310 !isa<PointerType>(FTy->getParamType(0)))
2311 continue;
2312 setDoesNotThrow(F);
2313 setDoesNotCapture(F, 1);
2314 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002315 } else if (Name == "fputc" ||
2316 Name == "fstat" ||
2317 Name == "frexp" ||
2318 Name == "frexpf" ||
2319 Name == "frexpl" ||
2320 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002321 if (FTy->getNumParams() != 2 ||
2322 !isa<PointerType>(FTy->getParamType(1)))
2323 continue;
2324 setDoesNotThrow(F);
2325 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002326 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002327 if (FTy->getNumParams() != 3 ||
2328 !isa<PointerType>(FTy->getParamType(0)) ||
2329 !isa<PointerType>(FTy->getParamType(2)))
2330 continue;
2331 setDoesNotThrow(F);
2332 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002333 } else if (Name == "fread" ||
2334 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002335 if (FTy->getNumParams() != 4 ||
2336 !isa<PointerType>(FTy->getParamType(0)) ||
2337 !isa<PointerType>(FTy->getParamType(3)))
2338 continue;
2339 setDoesNotThrow(F);
2340 setDoesNotCapture(F, 1);
2341 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002342 } else if (Name == "fputs" ||
2343 Name == "fscanf" ||
2344 Name == "fprintf" ||
2345 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002346 if (FTy->getNumParams() < 2 ||
2347 !isa<PointerType>(FTy->getParamType(0)) ||
2348 !isa<PointerType>(FTy->getParamType(1)))
2349 continue;
2350 setDoesNotThrow(F);
2351 setDoesNotCapture(F, 1);
2352 setDoesNotCapture(F, 2);
2353 }
2354 break;
2355 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002356 if (Name == "getc" ||
2357 Name == "getlogin_r" ||
2358 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002359 if (FTy->getNumParams() == 0 ||
2360 !isa<PointerType>(FTy->getParamType(0)))
2361 continue;
2362 setDoesNotThrow(F);
2363 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002364 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002365 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002366 !isa<PointerType>(FTy->getParamType(0)))
2367 continue;
2368 setDoesNotThrow(F);
2369 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002370 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002371 } else if (Name == "gets" ||
2372 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002373 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002374 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002375 if (FTy->getNumParams() != 2 ||
2376 !isa<PointerType>(FTy->getParamType(1)))
2377 continue;
2378 setDoesNotThrow(F);
2379 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002380 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002381 if (FTy->getNumParams() != 1 ||
2382 !isa<PointerType>(FTy->getParamType(0)))
2383 continue;
2384 setDoesNotThrow(F);
2385 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002386 }
2387 break;
2388 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002389 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002390 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002391 !isa<PointerType>(FTy->getParamType(1)))
2392 continue;
2393 setDoesNotThrow(F);
2394 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002395 } else if (Name == "uname" ||
2396 Name == "unlink" ||
2397 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002398 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002399 !isa<PointerType>(FTy->getParamType(0)))
2400 continue;
2401 setDoesNotThrow(F);
2402 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002403 } else if (Name == "utime" ||
2404 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002405 if (FTy->getNumParams() != 2 ||
2406 !isa<PointerType>(FTy->getParamType(0)) ||
2407 !isa<PointerType>(FTy->getParamType(1)))
2408 continue;
2409 setDoesNotThrow(F);
2410 setDoesNotCapture(F, 1);
2411 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002412 }
2413 break;
2414 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002415 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002416 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002417 !isa<PointerType>(FTy->getParamType(1)))
2418 continue;
2419 setDoesNotThrow(F);
2420 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002421 } else if (Name == "puts" ||
2422 Name == "printf" ||
2423 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002424 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002425 !isa<PointerType>(FTy->getParamType(0)))
2426 continue;
2427 setDoesNotThrow(F);
2428 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002429 } else if (Name == "pread" ||
2430 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002431 if (FTy->getNumParams() != 4 ||
2432 !isa<PointerType>(FTy->getParamType(1)))
2433 continue;
2434 // May throw; these are valid pthread cancellation points.
2435 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002436 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002437 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002438 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002439 if (FTy->getNumParams() != 2 ||
2440 !isa<PointerType>(FTy->getReturnType()) ||
2441 !isa<PointerType>(FTy->getParamType(0)) ||
2442 !isa<PointerType>(FTy->getParamType(1)))
2443 continue;
2444 setDoesNotThrow(F);
2445 setDoesNotAlias(F, 0);
2446 setDoesNotCapture(F, 1);
2447 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002448 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002449 if (FTy->getNumParams() != 1 ||
2450 !isa<PointerType>(FTy->getParamType(0)))
2451 continue;
2452 setDoesNotThrow(F);
2453 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002454 }
2455 break;
2456 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002457 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002458 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002459 !isa<PointerType>(FTy->getParamType(1)))
2460 continue;
2461 setDoesNotThrow(F);
2462 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002463 } else if (Name == "vsscanf" ||
2464 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002465 if (FTy->getNumParams() != 3 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002466 !isa<PointerType>(FTy->getParamType(1)) ||
2467 !isa<PointerType>(FTy->getParamType(2)))
2468 continue;
2469 setDoesNotThrow(F);
2470 setDoesNotCapture(F, 1);
2471 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002472 } else if (Name == "valloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002473 if (!isa<PointerType>(FTy->getReturnType()))
2474 continue;
2475 setDoesNotThrow(F);
2476 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002477 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002478 if (FTy->getNumParams() != 2 ||
2479 !isa<PointerType>(FTy->getParamType(0)))
2480 continue;
2481 setDoesNotThrow(F);
2482 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002483 } else if (Name == "vfprintf" ||
2484 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002485 if (FTy->getNumParams() != 3 ||
2486 !isa<PointerType>(FTy->getParamType(0)) ||
2487 !isa<PointerType>(FTy->getParamType(1)))
2488 continue;
2489 setDoesNotThrow(F);
2490 setDoesNotCapture(F, 1);
2491 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002492 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002493 if (FTy->getNumParams() != 4 ||
2494 !isa<PointerType>(FTy->getParamType(0)) ||
2495 !isa<PointerType>(FTy->getParamType(2)))
2496 continue;
2497 setDoesNotThrow(F);
2498 setDoesNotCapture(F, 1);
2499 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002500 }
2501 break;
2502 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002503 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002504 if (FTy->getNumParams() < 2 ||
2505 !isa<PointerType>(FTy->getParamType(0)))
2506 continue;
2507 // May throw; "open" is a valid pthread cancellation point.
2508 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002509 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002510 if (FTy->getNumParams() != 1 ||
2511 !isa<PointerType>(FTy->getReturnType()) ||
2512 !isa<PointerType>(FTy->getParamType(0)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002513 continue;
2514 setDoesNotThrow(F);
2515 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002516 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002517 }
2518 break;
2519 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002520 if (Name == "tmpfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002521 if (!isa<PointerType>(FTy->getReturnType()))
2522 continue;
2523 setDoesNotThrow(F);
2524 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002525 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002526 if (FTy->getNumParams() != 1 ||
2527 !isa<PointerType>(FTy->getParamType(0)))
2528 continue;
2529 setDoesNotThrow(F);
2530 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002531 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002532 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002533 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002534 if (Name == "htonl" ||
2535 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002536 setDoesNotThrow(F);
2537 setDoesNotAccessMemory(F);
2538 }
2539 break;
2540 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002541 if (Name == "ntohl" ||
2542 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002543 setDoesNotThrow(F);
2544 setDoesNotAccessMemory(F);
2545 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002546 break;
2547 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002548 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002549 if (FTy->getNumParams() != 2 ||
2550 !isa<PointerType>(FTy->getParamType(0)) ||
2551 !isa<PointerType>(FTy->getParamType(1)))
2552 continue;
2553 setDoesNotThrow(F);
2554 setDoesNotCapture(F, 1);
2555 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002556 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002557 if (FTy->getNumParams() != 3 ||
2558 !isa<PointerType>(FTy->getParamType(0)))
2559 continue;
2560 setDoesNotThrow(F);
2561 setDoesNotCapture(F, 1);
2562 }
2563 break;
2564 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002565 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002566 if (FTy->getNumParams() != 4 ||
2567 !isa<PointerType>(FTy->getParamType(3)))
2568 continue;
2569 // May throw; places call through function pointer.
2570 setDoesNotCapture(F, 4);
2571 }
2572 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002573 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002574 if (Name == "__strdup" ||
2575 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002576 if (FTy->getNumParams() < 1 ||
2577 !isa<PointerType>(FTy->getReturnType()) ||
2578 !isa<PointerType>(FTy->getParamType(0)))
2579 continue;
2580 setDoesNotThrow(F);
2581 setDoesNotAlias(F, 0);
2582 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002583 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002584 if (FTy->getNumParams() != 3 ||
2585 !isa<PointerType>(FTy->getParamType(1)))
2586 continue;
2587 setDoesNotThrow(F);
2588 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002589 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002590 if (FTy->getNumParams() != 1 ||
2591 !isa<PointerType>(FTy->getParamType(0)))
2592 continue;
2593 setDoesNotThrow(F);
2594 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002595 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002596 if (FTy->getNumParams() != 2 ||
2597 !isa<PointerType>(FTy->getParamType(1)))
2598 continue;
2599 setDoesNotThrow(F);
2600 setDoesNotCapture(F, 2);
2601 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002602 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002603 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002604 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002605 if (FTy->getNumParams() < 1 ||
2606 !isa<PointerType>(FTy->getParamType(0)))
2607 continue;
2608 setDoesNotThrow(F);
2609 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002610 } else if (Name == "\1stat64" ||
2611 Name == "\1lstat64" ||
2612 Name == "\1statvfs64" ||
2613 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002614 if (FTy->getNumParams() < 1 ||
Nick Lewycky225f7472009-02-15 22:47:25 +00002615 !isa<PointerType>(FTy->getParamType(0)) ||
2616 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002617 continue;
2618 setDoesNotThrow(F);
2619 setDoesNotCapture(F, 1);
2620 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002621 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002622 if (FTy->getNumParams() != 2 ||
2623 !isa<PointerType>(FTy->getReturnType()) ||
2624 !isa<PointerType>(FTy->getParamType(0)) ||
2625 !isa<PointerType>(FTy->getParamType(1)))
2626 continue;
2627 setDoesNotThrow(F);
2628 setDoesNotAlias(F, 0);
2629 setDoesNotCapture(F, 1);
2630 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002631 } else if (Name == "\1fseeko64" ||
2632 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002633 if (FTy->getNumParams() == 0 ||
2634 !isa<PointerType>(FTy->getParamType(0)))
2635 continue;
2636 setDoesNotThrow(F);
2637 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002638 } else if (Name == "\1tmpfile64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002639 if (!isa<PointerType>(FTy->getReturnType()))
2640 continue;
2641 setDoesNotThrow(F);
2642 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002643 } else if (Name == "\1fstat64" ||
2644 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002645 if (FTy->getNumParams() != 2 ||
2646 !isa<PointerType>(FTy->getParamType(1)))
2647 continue;
2648 setDoesNotThrow(F);
2649 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002650 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002651 if (FTy->getNumParams() < 2 ||
2652 !isa<PointerType>(FTy->getParamType(0)))
2653 continue;
2654 // May throw; "open" is a valid pthread cancellation point.
2655 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002656 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002657 break;
2658 }
2659 }
2660 return Modified;
2661}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002662
2663// TODO:
2664// Additional cases that we need to add to this file:
2665//
2666// cbrt:
2667// * cbrt(expN(X)) -> expN(x/3)
2668// * cbrt(sqrt(x)) -> pow(x,1/6)
2669// * cbrt(sqrt(x)) -> pow(x,1/9)
2670//
2671// cos, cosf, cosl:
2672// * cos(-x) -> cos(x)
2673//
2674// exp, expf, expl:
2675// * exp(log(x)) -> x
2676//
2677// log, logf, logl:
2678// * log(exp(x)) -> x
2679// * log(x**y) -> y*log(x)
2680// * log(exp(y)) -> y*log(e)
2681// * log(exp2(y)) -> y*log(2)
2682// * log(exp10(y)) -> y*log(10)
2683// * log(sqrt(x)) -> 0.5*log(x)
2684// * log(pow(x,y)) -> y*log(x)
2685//
2686// lround, lroundf, lroundl:
2687// * lround(cnst) -> cnst'
2688//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002689// pow, powf, powl:
2690// * pow(exp(x),y) -> exp(x*y)
2691// * pow(sqrt(x),y) -> pow(x,y*0.5)
2692// * pow(pow(x,y),z)-> pow(x,y*z)
2693//
2694// puts:
2695// * puts("") -> putchar("\n")
2696//
2697// round, roundf, roundl:
2698// * round(cnst) -> cnst'
2699//
2700// signbit:
2701// * signbit(cnst) -> cnst'
2702// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2703//
2704// sqrt, sqrtf, sqrtl:
2705// * sqrt(expN(x)) -> expN(x*0.5)
2706// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2707// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2708//
2709// stpcpy:
2710// * stpcpy(str, "literal") ->
2711// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2712// strrchr:
2713// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2714// (if c is a constant integer and s is a constant string)
2715// * strrchr(s1,0) -> strchr(s1,0)
2716//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002717// strpbrk:
2718// * strpbrk(s,a) -> offset_in_for(s,a)
2719// (if s and a are both constant strings)
2720// * strpbrk(s,"") -> 0
2721// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2722//
2723// strspn, strcspn:
2724// * strspn(s,a) -> const_int (if both args are constant)
2725// * strspn("",a) -> 0
2726// * strspn(s,"") -> 0
2727// * strcspn(s,a) -> const_int (if both args are constant)
2728// * strcspn("",a) -> 0
2729// * strcspn(s,"") -> strlen(a)
2730//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002731// tan, tanf, tanl:
2732// * tan(atan(x)) -> x
2733//
2734// trunc, truncf, truncl:
2735// * trunc(cnst) -> cnst'
2736//
2737//