blob: 1a351f0e06f9e7708969538b9f7f21b520854d7a [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 Christopher3a8bb732010-02-02 00:13:06 +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 Christophereae6ed12010-02-02 00:06:55 +0000235 Type::getInt8PtrTy(*Context),
236 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000237 Type::getInt32Ty(*Context),
Eric Christophereae6ed12010-02-02 00:06:55 +0000238 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,
Eric Christopher3a8bb732010-02-02 00:13:06 +0000324 Type::getInt32Ty(*Context),
325 /*isSigned*/true,
326 "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),
Eric Christopher3a8bb732010-02-02 00:13:06 +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;
Duncan Sands1df98592010-02-16 11:11:14 +0000360 if (File->getType()->isPointerTy())
Eric Christopher37c8b862009-10-07 21:14:25 +0000361 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000362 Type::getInt32Ty(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000363 Type::getInt32Ty(*Context), File->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000364 NULL);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000365 else
Eric Christopher37c8b862009-10-07 21:14:25 +0000366 F = M->getOrInsertFunction("fputc",
Eric Christopher3a8bb732010-02-02 00:13:06 +0000367 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;
Duncan Sands1df98592010-02-16 11:11:14 +0000387 if (File->getType()->isPointerTy())
Eric Christopher37c8b862009-10-07 21:14:25 +0000388 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000389 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;
Duncan Sands1df98592010-02-16 11:11:14 +0000412 if (File->getType()->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000413 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),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000417 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),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000423 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/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +0000437/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000438static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
439 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
440 UI != E; ++UI) {
441 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
442 if (IC->isEquality())
443 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
444 if (C->isNullValue())
445 continue;
446 // Unknown instruction.
447 return false;
448 }
449 return true;
450}
451
452//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000453// String and Memory LibCall Optimizations
454//===----------------------------------------------------------------------===//
455
456//===---------------------------------------===//
457// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000458namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000459struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000460 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000461 // Verify the "strcat" function prototype.
462 const FunctionType *FT = Callee->getFunctionType();
463 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000464 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000465 FT->getParamType(0) != FT->getReturnType() ||
466 FT->getParamType(1) != FT->getReturnType())
467 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000468
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000469 // Extract some information from the instruction
470 Value *Dst = CI->getOperand(1);
471 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000472
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000473 // See if we can get the length of the input string.
474 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000475 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000476 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000477
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000478 // Handle the simple, do-nothing case: strcat(x, "") -> x
479 if (Len == 0)
480 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000481
482 // These optimizations require TargetData.
483 if (!TD) return 0;
484
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000485 EmitStrLenMemCpy(Src, Dst, Len, B);
486 return Dst;
487 }
488
489 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000490 // We need to find the end of the destination string. That's where the
491 // memory is to be moved to. We just generate a call to strlen.
492 Value *DstLen = EmitStrLen(Dst, B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000493
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000494 // Now that we have the destination's length, we must index into the
495 // destination's pointer to get the actual memcpy destination (end of
496 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000497 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000498
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000499 // We have enough information to now generate the memcpy call to do the
500 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000501 EmitMemCpy(CpyDst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000502 ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000503 }
504};
505
506//===---------------------------------------===//
507// 'strncat' Optimizations
508
Chris Lattner3e8b6632009-09-02 06:11:42 +0000509struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000510 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
511 // Verify the "strncat" function prototype.
512 const FunctionType *FT = Callee->getFunctionType();
513 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000514 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000515 FT->getParamType(0) != FT->getReturnType() ||
516 FT->getParamType(1) != FT->getReturnType() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000517 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000518 return 0;
519
520 // Extract some information from the instruction
521 Value *Dst = CI->getOperand(1);
522 Value *Src = CI->getOperand(2);
523 uint64_t Len;
524
525 // We don't do anything if length is not constant
526 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
527 Len = LengthArg->getZExtValue();
528 else
529 return 0;
530
531 // See if we can get the length of the input string.
532 uint64_t SrcLen = GetStringLength(Src);
533 if (SrcLen == 0) return 0;
534 --SrcLen; // Unbias length.
535
536 // Handle the simple, do-nothing cases:
537 // strncat(x, "", c) -> x
538 // strncat(x, c, 0) -> x
539 if (SrcLen == 0 || Len == 0) return Dst;
540
Dan Gohmanf14d9192009-08-18 00:48:13 +0000541 // These optimizations require TargetData.
542 if (!TD) return 0;
543
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000544 // We don't optimize this case
545 if (Len < SrcLen) return 0;
546
547 // strncat(x, s, c) -> strcat(x, s)
548 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000549 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000550 return Dst;
551 }
552};
553
554//===---------------------------------------===//
555// 'strchr' Optimizations
556
Chris Lattner3e8b6632009-09-02 06:11:42 +0000557struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000558 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000559 // Verify the "strchr" function prototype.
560 const FunctionType *FT = Callee->getFunctionType();
561 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000562 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000563 FT->getParamType(0) != FT->getReturnType())
564 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000565
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000566 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000567
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000568 // If the second operand is non-constant, see if we can compute the length
569 // of the input string and turn this into memchr.
570 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
571 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000572 // These optimizations require TargetData.
573 if (!TD) return 0;
574
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000575 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000576 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000577 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000578
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000579 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Owen Anderson1d0be152009-08-13 21:58:54 +0000580 ConstantInt::get(TD->getIntPtrType(*Context), Len), B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000581 }
582
583 // Otherwise, the character is a constant, see if the first argument is
584 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000585 std::string Str;
586 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000587 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000588
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000589 // strchr can find the nul character.
590 Str += '\0';
591 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000592
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000593 // Compute the offset.
594 uint64_t i = 0;
595 while (1) {
596 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000597 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000598 // Did we find our match?
599 if (Str[i] == CharValue)
600 break;
601 ++i;
602 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000603
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000604 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000605 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000606 return B.CreateGEP(SrcStr, Idx, "strchr");
607 }
608};
609
610//===---------------------------------------===//
611// 'strcmp' Optimizations
612
Chris Lattner3e8b6632009-09-02 06:11:42 +0000613struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000614 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000615 // Verify the "strcmp" function prototype.
616 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000617 if (FT->getNumParams() != 2 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000618 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000619 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000620 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000621 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000622
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000623 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
624 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000625 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000626
Bill Wendling0582ae92009-03-13 04:39:26 +0000627 std::string Str1, Str2;
628 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
629 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000630
Bill Wendling0582ae92009-03-13 04:39:26 +0000631 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000632 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000633
Bill Wendling0582ae92009-03-13 04:39:26 +0000634 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000635 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000636
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000637 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000638 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000639 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000640 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000641
642 // strcmp(P, "x") -> memcmp(P, "x", 2)
643 uint64_t Len1 = GetStringLength(Str1P);
644 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000645 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000646 // These optimizations require TargetData.
647 if (!TD) return 0;
648
Nick Lewycky13a09e22008-12-21 00:19:21 +0000649 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000650 ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattner849832c2009-06-19 04:17:36 +0000651 std::min(Len1, Len2)), B);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000652 }
653
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000654 return 0;
655 }
656};
657
658//===---------------------------------------===//
659// 'strncmp' Optimizations
660
Chris Lattner3e8b6632009-09-02 06:11:42 +0000661struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000662 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000663 // Verify the "strncmp" function prototype.
664 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000665 if (FT->getNumParams() != 3 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000666 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000667 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000668 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000669 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000670 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000671
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000672 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
673 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000674 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000675
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000676 // Get the length argument if it is constant.
677 uint64_t Length;
678 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
679 Length = LengthArg->getZExtValue();
680 else
681 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000682
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000683 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000684 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000685
Bill Wendling0582ae92009-03-13 04:39:26 +0000686 std::string Str1, Str2;
687 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
688 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000689
Bill Wendling0582ae92009-03-13 04:39:26 +0000690 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000691 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000692
Bill Wendling0582ae92009-03-13 04:39:26 +0000693 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000694 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000695
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000696 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000697 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000698 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000699 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000700 return 0;
701 }
702};
703
704
705//===---------------------------------------===//
706// 'strcpy' Optimizations
707
Chris Lattner3e8b6632009-09-02 06:11:42 +0000708struct StrCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000709 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000710 // Verify the "strcpy" function prototype.
711 const FunctionType *FT = Callee->getFunctionType();
712 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
713 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000714 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000715 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000716
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000717 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
718 if (Dst == Src) // strcpy(x,x) -> x
719 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000720
Dan Gohmanf14d9192009-08-18 00:48:13 +0000721 // These optimizations require TargetData.
722 if (!TD) return 0;
723
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000724 // See if we can get the length of the input string.
725 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000726 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000727
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000728 // We have enough information to now generate the memcpy call to do the
729 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000730 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000731 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000732 return Dst;
733 }
734};
735
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000736//===---------------------------------------===//
737// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000738
Chris Lattner3e8b6632009-09-02 06:11:42 +0000739struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000740 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
741 const FunctionType *FT = Callee->getFunctionType();
742 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
743 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000744 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000745 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000746 return 0;
747
748 Value *Dst = CI->getOperand(1);
749 Value *Src = CI->getOperand(2);
750 Value *LenOp = CI->getOperand(3);
751
752 // See if we can get the length of the input string.
753 uint64_t SrcLen = GetStringLength(Src);
754 if (SrcLen == 0) return 0;
755 --SrcLen;
756
757 if (SrcLen == 0) {
758 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000759 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
760 B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000761 return Dst;
762 }
763
764 uint64_t Len;
765 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
766 Len = LengthArg->getZExtValue();
767 else
768 return 0;
769
770 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
771
Dan Gohmanf14d9192009-08-18 00:48:13 +0000772 // These optimizations require TargetData.
773 if (!TD) return 0;
774
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000775 // Let strncpy handle the zero padding
776 if (Len > SrcLen+1) return 0;
777
778 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000779 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000780 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000781
782 return Dst;
783 }
784};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000785
786//===---------------------------------------===//
787// 'strlen' Optimizations
788
Chris Lattner3e8b6632009-09-02 06:11:42 +0000789struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000790 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000791 const FunctionType *FT = Callee->getFunctionType();
792 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000793 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000794 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000795 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000796
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000797 Value *Src = CI->getOperand(1);
798
799 // Constant folding: strlen("xyz") -> 3
800 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000801 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000802
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000803 // strlen(x) != 0 --> *x != 0
804 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000805 if (IsOnlyUsedInZeroEqualityComparison(CI))
806 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
807 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000808 }
809};
810
811//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000812// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000813
Chris Lattner3e8b6632009-09-02 06:11:42 +0000814struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000815 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
816 const FunctionType *FT = Callee->getFunctionType();
817 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000818 !FT->getParamType(0)->isPointerTy() ||
819 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000820 return 0;
821
822 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000823 if (isa<ConstantPointerNull>(EndPtr)) {
824 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000825 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000826 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000827
828 return 0;
829 }
830};
831
Chris Lattner24604112009-12-16 09:32:05 +0000832//===---------------------------------------===//
833// 'strstr' Optimizations
834
835struct StrStrOpt : public LibCallOptimization {
836 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
837 const FunctionType *FT = Callee->getFunctionType();
838 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000839 !FT->getParamType(0)->isPointerTy() ||
840 !FT->getParamType(1)->isPointerTy() ||
841 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000842 return 0;
843
844 // fold strstr(x, x) -> x.
845 if (CI->getOperand(1) == CI->getOperand(2))
846 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000847
Chris Lattner24604112009-12-16 09:32:05 +0000848 // See if either input string is a constant string.
849 std::string SearchStr, ToFindStr;
850 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
851 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000852
Chris Lattner24604112009-12-16 09:32:05 +0000853 // fold strstr(x, "") -> x.
854 if (HasStr2 && ToFindStr.empty())
855 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000856
Chris Lattner24604112009-12-16 09:32:05 +0000857 // If both strings are known, constant fold it.
858 if (HasStr1 && HasStr2) {
859 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000860
Chris Lattner24604112009-12-16 09:32:05 +0000861 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
862 return Constant::getNullValue(CI->getType());
863
864 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
865 Value *Result = CastToCStr(CI->getOperand(1), B);
866 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
867 return B.CreateBitCast(Result, CI->getType());
868 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000869
Chris Lattner24604112009-12-16 09:32:05 +0000870 // fold strstr(x, "y") -> strchr(x, 'y').
871 if (HasStr2 && ToFindStr.size() == 1)
872 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B),
873 CI->getType());
874 return 0;
875 }
876};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000877
Nick Lewycky4c498412009-02-13 15:31:46 +0000878
879//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000880// 'memcmp' Optimizations
881
Chris Lattner3e8b6632009-09-02 06:11:42 +0000882struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000883 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000884 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000885 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
886 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000887 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000888 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000889
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000890 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000891
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000892 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000893 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000894
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000895 // Make sure we have a constant length.
896 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000897 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000898 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000899
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000900 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000901 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000902
903 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
904 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
905 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +0000906 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000907 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000908
Benjamin Kramer992a6372009-11-05 17:44:22 +0000909 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
910 std::string LHSStr, RHSStr;
911 if (GetConstantStringInfo(LHS, LHSStr) &&
912 GetConstantStringInfo(RHS, RHSStr)) {
913 // Make sure we're not reading out-of-bounds memory.
914 if (Len > LHSStr.length() || Len > RHSStr.length())
915 return 0;
916 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
917 return ConstantInt::get(CI->getType(), Ret);
918 }
919
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000920 return 0;
921 }
922};
923
924//===---------------------------------------===//
925// 'memcpy' Optimizations
926
Chris Lattner3e8b6632009-09-02 06:11:42 +0000927struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000928 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000929 // These optimizations require TargetData.
930 if (!TD) return 0;
931
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000932 const FunctionType *FT = Callee->getFunctionType();
933 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000934 !FT->getParamType(0)->isPointerTy() ||
935 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000936 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000937 return 0;
938
939 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
940 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
941 return CI->getOperand(1);
942 }
943};
944
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000945//===---------------------------------------===//
946// 'memmove' Optimizations
947
Chris Lattner3e8b6632009-09-02 06:11:42 +0000948struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000949 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000950 // These optimizations require TargetData.
951 if (!TD) return 0;
952
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000953 const FunctionType *FT = Callee->getFunctionType();
954 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000955 !FT->getParamType(0)->isPointerTy() ||
956 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000957 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000958 return 0;
959
960 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopher80bf1d52009-11-21 01:01:30 +0000961 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000962 return CI->getOperand(1);
963 }
964};
965
966//===---------------------------------------===//
967// 'memset' Optimizations
968
Chris Lattner3e8b6632009-09-02 06:11:42 +0000969struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000970 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000971 // These optimizations require TargetData.
972 if (!TD) return 0;
973
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000974 const FunctionType *FT = Callee->getFunctionType();
975 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000976 !FT->getParamType(0)->isPointerTy() ||
977 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000978 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000979 return 0;
980
981 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000982 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
983 false);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000984 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000985 return CI->getOperand(1);
986 }
987};
988
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000989//===----------------------------------------------------------------------===//
Eric Christopher80bf1d52009-11-21 01:01:30 +0000990// Object Size Checking Optimizations
991//===----------------------------------------------------------------------===//
992
993//===---------------------------------------===//
Eric Christopher80bf1d52009-11-21 01:01:30 +0000994// 'memcpy_chk' Optimizations
995
996struct MemCpyChkOpt : public LibCallOptimization {
997 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
998 // These optimizations require TargetData.
999 if (!TD) return 0;
1000
1001 const FunctionType *FT = Callee->getFunctionType();
1002 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +00001003 !FT->getParamType(0)->isPointerTy() ||
1004 !FT->getParamType(1)->isPointerTy() ||
1005 !FT->getParamType(3)->isIntegerTy() ||
Eric Christopherf734be22009-12-22 01:23:51 +00001006 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eric Christopher80bf1d52009-11-21 01:01:30 +00001007 return 0;
1008
Evan Chenga79eb382010-03-05 20:59:47 +00001009 ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1010 if (!ObjSizeCI)
Eric Christopher80bf1d52009-11-21 01:01:30 +00001011 return 0;
Evan Chenga79eb382010-03-05 20:59:47 +00001012 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
1013 if (ObjSizeCI->isAllOnesValue() ||
1014 (SizeCI && ObjSizeCI->getValue().uge(SizeCI->getValue()))) {
Eric Christopher80bf1d52009-11-21 01:01:30 +00001015 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
1016 return CI->getOperand(1);
1017 }
1018
1019 return 0;
1020 }
1021};
1022
1023//===---------------------------------------===//
1024// 'memset_chk' Optimizations
1025
1026struct MemSetChkOpt : public LibCallOptimization {
1027 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1028 // These optimizations require TargetData.
1029 if (!TD) return 0;
1030
1031 const FunctionType *FT = Callee->getFunctionType();
1032 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +00001033 !FT->getParamType(0)->isPointerTy() ||
1034 !FT->getParamType(1)->isIntegerTy() ||
1035 !FT->getParamType(3)->isIntegerTy() ||
Eric Christopher80bf1d52009-11-21 01:01:30 +00001036 FT->getParamType(2) != TD->getIntPtrType(*Context))
1037 return 0;
1038
Evan Chenga79eb382010-03-05 20:59:47 +00001039 ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1040 if (!ObjSizeCI)
Eric Christopher80bf1d52009-11-21 01:01:30 +00001041 return 0;
Evan Chenga79eb382010-03-05 20:59:47 +00001042 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
1043 if (ObjSizeCI->isAllOnesValue() ||
1044 (SizeCI && ObjSizeCI->getValue().uge(SizeCI->getValue()))) {
Eric Christopher80bf1d52009-11-21 01:01:30 +00001045 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1046 false);
1047 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
1048 return CI->getOperand(1);
1049 }
1050
1051 return 0;
1052 }
1053};
1054
1055//===---------------------------------------===//
1056// 'memmove_chk' Optimizations
1057
1058struct MemMoveChkOpt : public LibCallOptimization {
1059 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1060 // These optimizations require TargetData.
1061 if (!TD) return 0;
1062
1063 const FunctionType *FT = Callee->getFunctionType();
1064 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +00001065 !FT->getParamType(0)->isPointerTy() ||
1066 !FT->getParamType(1)->isPointerTy() ||
1067 !FT->getParamType(3)->isIntegerTy() ||
Eric Christopher80bf1d52009-11-21 01:01:30 +00001068 FT->getParamType(2) != TD->getIntPtrType(*Context))
1069 return 0;
1070
Evan Chenga79eb382010-03-05 20:59:47 +00001071 ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1072 if (!ObjSizeCI)
Eric Christopher80bf1d52009-11-21 01:01:30 +00001073 return 0;
Evan Chenga79eb382010-03-05 20:59:47 +00001074 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
1075 if (ObjSizeCI->isAllOnesValue() ||
1076 (SizeCI && ObjSizeCI->getValue().uge(SizeCI->getValue()))) {
Eric Christopher80bf1d52009-11-21 01:01:30 +00001077 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
1078 1, B);
1079 return CI->getOperand(1);
1080 }
1081
1082 return 0;
1083 }
1084};
1085
Eric Christopher7672d042010-01-23 05:29:06 +00001086struct StrCpyChkOpt : public LibCallOptimization {
1087 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Eric Christopher7672d042010-01-23 05:29:06 +00001088 const FunctionType *FT = Callee->getFunctionType();
1089 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +00001090 !FT->getParamType(0)->isPointerTy() ||
1091 !FT->getParamType(1)->isPointerTy())
Eric Christopher7672d042010-01-23 05:29:06 +00001092 return 0;
1093
Evan Chenga79eb382010-03-05 20:59:47 +00001094 ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
1095 if (!ObjSizeCI)
Eric Christopher7672d042010-01-23 05:29:06 +00001096 return 0;
1097
Eric Christopher0c6a8f92010-02-03 00:21:58 +00001098 // If a) we don't have any length information, or b) we know this will
1099 // fit then just lower to a plain strcpy. Otherwise we'll keep our
1100 // strcpy_chk call which may fail at runtime if the size is too long.
1101 // TODO: It might be nice to get a maximum length out of the possible
1102 // string lengths for varying.
Evan Chenga79eb382010-03-05 20:59:47 +00001103 if (ObjSizeCI->isAllOnesValue() ||
1104 ObjSizeCI->getZExtValue() >= GetStringLength(CI->getOperand(2)))
Eric Christopher7672d042010-01-23 05:29:06 +00001105 return EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B);
1106
1107 return 0;
1108 }
1109};
1110
1111
Eric Christopher80bf1d52009-11-21 01:01:30 +00001112//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001113// Math Library Optimizations
1114//===----------------------------------------------------------------------===//
1115
1116//===---------------------------------------===//
1117// 'pow*' Optimizations
1118
Chris Lattner3e8b6632009-09-02 06:11:42 +00001119struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001120 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001121 const FunctionType *FT = Callee->getFunctionType();
1122 // Just make sure this has 2 arguments of the same FP type, which match the
1123 // result type.
1124 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1125 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001126 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001127 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001128
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001129 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
1130 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1131 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
1132 return Op1C;
1133 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +00001134 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001135 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001136
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001137 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1138 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001139
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001140 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001141 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001142
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001143 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +00001144 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1145 // This is faster than calling pow, and still handles negative zero
1146 // and negative infinite correctly.
1147 // TODO: In fast-math mode, this could be just sqrt(x).
1148 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +00001149 Value *Inf = ConstantFP::getInfinity(CI->getType());
1150 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +00001151 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1152 Callee->getAttributes());
1153 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1154 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +00001155 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
1156 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
1157 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001158 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001159
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001160 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1161 return Op1;
1162 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001163 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001164 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001165 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001166 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001167 return 0;
1168 }
1169};
1170
1171//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +00001172// 'exp2' Optimizations
1173
Chris Lattner3e8b6632009-09-02 06:11:42 +00001174struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001175 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +00001176 const FunctionType *FT = Callee->getFunctionType();
1177 // Just make sure this has 1 argument of FP type, which matches the
1178 // result type.
1179 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001180 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001181 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001182
Chris Lattnere818f772008-05-02 18:43:35 +00001183 Value *Op = CI->getOperand(1);
1184 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1185 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1186 Value *LdExpArg = 0;
1187 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1188 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001189 LdExpArg = B.CreateSExt(OpC->getOperand(0),
1190 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001191 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1192 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001193 LdExpArg = B.CreateZExt(OpC->getOperand(0),
1194 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001195 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001196
Chris Lattnere818f772008-05-02 18:43:35 +00001197 if (LdExpArg) {
1198 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001199 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001200 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001201 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001202 Name = "ldexp";
1203 else
1204 Name = "ldexpl";
1205
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001206 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001207 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001208 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +00001209
1210 Module *M = Caller->getParent();
1211 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +00001212 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +00001213 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001214 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1215 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1216 CI->setCallingConv(F->getCallingConv());
1217
1218 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +00001219 }
1220 return 0;
1221 }
1222};
Chris Lattnere818f772008-05-02 18:43:35 +00001223
1224//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001225// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1226
Chris Lattner3e8b6632009-09-02 06:11:42 +00001227struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001228 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001229 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001230 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1231 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001232 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001233
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001234 // If this is something like 'floor((double)floatval)', convert to floorf.
1235 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001236 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001237 return 0;
1238
1239 // floor((double)floatval) -> (double)floorf(floatval)
1240 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +00001241 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
1242 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +00001243 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001244 }
1245};
1246
1247//===----------------------------------------------------------------------===//
1248// Integer Optimizations
1249//===----------------------------------------------------------------------===//
1250
1251//===---------------------------------------===//
1252// 'ffs*' Optimizations
1253
Chris Lattner3e8b6632009-09-02 06:11:42 +00001254struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001255 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001256 const FunctionType *FT = Callee->getFunctionType();
1257 // Just make sure this has 2 arguments of the same FP type, which match the
1258 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +00001259 if (FT->getNumParams() != 1 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001260 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +00001261 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001262 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001263
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001264 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001265
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001266 // Constant fold.
1267 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1268 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +00001269 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +00001270 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001271 CI->getValue().countTrailingZeros()+1);
1272 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001273
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001274 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1275 const Type *ArgType = Op->getType();
1276 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1277 Intrinsic::cttz, &ArgType, 1);
1278 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +00001279 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +00001280 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001281
Owen Andersona7235ea2009-07-31 20:28:14 +00001282 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001283 return B.CreateSelect(Cond, V,
1284 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001285 }
1286};
1287
1288//===---------------------------------------===//
1289// 'isdigit' Optimizations
1290
Chris Lattner3e8b6632009-09-02 06:11:42 +00001291struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001292 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001293 const FunctionType *FT = Callee->getFunctionType();
1294 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +00001295 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001296 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001297 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001298
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001299 // isdigit(c) -> (c-'0') <u 10
1300 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001301 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001302 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001303 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001304 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001305 return B.CreateZExt(Op, CI->getType());
1306 }
1307};
1308
1309//===---------------------------------------===//
1310// 'isascii' Optimizations
1311
Chris Lattner3e8b6632009-09-02 06:11:42 +00001312struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001313 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001314 const FunctionType *FT = Callee->getFunctionType();
1315 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +00001316 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001317 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001318 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001319
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001320 // isascii(c) -> c <u 128
1321 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001322 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001323 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001324 return B.CreateZExt(Op, CI->getType());
1325 }
1326};
Eric Christopher37c8b862009-10-07 21:14:25 +00001327
Chris Lattner313f0e62008-06-09 08:26:51 +00001328//===---------------------------------------===//
1329// 'abs', 'labs', 'llabs' Optimizations
1330
Chris Lattner3e8b6632009-09-02 06:11:42 +00001331struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001332 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001333 const FunctionType *FT = Callee->getFunctionType();
1334 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +00001335 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +00001336 FT->getParamType(0) != FT->getReturnType())
1337 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001338
Chris Lattner313f0e62008-06-09 08:26:51 +00001339 // abs(x) -> x >s -1 ? x : -x
1340 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001341 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001342 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001343 "ispos");
1344 Value *Neg = B.CreateNeg(Op, "neg");
1345 return B.CreateSelect(Pos, Op, Neg);
1346 }
1347};
Eric Christopher37c8b862009-10-07 21:14:25 +00001348
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001349
1350//===---------------------------------------===//
1351// 'toascii' Optimizations
1352
Chris Lattner3e8b6632009-09-02 06:11:42 +00001353struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001354 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001355 const FunctionType *FT = Callee->getFunctionType();
1356 // We require i32(i32)
1357 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001358 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001359 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001360
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001361 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001362 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +00001363 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001364 }
1365};
1366
1367//===----------------------------------------------------------------------===//
1368// Formatting and IO Optimizations
1369//===----------------------------------------------------------------------===//
1370
1371//===---------------------------------------===//
1372// 'printf' Optimizations
1373
Chris Lattner3e8b6632009-09-02 06:11:42 +00001374struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001375 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001376 // Require one fixed pointer argument and an integer/void result.
1377 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001378 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1379 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001380 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001381 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001382
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001383 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001384 std::string FormatStr;
1385 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1386 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001387
1388 // Empty format string -> noop.
1389 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001390 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001391 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001392
Chris Lattner74965f22009-11-09 04:57:04 +00001393 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1394 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001395 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001396 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
1397 FormatStr[0]), B);
1398 if (CI->use_empty()) return CI;
1399 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001400 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001401
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001402 // printf("foo\n") --> puts("foo")
1403 if (FormatStr[FormatStr.size()-1] == '\n' &&
1404 FormatStr.find('%') == std::string::npos) { // no format characters.
1405 // Create a string literal with no \n on it. We expect the constant merge
1406 // pass to be run after this pass, to merge duplicate strings.
1407 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001408 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001409 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1410 GlobalVariable::InternalLinkage, C, "str");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001411 EmitPutS(C, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001412 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001413 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001414 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001415
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001416 // Optimize specific format strings.
1417 // printf("%c", chr) --> putchar(*(i8*)dst)
1418 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
Duncan Sands1df98592010-02-16 11:11:14 +00001419 CI->getOperand(2)->getType()->isIntegerTy()) {
Chris Lattner74965f22009-11-09 04:57:04 +00001420 Value *Res = EmitPutChar(CI->getOperand(2), B);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001421
Chris Lattner74965f22009-11-09 04:57:04 +00001422 if (CI->use_empty()) return CI;
1423 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001424 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001425
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001426 // printf("%s\n", str) --> puts(str)
1427 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
Duncan Sands1df98592010-02-16 11:11:14 +00001428 CI->getOperand(2)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001429 CI->use_empty()) {
1430 EmitPutS(CI->getOperand(2), B);
1431 return CI;
1432 }
1433 return 0;
1434 }
1435};
1436
1437//===---------------------------------------===//
1438// 'sprintf' Optimizations
1439
Chris Lattner3e8b6632009-09-02 06:11:42 +00001440struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001441 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001442 // Require two fixed pointer arguments and an integer result.
1443 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001444 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1445 !FT->getParamType(1)->isPointerTy() ||
1446 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001447 return 0;
1448
1449 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001450 std::string FormatStr;
1451 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1452 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001453
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001454 // If we just have a format string (nothing else crazy) transform it.
1455 if (CI->getNumOperands() == 3) {
1456 // Make sure there's no % in the constant array. We could try to handle
1457 // %% -> % in the future if we cared.
1458 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1459 if (FormatStr[i] == '%')
1460 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001461
1462 // These optimizations require TargetData.
1463 if (!TD) return 0;
1464
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001465 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1466 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001467 ConstantInt::get
1468 (TD->getIntPtrType(*Context), FormatStr.size()+1),1,B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001469 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001470 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001471
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001472 // The remaining optimizations require the format string to be "%s" or "%c"
1473 // and have an extra operand.
1474 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1475 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001476
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001477 // Decode the second character of the format string.
1478 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001479 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Duncan Sands1df98592010-02-16 11:11:14 +00001480 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001481 Value *V = B.CreateTrunc(CI->getOperand(3),
1482 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001483 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1484 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001485 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1486 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001487 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001488
Owen Andersoneed707b2009-07-24 23:12:02 +00001489 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001490 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001491
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001492 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001493 // These optimizations require TargetData.
1494 if (!TD) return 0;
1495
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001496 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Duncan Sands1df98592010-02-16 11:11:14 +00001497 if (!CI->getOperand(3)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001498
1499 Value *Len = EmitStrLen(CI->getOperand(3), B);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001500 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001501 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001502 "leninc");
1503 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001504
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001505 // The sprintf result is the unincremented number of bytes in the string.
1506 return B.CreateIntCast(Len, CI->getType(), false);
1507 }
1508 return 0;
1509 }
1510};
1511
1512//===---------------------------------------===//
1513// 'fwrite' Optimizations
1514
Chris Lattner3e8b6632009-09-02 06:11:42 +00001515struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001516 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001517 // Require a pointer, an integer, an integer, a pointer, returning integer.
1518 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001519 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1520 !FT->getParamType(1)->isIntegerTy() ||
1521 !FT->getParamType(2)->isIntegerTy() ||
1522 !FT->getParamType(3)->isPointerTy() ||
1523 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001524 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001525
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001526 // Get the element size and count.
1527 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1528 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1529 if (!SizeC || !CountC) return 0;
1530 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001531
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001532 // If this is writing zero records, remove the call (it's a noop).
1533 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001534 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001535
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001536 // If this is writing one byte, turn it into fputc.
1537 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1538 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1539 EmitFPutC(Char, CI->getOperand(4), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001540 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001541 }
1542
1543 return 0;
1544 }
1545};
1546
1547//===---------------------------------------===//
1548// 'fputs' Optimizations
1549
Chris Lattner3e8b6632009-09-02 06:11:42 +00001550struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001551 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001552 // These optimizations require TargetData.
1553 if (!TD) return 0;
1554
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001555 // Require two pointers. Also, we can't optimize if return value is used.
1556 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001557 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1558 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001559 !CI->use_empty())
1560 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001561
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001562 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1563 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001564 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001565 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001566 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001567 CI->getOperand(2), B);
1568 return CI; // Known to have no uses (see above).
1569 }
1570};
1571
1572//===---------------------------------------===//
1573// 'fprintf' Optimizations
1574
Chris Lattner3e8b6632009-09-02 06:11:42 +00001575struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001576 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001577 // Require two fixed paramters as pointers and integer result.
1578 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001579 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1580 !FT->getParamType(1)->isPointerTy() ||
1581 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001582 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001583
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001584 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001585 std::string FormatStr;
1586 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1587 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001588
1589 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1590 if (CI->getNumOperands() == 3) {
1591 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1592 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001593 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001594
1595 // These optimizations require TargetData.
1596 if (!TD) return 0;
1597
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001598 EmitFWrite(CI->getOperand(2),
1599 ConstantInt::get(TD->getIntPtrType(*Context),
1600 FormatStr.size()),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001601 CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001602 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001603 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001604
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001605 // The remaining optimizations require the format string to be "%s" or "%c"
1606 // and have an extra operand.
1607 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1608 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001609
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001610 // Decode the second character of the format string.
1611 if (FormatStr[1] == 'c') {
1612 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
Duncan Sands1df98592010-02-16 11:11:14 +00001613 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001614 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001615 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001616 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001617
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001618 if (FormatStr[1] == 's') {
1619 // fprintf(F, "%s", str) -> fputs(str, F)
Duncan Sands1df98592010-02-16 11:11:14 +00001620 if (!CI->getOperand(3)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001621 return 0;
1622 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1623 return CI;
1624 }
1625 return 0;
1626 }
1627};
1628
Bill Wendlingac178222008-05-05 21:37:59 +00001629} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001630
1631//===----------------------------------------------------------------------===//
1632// SimplifyLibCalls Pass Implementation
1633//===----------------------------------------------------------------------===//
1634
1635namespace {
1636 /// This pass optimizes well known library functions from libc and libm.
1637 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001638 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001639 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001640 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001641 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1642 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001643 StrToOpt StrTo; StrStrOpt StrStr;
1644 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001645 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001646 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001647 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001648 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1649 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001650 // Formatting and IO Optimizations
1651 SPrintFOpt SPrintF; PrintFOpt PrintF;
1652 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001653
1654 // Object Size Checking
Eric Christopher80bf1d52009-11-21 01:01:30 +00001655 MemCpyChkOpt MemCpyChk; MemSetChkOpt MemSetChk; MemMoveChkOpt MemMoveChk;
Eric Christopher7672d042010-01-23 05:29:06 +00001656 StrCpyChkOpt StrCpyChk;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001657
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001658 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001659 public:
1660 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001661 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001662
1663 void InitOptimizations();
1664 bool runOnFunction(Function &F);
1665
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001666 void setDoesNotAccessMemory(Function &F);
1667 void setOnlyReadsMemory(Function &F);
1668 void setDoesNotThrow(Function &F);
1669 void setDoesNotCapture(Function &F, unsigned n);
1670 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001671 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001672
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001673 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001674 }
1675 };
1676 char SimplifyLibCalls::ID = 0;
1677} // end anonymous namespace.
1678
1679static RegisterPass<SimplifyLibCalls>
1680X("simplify-libcalls", "Simplify well-known library calls");
1681
1682// Public interface to the Simplify LibCalls pass.
1683FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001684 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001685}
1686
1687/// Optimizations - Populate the Optimizations map with all the optimizations
1688/// we know.
1689void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001690 // String and Memory LibCall Optimizations
1691 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001692 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001693 Optimizations["strchr"] = &StrChr;
1694 Optimizations["strcmp"] = &StrCmp;
1695 Optimizations["strncmp"] = &StrNCmp;
1696 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001697 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001698 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001699 Optimizations["strtol"] = &StrTo;
1700 Optimizations["strtod"] = &StrTo;
1701 Optimizations["strtof"] = &StrTo;
1702 Optimizations["strtoul"] = &StrTo;
1703 Optimizations["strtoll"] = &StrTo;
1704 Optimizations["strtold"] = &StrTo;
1705 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001706 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001707 Optimizations["memcmp"] = &MemCmp;
1708 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001709 Optimizations["memmove"] = &MemMove;
1710 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001711
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001712 // Math Library Optimizations
1713 Optimizations["powf"] = &Pow;
1714 Optimizations["pow"] = &Pow;
1715 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001716 Optimizations["llvm.pow.f32"] = &Pow;
1717 Optimizations["llvm.pow.f64"] = &Pow;
1718 Optimizations["llvm.pow.f80"] = &Pow;
1719 Optimizations["llvm.pow.f128"] = &Pow;
1720 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001721 Optimizations["exp2l"] = &Exp2;
1722 Optimizations["exp2"] = &Exp2;
1723 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001724 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1725 Optimizations["llvm.exp2.f128"] = &Exp2;
1726 Optimizations["llvm.exp2.f80"] = &Exp2;
1727 Optimizations["llvm.exp2.f64"] = &Exp2;
1728 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001729
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001730#ifdef HAVE_FLOORF
1731 Optimizations["floor"] = &UnaryDoubleFP;
1732#endif
1733#ifdef HAVE_CEILF
1734 Optimizations["ceil"] = &UnaryDoubleFP;
1735#endif
1736#ifdef HAVE_ROUNDF
1737 Optimizations["round"] = &UnaryDoubleFP;
1738#endif
1739#ifdef HAVE_RINTF
1740 Optimizations["rint"] = &UnaryDoubleFP;
1741#endif
1742#ifdef HAVE_NEARBYINTF
1743 Optimizations["nearbyint"] = &UnaryDoubleFP;
1744#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001745
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001746 // Integer Optimizations
1747 Optimizations["ffs"] = &FFS;
1748 Optimizations["ffsl"] = &FFS;
1749 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001750 Optimizations["abs"] = &Abs;
1751 Optimizations["labs"] = &Abs;
1752 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001753 Optimizations["isdigit"] = &IsDigit;
1754 Optimizations["isascii"] = &IsAscii;
1755 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001756
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001757 // Formatting and IO Optimizations
1758 Optimizations["sprintf"] = &SPrintF;
1759 Optimizations["printf"] = &PrintF;
1760 Optimizations["fwrite"] = &FWrite;
1761 Optimizations["fputs"] = &FPuts;
1762 Optimizations["fprintf"] = &FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001763
1764 // Object Size Checking
Eric Christopher80bf1d52009-11-21 01:01:30 +00001765 Optimizations["__memcpy_chk"] = &MemCpyChk;
1766 Optimizations["__memset_chk"] = &MemSetChk;
1767 Optimizations["__memmove_chk"] = &MemMoveChk;
Eric Christopher7672d042010-01-23 05:29:06 +00001768 Optimizations["__strcpy_chk"] = &StrCpyChk;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001769}
1770
1771
1772/// runOnFunction - Top level algorithm.
1773///
1774bool SimplifyLibCalls::runOnFunction(Function &F) {
1775 if (Optimizations.empty())
1776 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001777
Dan Gohmanf14d9192009-08-18 00:48:13 +00001778 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001779
Owen Andersone922c022009-07-22 00:24:57 +00001780 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001781
1782 bool Changed = false;
1783 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1784 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1785 // Ignore non-calls.
1786 CallInst *CI = dyn_cast<CallInst>(I++);
1787 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001788
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001789 // Ignore indirect calls and calls to non-external functions.
1790 Function *Callee = CI->getCalledFunction();
1791 if (Callee == 0 || !Callee->isDeclaration() ||
1792 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1793 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001794
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001795 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001796 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1797 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001798
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001799 // Set the builder to the instruction after the call.
1800 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001801
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001802 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001803 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001804 if (Result == 0) continue;
1805
David Greene6a6b90e2010-01-05 01:27:21 +00001806 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1807 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001808
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001809 // Something changed!
1810 Changed = true;
1811 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001812
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001813 // Inspect the instruction after the call (which was potentially just
1814 // added) next.
1815 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001816
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001817 if (CI != Result && !CI->use_empty()) {
1818 CI->replaceAllUsesWith(Result);
1819 if (!Result->hasName())
1820 Result->takeName(CI);
1821 }
1822 CI->eraseFromParent();
1823 }
1824 }
1825 return Changed;
1826}
1827
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001828// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001829
1830void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1831 if (!F.doesNotAccessMemory()) {
1832 F.setDoesNotAccessMemory();
1833 ++NumAnnotated;
1834 Modified = true;
1835 }
1836}
1837void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1838 if (!F.onlyReadsMemory()) {
1839 F.setOnlyReadsMemory();
1840 ++NumAnnotated;
1841 Modified = true;
1842 }
1843}
1844void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1845 if (!F.doesNotThrow()) {
1846 F.setDoesNotThrow();
1847 ++NumAnnotated;
1848 Modified = true;
1849 }
1850}
1851void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1852 if (!F.doesNotCapture(n)) {
1853 F.setDoesNotCapture(n);
1854 ++NumAnnotated;
1855 Modified = true;
1856 }
1857}
1858void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1859 if (!F.doesNotAlias(n)) {
1860 F.setDoesNotAlias(n);
1861 ++NumAnnotated;
1862 Modified = true;
1863 }
1864}
1865
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001866/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001867///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001868bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001869 Modified = false;
1870 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1871 Function &F = *I;
1872 if (!F.isDeclaration())
1873 continue;
1874
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001875 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001876 continue;
1877
1878 const FunctionType *FTy = F.getFunctionType();
1879
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001880 StringRef Name = F.getName();
1881 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001882 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001883 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001884 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001885 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001886 continue;
1887 setOnlyReadsMemory(F);
1888 setDoesNotThrow(F);
1889 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001890 } else if (Name == "strcpy" ||
1891 Name == "stpcpy" ||
1892 Name == "strcat" ||
1893 Name == "strtol" ||
1894 Name == "strtod" ||
1895 Name == "strtof" ||
1896 Name == "strtoul" ||
1897 Name == "strtoll" ||
1898 Name == "strtold" ||
1899 Name == "strncat" ||
1900 Name == "strncpy" ||
1901 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001902 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001903 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001904 continue;
1905 setDoesNotThrow(F);
1906 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001907 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001908 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001909 !FTy->getParamType(0)->isPointerTy() ||
1910 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001911 continue;
1912 setDoesNotThrow(F);
1913 setDoesNotCapture(F, 1);
1914 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001915 } else if (Name == "strcmp" ||
1916 Name == "strspn" ||
1917 Name == "strncmp" ||
1918 Name ==" strcspn" ||
1919 Name == "strcoll" ||
1920 Name == "strcasecmp" ||
1921 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001922 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001923 !FTy->getParamType(0)->isPointerTy() ||
1924 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001925 continue;
1926 setOnlyReadsMemory(F);
1927 setDoesNotThrow(F);
1928 setDoesNotCapture(F, 1);
1929 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001930 } else if (Name == "strstr" ||
1931 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001932 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001933 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001934 continue;
1935 setOnlyReadsMemory(F);
1936 setDoesNotThrow(F);
1937 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001938 } else if (Name == "strtok" ||
1939 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001940 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001941 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001942 continue;
1943 setDoesNotThrow(F);
1944 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001945 } else if (Name == "scanf" ||
1946 Name == "setbuf" ||
1947 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001948 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001949 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001950 continue;
1951 setDoesNotThrow(F);
1952 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001953 } else if (Name == "strdup" ||
1954 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001955 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001956 !FTy->getReturnType()->isPointerTy() ||
1957 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001958 continue;
1959 setDoesNotThrow(F);
1960 setDoesNotAlias(F, 0);
1961 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001962 } else if (Name == "stat" ||
1963 Name == "sscanf" ||
1964 Name == "sprintf" ||
1965 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001966 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001967 !FTy->getParamType(0)->isPointerTy() ||
1968 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001969 continue;
1970 setDoesNotThrow(F);
1971 setDoesNotCapture(F, 1);
1972 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001973 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001974 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001975 !FTy->getParamType(0)->isPointerTy() ||
1976 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001977 continue;
1978 setDoesNotThrow(F);
1979 setDoesNotCapture(F, 1);
1980 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001981 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001982 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001983 !FTy->getParamType(1)->isPointerTy() ||
1984 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001985 continue;
1986 setDoesNotThrow(F);
1987 setDoesNotCapture(F, 2);
1988 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001989 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001990 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001991 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001992 continue;
1993 // May throw; "system" is a valid pthread cancellation point.
1994 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001995 }
1996 break;
1997 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001998 if (Name == "malloc") {
1999 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002000 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00002001 continue;
2002 setDoesNotThrow(F);
2003 setDoesNotAlias(F, 0);
2004 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002005 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002006 !FTy->getParamType(0)->isPointerTy() ||
2007 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002008 continue;
2009 setOnlyReadsMemory(F);
2010 setDoesNotThrow(F);
2011 setDoesNotCapture(F, 1);
2012 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002013 } else if (Name == "memchr" ||
2014 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002015 if (FTy->getNumParams() != 3)
2016 continue;
2017 setOnlyReadsMemory(F);
2018 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002019 } else if (Name == "modf" ||
2020 Name == "modff" ||
2021 Name == "modfl" ||
2022 Name == "memcpy" ||
2023 Name == "memccpy" ||
2024 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002025 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002026 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002027 continue;
2028 setDoesNotThrow(F);
2029 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002030 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00002031 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002032 continue;
2033 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002034 } else if (Name == "mkdir" ||
2035 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002036 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002037 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002038 continue;
2039 setDoesNotThrow(F);
2040 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002041 }
2042 break;
2043 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002044 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002045 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002046 !FTy->getParamType(0)->isPointerTy() ||
2047 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002048 continue;
2049 setDoesNotThrow(F);
2050 setDoesNotAlias(F, 0);
2051 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002052 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002053 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002054 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002055 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002056 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002057 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002058 } else if (Name == "rmdir" ||
2059 Name == "rewind" ||
2060 Name == "remove" ||
2061 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002062 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002063 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002064 continue;
2065 setDoesNotThrow(F);
2066 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002067 } else if (Name == "rename" ||
2068 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002069 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002070 !FTy->getParamType(0)->isPointerTy() ||
2071 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002072 continue;
2073 setDoesNotThrow(F);
2074 setDoesNotCapture(F, 1);
2075 setDoesNotCapture(F, 2);
2076 }
2077 break;
2078 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002079 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002080 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002081 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002082 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002083 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002084 setDoesNotCapture(F, 2);
2085 }
2086 break;
2087 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002088 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002089 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002090 !FTy->getParamType(0)->isPointerTy() ||
2091 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002092 continue;
2093 setDoesNotThrow(F);
2094 setDoesNotCapture(F, 1);
2095 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002096 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002097 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002098 !FTy->getParamType(0)->isPointerTy() ||
2099 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002100 continue;
2101 setDoesNotThrow(F);
2102 setOnlyReadsMemory(F);
2103 setDoesNotCapture(F, 1);
2104 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002105 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002106 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002107 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002108 continue;
2109 setDoesNotThrow(F);
2110 setDoesNotCapture(F, 1);
2111 }
2112 break;
2113 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002114 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002115 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002116 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002117 continue;
2118 setDoesNotThrow(F);
2119 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002120 } else if (Name == "chmod" ||
2121 Name == "chown" ||
2122 Name == "ctermid" ||
2123 Name == "clearerr" ||
2124 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002125 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002126 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002127 continue;
2128 setDoesNotThrow(F);
2129 setDoesNotCapture(F, 1);
2130 }
2131 break;
2132 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002133 if (Name == "atoi" ||
2134 Name == "atol" ||
2135 Name == "atof" ||
2136 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002137 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002138 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002139 continue;
2140 setDoesNotThrow(F);
2141 setOnlyReadsMemory(F);
2142 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002143 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002144 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002145 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002146 continue;
2147 setDoesNotThrow(F);
2148 setDoesNotCapture(F, 1);
2149 }
2150 break;
2151 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002152 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002153 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002154 !FTy->getReturnType()->isPointerTy() ||
2155 !FTy->getParamType(0)->isPointerTy() ||
2156 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002157 continue;
2158 setDoesNotThrow(F);
2159 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002160 setDoesNotCapture(F, 1);
2161 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002162 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002163 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002164 !FTy->getReturnType()->isPointerTy() ||
2165 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002166 continue;
2167 setDoesNotThrow(F);
2168 setDoesNotAlias(F, 0);
2169 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002170 } else if (Name == "feof" ||
2171 Name == "free" ||
2172 Name == "fseek" ||
2173 Name == "ftell" ||
2174 Name == "fgetc" ||
2175 Name == "fseeko" ||
2176 Name == "ftello" ||
2177 Name == "fileno" ||
2178 Name == "fflush" ||
2179 Name == "fclose" ||
2180 Name == "fsetpos" ||
2181 Name == "flockfile" ||
2182 Name == "funlockfile" ||
2183 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002184 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002185 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002186 continue;
2187 setDoesNotThrow(F);
2188 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002189 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002190 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002191 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002192 continue;
2193 setDoesNotThrow(F);
2194 setDoesNotCapture(F, 1);
2195 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002196 } else if (Name == "fputc" ||
2197 Name == "fstat" ||
2198 Name == "frexp" ||
2199 Name == "frexpf" ||
2200 Name == "frexpl" ||
2201 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002202 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002203 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002204 continue;
2205 setDoesNotThrow(F);
2206 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002207 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002208 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002209 !FTy->getParamType(0)->isPointerTy() ||
2210 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002211 continue;
2212 setDoesNotThrow(F);
2213 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002214 } else if (Name == "fread" ||
2215 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002216 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002217 !FTy->getParamType(0)->isPointerTy() ||
2218 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002219 continue;
2220 setDoesNotThrow(F);
2221 setDoesNotCapture(F, 1);
2222 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002223 } else if (Name == "fputs" ||
2224 Name == "fscanf" ||
2225 Name == "fprintf" ||
2226 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002227 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002228 !FTy->getParamType(0)->isPointerTy() ||
2229 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002230 continue;
2231 setDoesNotThrow(F);
2232 setDoesNotCapture(F, 1);
2233 setDoesNotCapture(F, 2);
2234 }
2235 break;
2236 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002237 if (Name == "getc" ||
2238 Name == "getlogin_r" ||
2239 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002240 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002241 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002242 continue;
2243 setDoesNotThrow(F);
2244 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002245 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002246 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002247 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002248 continue;
2249 setDoesNotThrow(F);
2250 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002251 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002252 } else if (Name == "gets" ||
2253 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002254 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002255 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002256 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002257 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002258 continue;
2259 setDoesNotThrow(F);
2260 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002261 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002262 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002263 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002264 continue;
2265 setDoesNotThrow(F);
2266 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002267 }
2268 break;
2269 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002270 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002271 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002272 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002273 continue;
2274 setDoesNotThrow(F);
2275 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002276 } else if (Name == "uname" ||
2277 Name == "unlink" ||
2278 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002279 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002280 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002281 continue;
2282 setDoesNotThrow(F);
2283 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002284 } else if (Name == "utime" ||
2285 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002286 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002287 !FTy->getParamType(0)->isPointerTy() ||
2288 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002289 continue;
2290 setDoesNotThrow(F);
2291 setDoesNotCapture(F, 1);
2292 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002293 }
2294 break;
2295 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002296 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002297 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002298 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002299 continue;
2300 setDoesNotThrow(F);
2301 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002302 } else if (Name == "puts" ||
2303 Name == "printf" ||
2304 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002305 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002306 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002307 continue;
2308 setDoesNotThrow(F);
2309 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002310 } else if (Name == "pread" ||
2311 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002312 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002313 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002314 continue;
2315 // May throw; these are valid pthread cancellation points.
2316 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002317 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002318 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002319 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002320 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002321 !FTy->getReturnType()->isPointerTy() ||
2322 !FTy->getParamType(0)->isPointerTy() ||
2323 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002324 continue;
2325 setDoesNotThrow(F);
2326 setDoesNotAlias(F, 0);
2327 setDoesNotCapture(F, 1);
2328 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002329 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002330 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002331 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002332 continue;
2333 setDoesNotThrow(F);
2334 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002335 }
2336 break;
2337 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002338 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002339 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002340 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002341 continue;
2342 setDoesNotThrow(F);
2343 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002344 } else if (Name == "vsscanf" ||
2345 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002346 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002347 !FTy->getParamType(1)->isPointerTy() ||
2348 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002349 continue;
2350 setDoesNotThrow(F);
2351 setDoesNotCapture(F, 1);
2352 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002353 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00002354 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002355 continue;
2356 setDoesNotThrow(F);
2357 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002358 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002359 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002360 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002361 continue;
2362 setDoesNotThrow(F);
2363 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002364 } else if (Name == "vfprintf" ||
2365 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002366 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002367 !FTy->getParamType(0)->isPointerTy() ||
2368 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002369 continue;
2370 setDoesNotThrow(F);
2371 setDoesNotCapture(F, 1);
2372 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002373 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002374 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002375 !FTy->getParamType(0)->isPointerTy() ||
2376 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002377 continue;
2378 setDoesNotThrow(F);
2379 setDoesNotCapture(F, 1);
2380 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002381 }
2382 break;
2383 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002384 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002385 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002386 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002387 continue;
2388 // May throw; "open" is a valid pthread cancellation point.
2389 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002390 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002391 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002392 !FTy->getReturnType()->isPointerTy() ||
2393 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002394 continue;
2395 setDoesNotThrow(F);
2396 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002397 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002398 }
2399 break;
2400 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002401 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00002402 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002403 continue;
2404 setDoesNotThrow(F);
2405 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002406 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002407 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002408 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002409 continue;
2410 setDoesNotThrow(F);
2411 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002412 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002413 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002414 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002415 if (Name == "htonl" ||
2416 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002417 setDoesNotThrow(F);
2418 setDoesNotAccessMemory(F);
2419 }
2420 break;
2421 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002422 if (Name == "ntohl" ||
2423 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002424 setDoesNotThrow(F);
2425 setDoesNotAccessMemory(F);
2426 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002427 break;
2428 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002429 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002430 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002431 !FTy->getParamType(0)->isPointerTy() ||
2432 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002433 continue;
2434 setDoesNotThrow(F);
2435 setDoesNotCapture(F, 1);
2436 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002437 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002438 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002439 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002440 continue;
2441 setDoesNotThrow(F);
2442 setDoesNotCapture(F, 1);
2443 }
2444 break;
2445 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002446 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002447 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002448 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002449 continue;
2450 // May throw; places call through function pointer.
2451 setDoesNotCapture(F, 4);
2452 }
2453 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002454 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002455 if (Name == "__strdup" ||
2456 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002457 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002458 !FTy->getReturnType()->isPointerTy() ||
2459 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002460 continue;
2461 setDoesNotThrow(F);
2462 setDoesNotAlias(F, 0);
2463 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002464 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002465 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002466 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002467 continue;
2468 setDoesNotThrow(F);
2469 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002470 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002471 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002472 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002473 continue;
2474 setDoesNotThrow(F);
2475 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002476 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002477 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002478 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002479 continue;
2480 setDoesNotThrow(F);
2481 setDoesNotCapture(F, 2);
2482 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002483 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002484 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002485 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002486 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002487 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002488 continue;
2489 setDoesNotThrow(F);
2490 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002491 } else if (Name == "\1stat64" ||
2492 Name == "\1lstat64" ||
2493 Name == "\1statvfs64" ||
2494 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002495 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002496 !FTy->getParamType(0)->isPointerTy() ||
2497 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002498 continue;
2499 setDoesNotThrow(F);
2500 setDoesNotCapture(F, 1);
2501 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002502 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002503 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002504 !FTy->getReturnType()->isPointerTy() ||
2505 !FTy->getParamType(0)->isPointerTy() ||
2506 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002507 continue;
2508 setDoesNotThrow(F);
2509 setDoesNotAlias(F, 0);
2510 setDoesNotCapture(F, 1);
2511 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002512 } else if (Name == "\1fseeko64" ||
2513 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002514 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002515 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002516 continue;
2517 setDoesNotThrow(F);
2518 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002519 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002520 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002521 continue;
2522 setDoesNotThrow(F);
2523 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002524 } else if (Name == "\1fstat64" ||
2525 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002526 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002527 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002528 continue;
2529 setDoesNotThrow(F);
2530 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002531 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002532 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002533 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002534 continue;
2535 // May throw; "open" is a valid pthread cancellation point.
2536 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002537 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002538 break;
2539 }
2540 }
2541 return Modified;
2542}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002543
2544// TODO:
2545// Additional cases that we need to add to this file:
2546//
2547// cbrt:
2548// * cbrt(expN(X)) -> expN(x/3)
2549// * cbrt(sqrt(x)) -> pow(x,1/6)
2550// * cbrt(sqrt(x)) -> pow(x,1/9)
2551//
2552// cos, cosf, cosl:
2553// * cos(-x) -> cos(x)
2554//
2555// exp, expf, expl:
2556// * exp(log(x)) -> x
2557//
2558// log, logf, logl:
2559// * log(exp(x)) -> x
2560// * log(x**y) -> y*log(x)
2561// * log(exp(y)) -> y*log(e)
2562// * log(exp2(y)) -> y*log(2)
2563// * log(exp10(y)) -> y*log(10)
2564// * log(sqrt(x)) -> 0.5*log(x)
2565// * log(pow(x,y)) -> y*log(x)
2566//
2567// lround, lroundf, lroundl:
2568// * lround(cnst) -> cnst'
2569//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002570// pow, powf, powl:
2571// * pow(exp(x),y) -> exp(x*y)
2572// * pow(sqrt(x),y) -> pow(x,y*0.5)
2573// * pow(pow(x,y),z)-> pow(x,y*z)
2574//
2575// puts:
2576// * puts("") -> putchar("\n")
2577//
2578// round, roundf, roundl:
2579// * round(cnst) -> cnst'
2580//
2581// signbit:
2582// * signbit(cnst) -> cnst'
2583// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2584//
2585// sqrt, sqrtf, sqrtl:
2586// * sqrt(expN(x)) -> expN(x*0.5)
2587// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2588// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2589//
2590// stpcpy:
2591// * stpcpy(str, "literal") ->
2592// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2593// strrchr:
2594// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2595// (if c is a constant integer and s is a constant string)
2596// * strrchr(s1,0) -> strchr(s1,0)
2597//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002598// strpbrk:
2599// * strpbrk(s,a) -> offset_in_for(s,a)
2600// (if s and a are both constant strings)
2601// * strpbrk(s,"") -> 0
2602// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2603//
2604// strspn, strcspn:
2605// * strspn(s,a) -> const_int (if both args are constant)
2606// * strspn("",a) -> 0
2607// * strspn(s,"") -> 0
2608// * strcspn(s,a) -> const_int (if both args are constant)
2609// * strcspn("",a) -> 0
2610// * strcspn(s,"") -> strlen(a)
2611//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002612// tan, tanf, tanl:
2613// * tan(atan(x)) -> x
2614//
2615// trunc, truncf, truncl:
2616// * trunc(cnst) -> cnst'
2617//
2618//