blob: 30c3f3f398c2a756ffb5553762a43139700ce366 [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);
83
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000084 /// EmitMemCpy - Emit a call to the memcpy function to the builder. This
85 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
Eric Christopher37c8b862009-10-07 21:14:25 +000086 Value *EmitMemCpy(Value *Dst, Value *Src, Value *Len,
Eric Christopher7a61d702008-08-08 19:39:37 +000087 unsigned Align, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +000088
Eric Christopher80bf1d52009-11-21 01:01:30 +000089 /// EmitMemMove - Emit a call to the memmove function to the builder. This
90 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
91 Value *EmitMemMove(Value *Dst, Value *Src, Value *Len,
92 unsigned Align, IRBuilder<> &B);
93
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000094 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
95 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
Eric Christopher7a61d702008-08-08 19:39:37 +000096 Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B);
Nick Lewycky13a09e22008-12-21 00:19:21 +000097
98 /// EmitMemCmp - Emit a call to the memcmp function.
99 Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B);
100
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000101 /// EmitMemSet - Emit a call to the memset function
102 Value *EmitMemSet(Value *Dst, Value *Val, Value *Len, IRBuilder<> &B);
103
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000104 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
105 /// 'floor'). This function is known to take a single of type matching 'Op'
106 /// and returns one value with the same type. If 'Op' is a long double, 'l'
107 /// is added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
Dan Gohman79cb8402009-09-25 23:10:17 +0000108 Value *EmitUnaryFloatFnCall(Value *Op, const char *Name, IRBuilder<> &B,
109 const AttrListPtr &Attrs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000110
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000111 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
112 /// is an integer.
Chris Lattner74965f22009-11-09 04:57:04 +0000113 Value *EmitPutChar(Value *Char, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000114
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000115 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
116 /// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000117 void EmitPutS(Value *Str, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000118
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000119 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
120 /// an i32, and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000121 void EmitFPutC(Value *Char, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000122
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000123 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
124 /// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000125 void EmitFPutS(Value *Str, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000126
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000127 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
128 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000129 void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000130
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000131};
132} // End anonymous namespace.
133
134/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Eric Christopher7a61d702008-08-08 19:39:37 +0000135Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) {
Chris Lattnerbf796322009-12-02 06:05:42 +0000136 return B.CreateBitCast(V, Type::getInt8PtrTy(*Context), "cstr");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000137}
138
139/// EmitStrLen - Emit a call to the strlen function to the builder, for the
140/// specified pointer. This always returns an integer value of size intptr_t.
Eric Christopher7a61d702008-08-08 19:39:37 +0000141Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000142 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000143 AttributeWithIndex AWI[2];
144 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
145 AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
146 Attribute::NoUnwind);
147
148 Constant *StrLen =M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000149 TD->getIntPtrType(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000150 Type::getInt8PtrTy(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000151 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000152 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
153 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
154 CI->setCallingConv(F->getCallingConv());
155
156 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000157}
158
Chris Lattner24604112009-12-16 09:32:05 +0000159/// EmitStrChr - Emit a call to the strchr function to the builder, for the
160/// specified pointer and character. Ptr is required to be some pointer type,
161/// and the return value has 'i8*' type.
162Value *LibCallOptimization::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B) {
163 Module *M = Caller->getParent();
164 AttributeWithIndex AWI =
165 AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
166
167 const Type *I8Ptr = Type::getInt8PtrTy(*Context);
168 const Type *I32Ty = Type::getInt32Ty(*Context);
169 Constant *StrChr = M->getOrInsertFunction("strchr", AttrListPtr::get(&AWI, 1),
170 I8Ptr, I8Ptr, I32Ty, NULL);
171 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
172 ConstantInt::get(I32Ty, C), "strchr");
173 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
174 CI->setCallingConv(F->getCallingConv());
175 return CI;
176}
177
178
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000179/// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
180/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
181Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
Eric Christopher7a61d702008-08-08 19:39:37 +0000182 unsigned Align, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000183 Module *M = Caller->getParent();
Chris Lattnerbf796322009-12-02 06:05:42 +0000184 const Type *Ty = Len->getType();
185 Value *MemCpy = Intrinsic::getDeclaration(M, Intrinsic::memcpy, &Ty, 1);
186 Dst = CastToCStr(Dst, B);
187 Src = CastToCStr(Src, B);
188 return B.CreateCall4(MemCpy, Dst, Src, Len,
Owen Anderson1d0be152009-08-13 21:58:54 +0000189 ConstantInt::get(Type::getInt32Ty(*Context), Align));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000190}
191
Chris Lattnerbf796322009-12-02 06:05:42 +0000192/// EmitMemMove - Emit a call to the memmove function to the builder. This
Eric Christopher80bf1d52009-11-21 01:01:30 +0000193/// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
194Value *LibCallOptimization::EmitMemMove(Value *Dst, Value *Src, Value *Len,
195 unsigned Align, IRBuilder<> &B) {
196 Module *M = Caller->getParent();
Chris Lattnerbf796322009-12-02 06:05:42 +0000197 const Type *Ty = TD->getIntPtrType(*Context);
198 Value *MemMove = Intrinsic::getDeclaration(M, Intrinsic::memmove, &Ty, 1);
199 Dst = CastToCStr(Dst, B);
200 Src = CastToCStr(Src, B);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000201 Value *A = ConstantInt::get(Type::getInt32Ty(*Context), Align);
Chris Lattnerbf796322009-12-02 06:05:42 +0000202 return B.CreateCall4(MemMove, Dst, Src, Len, A);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000203}
204
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000205/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
206/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
207Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
Eric Christopher7a61d702008-08-08 19:39:37 +0000208 Value *Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000209 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000210 AttributeWithIndex AWI;
211 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
212
213 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
Eric Christopher37c8b862009-10-07 21:14:25 +0000214 Type::getInt8PtrTy(*Context),
215 Type::getInt8PtrTy(*Context),
216 Type::getInt32Ty(*Context),
217 TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000218 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000219 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
220
221 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
222 CI->setCallingConv(F->getCallingConv());
223
224 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000225}
226
Nick Lewycky13a09e22008-12-21 00:19:21 +0000227/// EmitMemCmp - Emit a call to the memcmp function.
228Value *LibCallOptimization::EmitMemCmp(Value *Ptr1, Value *Ptr2,
229 Value *Len, IRBuilder<> &B) {
230 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000231 AttributeWithIndex AWI[3];
232 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
233 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
234 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
235 Attribute::NoUnwind);
236
237 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000238 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000239 Type::getInt8PtrTy(*Context),
240 Type::getInt8PtrTy(*Context),
Owen Anderson1d0be152009-08-13 21:58:54 +0000241 TD->getIntPtrType(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000242 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
243 Len, "memcmp");
244
245 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
246 CI->setCallingConv(F->getCallingConv());
247
248 return CI;
Nick Lewycky13a09e22008-12-21 00:19:21 +0000249}
250
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000251/// EmitMemSet - Emit a call to the memset function
252Value *LibCallOptimization::EmitMemSet(Value *Dst, Value *Val,
253 Value *Len, IRBuilder<> &B) {
254 Module *M = Caller->getParent();
255 Intrinsic::ID IID = Intrinsic::memset;
256 const Type *Tys[1];
257 Tys[0] = Len->getType();
258 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000259 Value *Align = ConstantInt::get(Type::getInt32Ty(*Context), 1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000260 return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
261}
262
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000263/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
264/// 'floor'). This function is known to take a single of type matching 'Op' and
265/// returns one value with the same type. If 'Op' is a long double, 'l' is
266/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
267Value *LibCallOptimization::EmitUnaryFloatFnCall(Value *Op, const char *Name,
Dan Gohman79cb8402009-09-25 23:10:17 +0000268 IRBuilder<> &B,
269 const AttrListPtr &Attrs) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000270 char NameBuffer[20];
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000271 if (!Op->getType()->isDoubleTy()) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000272 // If we need to add a suffix, copy into NameBuffer.
273 unsigned NameLen = strlen(Name);
274 assert(NameLen < sizeof(NameBuffer)-2);
275 memcpy(NameBuffer, Name, NameLen);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000276 if (Op->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000277 NameBuffer[NameLen] = 'f'; // floorf
278 else
279 NameBuffer[NameLen] = 'l'; // floorl
280 NameBuffer[NameLen+1] = 0;
281 Name = NameBuffer;
282 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000283
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000284 Module *M = Caller->getParent();
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000285 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000286 Op->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000287 CallInst *CI = B.CreateCall(Callee, Op, Name);
Dan Gohman79cb8402009-09-25 23:10:17 +0000288 CI->setAttributes(Attrs);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000289 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
290 CI->setCallingConv(F->getCallingConv());
291
292 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000293}
294
295/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
296/// is an integer.
Chris Lattner74965f22009-11-09 04:57:04 +0000297Value *LibCallOptimization::EmitPutChar(Value *Char, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000298 Module *M = Caller->getParent();
Owen Anderson1d0be152009-08-13 21:58:54 +0000299 Value *PutChar = M->getOrInsertFunction("putchar", Type::getInt32Ty(*Context),
300 Type::getInt32Ty(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000301 CallInst *CI = B.CreateCall(PutChar,
Eric Christopher37c8b862009-10-07 21:14:25 +0000302 B.CreateIntCast(Char,
303 Type::getInt32Ty(*Context),
Duncan Sandsf63c4102009-11-16 12:32:28 +0000304 /*isSigned*/true,
Eric Christopher37c8b862009-10-07 21:14:25 +0000305 "chari"),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000306 "putchar");
307
308 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
309 CI->setCallingConv(F->getCallingConv());
Chris Lattner74965f22009-11-09 04:57:04 +0000310 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000311}
312
313/// EmitPutS - Emit a call to the puts function. This assumes that Str is
314/// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000315void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000316 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000317 AttributeWithIndex AWI[2];
318 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
319 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
320
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000321 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000322 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000323 Type::getInt8PtrTy(*Context),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000324 NULL);
325 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
326 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
327 CI->setCallingConv(F->getCallingConv());
328
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000329}
330
331/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
332/// an integer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000333void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000334 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000335 AttributeWithIndex AWI[2];
336 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
337 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
338 Constant *F;
339 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000340 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
341 Type::getInt32Ty(*Context),
342 Type::getInt32Ty(*Context), File->getType(),
343 NULL);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000344 else
Eric Christopher37c8b862009-10-07 21:14:25 +0000345 F = M->getOrInsertFunction("fputc",
346 Type::getInt32Ty(*Context),
347 Type::getInt32Ty(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000348 File->getType(), NULL);
Duncan Sandsf63c4102009-11-16 12:32:28 +0000349 Char = B.CreateIntCast(Char, Type::getInt32Ty(*Context), /*isSigned*/true,
350 "chari");
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000351 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
352
353 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
354 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000355}
356
357/// EmitFPutS - Emit a call to the puts function. Str is required to be a
358/// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000359void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000360 Module *M = Caller->getParent();
Nick Lewycky225f7472009-02-15 22:47:25 +0000361 AttributeWithIndex AWI[3];
362 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
363 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
364 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000365 Constant *F;
366 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000367 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
368 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000369 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000370 File->getType(), NULL);
371 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000372 F = M->getOrInsertFunction("fputs", Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000373 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000374 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000375 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
376
377 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
378 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000379}
380
381/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
382/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
383void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Eric Christopher7a61d702008-08-08 19:39:37 +0000384 IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000385 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000386 AttributeWithIndex AWI[3];
387 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
388 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
389 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
390 Constant *F;
391 if (isa<PointerType>(File->getType()))
392 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000393 TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000394 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000395 TD->getIntPtrType(*Context),
396 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000397 File->getType(), NULL);
398 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000399 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000400 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000401 TD->getIntPtrType(*Context),
402 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000403 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000404 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Owen Anderson1d0be152009-08-13 21:58:54 +0000405 ConstantInt::get(TD->getIntPtrType(*Context), 1), File);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000406
407 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
408 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000409}
410
411//===----------------------------------------------------------------------===//
412// Helper Functions
413//===----------------------------------------------------------------------===//
414
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000415/// GetStringLengthH - If we can compute the length of the string pointed to by
416/// the specified pointer, return 'len+1'. If we can't, return 0.
417static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
418 // Look through noop bitcast instructions.
419 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
420 return GetStringLengthH(BCI->getOperand(0), PHIs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000421
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000422 // If this is a PHI node, there are two cases: either we have already seen it
423 // or we haven't.
424 if (PHINode *PN = dyn_cast<PHINode>(V)) {
425 if (!PHIs.insert(PN))
426 return ~0ULL; // already in the set.
Eric Christopher37c8b862009-10-07 21:14:25 +0000427
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000428 // If it was new, see if all the input strings are the same length.
429 uint64_t LenSoFar = ~0ULL;
430 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
431 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
432 if (Len == 0) return 0; // Unknown length -> unknown.
Eric Christopher37c8b862009-10-07 21:14:25 +0000433
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000434 if (Len == ~0ULL) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +0000435
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000436 if (Len != LenSoFar && LenSoFar != ~0ULL)
437 return 0; // Disagree -> unknown.
438 LenSoFar = Len;
439 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000440
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000441 // Success, all agree.
442 return LenSoFar;
443 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000444
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000445 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
446 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
447 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
448 if (Len1 == 0) return 0;
449 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
450 if (Len2 == 0) return 0;
451 if (Len1 == ~0ULL) return Len2;
452 if (Len2 == ~0ULL) return Len1;
453 if (Len1 != Len2) return 0;
454 return Len1;
455 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000456
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000457 // If the value is not a GEP instruction nor a constant expression with a
458 // GEP instruction, then return unknown.
459 User *GEP = 0;
460 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
461 GEP = GEPI;
462 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
463 if (CE->getOpcode() != Instruction::GetElementPtr)
464 return 0;
465 GEP = CE;
466 } else {
467 return 0;
468 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000469
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000470 // Make sure the GEP has exactly three arguments.
471 if (GEP->getNumOperands() != 3)
472 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000473
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000474 // Check to make sure that the first operand of the GEP is an integer and
475 // has value 0 so that we are sure we're indexing into the initializer.
476 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
477 if (!Idx->isZero())
478 return 0;
479 } else
480 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000481
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000482 // If the second index isn't a ConstantInt, then this is a variable index
483 // into the array. If this occurs, we can't say anything meaningful about
484 // the string.
485 uint64_t StartIdx = 0;
486 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
487 StartIdx = CI->getZExtValue();
488 else
489 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000490
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000491 // The GEP instruction, constant or instruction, must reference a global
492 // variable that is a constant and is initialized. The referenced constant
493 // initializer is the array that we'll use for optimization.
494 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
Dan Gohman107f41f2009-08-19 00:11:12 +0000495 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
496 GV->mayBeOverridden())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000497 return 0;
498 Constant *GlobalInit = GV->getInitializer();
Eric Christopher37c8b862009-10-07 21:14:25 +0000499
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000500 // Handle the ConstantAggregateZero case, which is a degenerate case. The
501 // initializer is constant zero so the length of the string must be zero.
502 if (isa<ConstantAggregateZero>(GlobalInit))
503 return 1; // Len = 0 offset by 1.
Eric Christopher37c8b862009-10-07 21:14:25 +0000504
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000505 // Must be a Constant Array
506 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
Owen Anderson1d0be152009-08-13 21:58:54 +0000507 if (!Array ||
508 Array->getType()->getElementType() != Type::getInt8Ty(V->getContext()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000509 return false;
Eric Christopher37c8b862009-10-07 21:14:25 +0000510
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000511 // Get the number of elements in the array
512 uint64_t NumElts = Array->getType()->getNumElements();
Eric Christopher37c8b862009-10-07 21:14:25 +0000513
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000514 // Traverse the constant array from StartIdx (derived above) which is
515 // the place the GEP refers to in the array.
516 for (unsigned i = StartIdx; i != NumElts; ++i) {
517 Constant *Elt = Array->getOperand(i);
518 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
519 if (!CI) // This array isn't suitable, non-int initializer.
520 return 0;
521 if (CI->isZero())
522 return i-StartIdx+1; // We found end of string, success!
523 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000524
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000525 return 0; // The array isn't null terminated, conservatively return 'unknown'.
526}
527
528/// GetStringLength - If we can compute the length of the string pointed to by
529/// the specified pointer, return 'len+1'. If we can't, return 0.
530static uint64_t GetStringLength(Value *V) {
531 if (!isa<PointerType>(V->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000532
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000533 SmallPtrSet<PHINode*, 32> PHIs;
534 uint64_t Len = GetStringLengthH(V, PHIs);
535 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
536 // an empty string as a length.
537 return Len == ~0ULL ? 1 : Len;
538}
539
540/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +0000541/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000542static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
543 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
544 UI != E; ++UI) {
545 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
546 if (IC->isEquality())
547 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
548 if (C->isNullValue())
549 continue;
550 // Unknown instruction.
551 return false;
552 }
553 return true;
554}
555
556//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000557// String and Memory LibCall Optimizations
558//===----------------------------------------------------------------------===//
559
560//===---------------------------------------===//
561// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000562namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000563struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000564 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000565 // Verify the "strcat" function prototype.
566 const FunctionType *FT = Callee->getFunctionType();
567 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000568 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000569 FT->getParamType(0) != FT->getReturnType() ||
570 FT->getParamType(1) != FT->getReturnType())
571 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000572
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000573 // Extract some information from the instruction
574 Value *Dst = CI->getOperand(1);
575 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000576
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000577 // See if we can get the length of the input string.
578 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000579 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000580 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000581
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000582 // Handle the simple, do-nothing case: strcat(x, "") -> x
583 if (Len == 0)
584 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000585
586 // These optimizations require TargetData.
587 if (!TD) return 0;
588
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000589 EmitStrLenMemCpy(Src, Dst, Len, B);
590 return Dst;
591 }
592
593 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000594 // We need to find the end of the destination string. That's where the
595 // memory is to be moved to. We just generate a call to strlen.
596 Value *DstLen = EmitStrLen(Dst, B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000597
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000598 // Now that we have the destination's length, we must index into the
599 // destination's pointer to get the actual memcpy destination (end of
600 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000601 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000602
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000603 // We have enough information to now generate the memcpy call to do the
604 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000605 EmitMemCpy(CpyDst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000606 ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000607 }
608};
609
610//===---------------------------------------===//
611// 'strncat' Optimizations
612
Chris Lattner3e8b6632009-09-02 06:11:42 +0000613struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000614 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
615 // Verify the "strncat" function prototype.
616 const FunctionType *FT = Callee->getFunctionType();
617 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000618 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000619 FT->getParamType(0) != FT->getReturnType() ||
620 FT->getParamType(1) != FT->getReturnType() ||
621 !isa<IntegerType>(FT->getParamType(2)))
622 return 0;
623
624 // Extract some information from the instruction
625 Value *Dst = CI->getOperand(1);
626 Value *Src = CI->getOperand(2);
627 uint64_t Len;
628
629 // We don't do anything if length is not constant
630 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
631 Len = LengthArg->getZExtValue();
632 else
633 return 0;
634
635 // See if we can get the length of the input string.
636 uint64_t SrcLen = GetStringLength(Src);
637 if (SrcLen == 0) return 0;
638 --SrcLen; // Unbias length.
639
640 // Handle the simple, do-nothing cases:
641 // strncat(x, "", c) -> x
642 // strncat(x, c, 0) -> x
643 if (SrcLen == 0 || Len == 0) return Dst;
644
Dan Gohmanf14d9192009-08-18 00:48:13 +0000645 // These optimizations require TargetData.
646 if (!TD) return 0;
647
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000648 // We don't optimize this case
649 if (Len < SrcLen) return 0;
650
651 // strncat(x, s, c) -> strcat(x, s)
652 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000653 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000654 return Dst;
655 }
656};
657
658//===---------------------------------------===//
659// 'strchr' Optimizations
660
Chris Lattner3e8b6632009-09-02 06:11:42 +0000661struct StrChrOpt : 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 "strchr" function prototype.
664 const FunctionType *FT = Callee->getFunctionType();
665 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000666 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000667 FT->getParamType(0) != FT->getReturnType())
668 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000669
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000670 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000671
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000672 // If the second operand is non-constant, see if we can compute the length
673 // of the input string and turn this into memchr.
674 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
675 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000676 // These optimizations require TargetData.
677 if (!TD) return 0;
678
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000679 uint64_t Len = GetStringLength(SrcStr);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000680 if (Len == 0 ||
681 FT->getParamType(1) != Type::getInt32Ty(*Context)) // memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000682 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000683
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000684 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Owen Anderson1d0be152009-08-13 21:58:54 +0000685 ConstantInt::get(TD->getIntPtrType(*Context), Len), B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000686 }
687
688 // Otherwise, the character is a constant, see if the first argument is
689 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000690 std::string Str;
691 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000692 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000693
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000694 // strchr can find the nul character.
695 Str += '\0';
696 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000697
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000698 // Compute the offset.
699 uint64_t i = 0;
700 while (1) {
701 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000702 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000703 // Did we find our match?
704 if (Str[i] == CharValue)
705 break;
706 ++i;
707 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000708
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000709 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000710 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000711 return B.CreateGEP(SrcStr, Idx, "strchr");
712 }
713};
714
715//===---------------------------------------===//
716// 'strcmp' Optimizations
717
Chris Lattner3e8b6632009-09-02 06:11:42 +0000718struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000719 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000720 // Verify the "strcmp" function prototype.
721 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000722 if (FT->getNumParams() != 2 ||
723 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000724 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000725 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000726 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000727
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000728 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
729 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000730 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000731
Bill Wendling0582ae92009-03-13 04:39:26 +0000732 std::string Str1, Str2;
733 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
734 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000735
Bill Wendling0582ae92009-03-13 04:39:26 +0000736 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000737 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000738
Bill Wendling0582ae92009-03-13 04:39:26 +0000739 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000740 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000741
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000742 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000743 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000744 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000745 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000746
747 // strcmp(P, "x") -> memcmp(P, "x", 2)
748 uint64_t Len1 = GetStringLength(Str1P);
749 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000750 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000751 // These optimizations require TargetData.
752 if (!TD) return 0;
753
Nick Lewycky13a09e22008-12-21 00:19:21 +0000754 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000755 ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattner849832c2009-06-19 04:17:36 +0000756 std::min(Len1, Len2)), B);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000757 }
758
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000759 return 0;
760 }
761};
762
763//===---------------------------------------===//
764// 'strncmp' Optimizations
765
Chris Lattner3e8b6632009-09-02 06:11:42 +0000766struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000767 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000768 // Verify the "strncmp" function prototype.
769 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000770 if (FT->getNumParams() != 3 ||
771 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000772 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000773 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000774 !isa<IntegerType>(FT->getParamType(2)))
775 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000776
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000777 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
778 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000779 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000780
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000781 // Get the length argument if it is constant.
782 uint64_t Length;
783 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
784 Length = LengthArg->getZExtValue();
785 else
786 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000787
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000788 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000789 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000790
Bill Wendling0582ae92009-03-13 04:39:26 +0000791 std::string Str1, Str2;
792 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
793 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000794
Bill Wendling0582ae92009-03-13 04:39:26 +0000795 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000796 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000797
Bill Wendling0582ae92009-03-13 04:39:26 +0000798 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000799 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000800
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000801 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000802 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000803 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000804 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000805 return 0;
806 }
807};
808
809
810//===---------------------------------------===//
811// 'strcpy' Optimizations
812
Chris Lattner3e8b6632009-09-02 06:11:42 +0000813struct StrCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000814 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000815 // Verify the "strcpy" function prototype.
816 const FunctionType *FT = Callee->getFunctionType();
817 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
818 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000819 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000820 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000821
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000822 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
823 if (Dst == Src) // strcpy(x,x) -> x
824 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000825
Dan Gohmanf14d9192009-08-18 00:48:13 +0000826 // These optimizations require TargetData.
827 if (!TD) return 0;
828
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000829 // See if we can get the length of the input string.
830 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000831 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000832
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000833 // We have enough information to now generate the memcpy call to do the
834 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000835 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000836 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000837 return Dst;
838 }
839};
840
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000841//===---------------------------------------===//
842// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000843
Chris Lattner3e8b6632009-09-02 06:11:42 +0000844struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000845 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
846 const FunctionType *FT = Callee->getFunctionType();
847 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
848 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000849 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000850 !isa<IntegerType>(FT->getParamType(2)))
851 return 0;
852
853 Value *Dst = CI->getOperand(1);
854 Value *Src = CI->getOperand(2);
855 Value *LenOp = CI->getOperand(3);
856
857 // See if we can get the length of the input string.
858 uint64_t SrcLen = GetStringLength(Src);
859 if (SrcLen == 0) return 0;
860 --SrcLen;
861
862 if (SrcLen == 0) {
863 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000864 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
865 B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000866 return Dst;
867 }
868
869 uint64_t Len;
870 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
871 Len = LengthArg->getZExtValue();
872 else
873 return 0;
874
875 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
876
Dan Gohmanf14d9192009-08-18 00:48:13 +0000877 // These optimizations require TargetData.
878 if (!TD) return 0;
879
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000880 // Let strncpy handle the zero padding
881 if (Len > SrcLen+1) return 0;
882
883 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000884 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000885 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000886
887 return Dst;
888 }
889};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000890
891//===---------------------------------------===//
892// 'strlen' Optimizations
893
Chris Lattner3e8b6632009-09-02 06:11:42 +0000894struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000895 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000896 const FunctionType *FT = Callee->getFunctionType();
897 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000898 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000899 !isa<IntegerType>(FT->getReturnType()))
900 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000901
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000902 Value *Src = CI->getOperand(1);
903
904 // Constant folding: strlen("xyz") -> 3
905 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000906 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000907
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000908 // strlen(x) != 0 --> *x != 0
909 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000910 if (IsOnlyUsedInZeroEqualityComparison(CI))
911 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
912 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000913 }
914};
915
916//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000917// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000918
Chris Lattner3e8b6632009-09-02 06:11:42 +0000919struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000920 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
921 const FunctionType *FT = Callee->getFunctionType();
922 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
923 !isa<PointerType>(FT->getParamType(0)) ||
924 !isa<PointerType>(FT->getParamType(1)))
925 return 0;
926
927 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000928 if (isa<ConstantPointerNull>(EndPtr)) {
929 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000930 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000931 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000932
933 return 0;
934 }
935};
936
Chris Lattner24604112009-12-16 09:32:05 +0000937//===---------------------------------------===//
938// 'strstr' Optimizations
939
940struct StrStrOpt : public LibCallOptimization {
941 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
942 const FunctionType *FT = Callee->getFunctionType();
943 if (FT->getNumParams() != 2 ||
944 !isa<PointerType>(FT->getParamType(0)) ||
945 !isa<PointerType>(FT->getParamType(1)) ||
946 !isa<PointerType>(FT->getReturnType()))
947 return 0;
948
949 // fold strstr(x, x) -> x.
950 if (CI->getOperand(1) == CI->getOperand(2))
951 return B.CreateBitCast(CI->getOperand(1), CI->getType());
952
953 // See if either input string is a constant string.
954 std::string SearchStr, ToFindStr;
955 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
956 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
957
958 // fold strstr(x, "") -> x.
959 if (HasStr2 && ToFindStr.empty())
960 return B.CreateBitCast(CI->getOperand(1), CI->getType());
961
962 // If both strings are known, constant fold it.
963 if (HasStr1 && HasStr2) {
964 std::string::size_type Offset = SearchStr.find(ToFindStr);
965
966 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
967 return Constant::getNullValue(CI->getType());
968
969 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
970 Value *Result = CastToCStr(CI->getOperand(1), B);
971 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
972 return B.CreateBitCast(Result, CI->getType());
973 }
974
975 // fold strstr(x, "y") -> strchr(x, 'y').
976 if (HasStr2 && ToFindStr.size() == 1)
977 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B),
978 CI->getType());
979 return 0;
980 }
981};
982
Nick Lewycky4c498412009-02-13 15:31:46 +0000983
984//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000985// 'memcmp' Optimizations
986
Chris Lattner3e8b6632009-09-02 06:11:42 +0000987struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000988 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000989 const FunctionType *FT = Callee->getFunctionType();
990 if (FT->getNumParams() != 3 || !isa<PointerType>(FT->getParamType(0)) ||
991 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000992 FT->getReturnType() != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000993 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000994
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000995 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000996
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000997 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000998 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000999
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001000 // Make sure we have a constant length.
1001 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001002 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001003 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +00001004
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001005 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +00001006 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001007
1008 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
1009 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
1010 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +00001011 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001012 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +00001013
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001014 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS ^ *(short*)RHS) != 0
1015 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS ^ *(int*)RHS) != 0
1016 if ((Len == 2 || Len == 4) && IsOnlyUsedInZeroEqualityComparison(CI)) {
Owen Andersondebcb012009-07-29 22:17:13 +00001017 const Type *PTy = PointerType::getUnqual(Len == 2 ?
Owen Anderson1d0be152009-08-13 21:58:54 +00001018 Type::getInt16Ty(*Context) : Type::getInt32Ty(*Context));
Duncan Sandsec00fcb2008-05-19 09:27:24 +00001019 LHS = B.CreateBitCast(LHS, PTy, "tmp");
1020 RHS = B.CreateBitCast(RHS, PTy, "tmp");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001021 LoadInst *LHSV = B.CreateLoad(LHS, "lhsv");
1022 LoadInst *RHSV = B.CreateLoad(RHS, "rhsv");
1023 LHSV->setAlignment(1); RHSV->setAlignment(1); // Unaligned loads.
1024 return B.CreateZExt(B.CreateXor(LHSV, RHSV, "shortdiff"), CI->getType());
1025 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +00001026
Benjamin Kramer992a6372009-11-05 17:44:22 +00001027 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
1028 std::string LHSStr, RHSStr;
1029 if (GetConstantStringInfo(LHS, LHSStr) &&
1030 GetConstantStringInfo(RHS, RHSStr)) {
1031 // Make sure we're not reading out-of-bounds memory.
1032 if (Len > LHSStr.length() || Len > RHSStr.length())
1033 return 0;
1034 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
1035 return ConstantInt::get(CI->getType(), Ret);
1036 }
1037
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001038 return 0;
1039 }
1040};
1041
1042//===---------------------------------------===//
1043// 'memcpy' Optimizations
1044
Chris Lattner3e8b6632009-09-02 06:11:42 +00001045struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001046 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001047 // These optimizations require TargetData.
1048 if (!TD) return 0;
1049
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001050 const FunctionType *FT = Callee->getFunctionType();
1051 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1052 !isa<PointerType>(FT->getParamType(0)) ||
1053 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001054 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001055 return 0;
1056
1057 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
1058 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
1059 return CI->getOperand(1);
1060 }
1061};
1062
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001063//===---------------------------------------===//
1064// 'memmove' Optimizations
1065
Chris Lattner3e8b6632009-09-02 06:11:42 +00001066struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001067 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001068 // These optimizations require TargetData.
1069 if (!TD) return 0;
1070
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001071 const FunctionType *FT = Callee->getFunctionType();
1072 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1073 !isa<PointerType>(FT->getParamType(0)) ||
1074 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001075 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001076 return 0;
1077
1078 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopher80bf1d52009-11-21 01:01:30 +00001079 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001080 return CI->getOperand(1);
1081 }
1082};
1083
1084//===---------------------------------------===//
1085// 'memset' Optimizations
1086
Chris Lattner3e8b6632009-09-02 06:11:42 +00001087struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001088 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001089 // These optimizations require TargetData.
1090 if (!TD) return 0;
1091
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001092 const FunctionType *FT = Callee->getFunctionType();
1093 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1094 !isa<PointerType>(FT->getParamType(0)) ||
Eli Friedman62bb4132009-07-18 08:34:51 +00001095 !isa<IntegerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001096 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001097 return 0;
1098
1099 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +00001100 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1101 false);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001102 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001103 return CI->getOperand(1);
1104 }
1105};
1106
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001107//===----------------------------------------------------------------------===//
Eric Christopher80bf1d52009-11-21 01:01:30 +00001108// Object Size Checking Optimizations
1109//===----------------------------------------------------------------------===//
1110
1111//===---------------------------------------===//
1112// 'object size'
1113namespace {
1114struct SizeOpt : public LibCallOptimization {
1115 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1116 // TODO: We can do more with this, but delaying to here should be no change
1117 // in behavior.
1118 ConstantInt *Const = dyn_cast<ConstantInt>(CI->getOperand(2));
1119
1120 if (!Const) return 0;
1121
1122 const Type *Ty = Callee->getFunctionType()->getReturnType();
1123
Eric Christopherd060b252009-12-23 02:51:48 +00001124 if (Const->getZExtValue() == 0)
Eric Christopher80bf1d52009-11-21 01:01:30 +00001125 return Constant::getAllOnesValue(Ty);
1126 else
1127 return ConstantInt::get(Ty, 0);
1128 }
1129};
1130}
1131
1132//===---------------------------------------===//
1133// 'memcpy_chk' Optimizations
1134
1135struct MemCpyChkOpt : public LibCallOptimization {
1136 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1137 // These optimizations require TargetData.
1138 if (!TD) return 0;
1139
1140 const FunctionType *FT = Callee->getFunctionType();
1141 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1142 !isa<PointerType>(FT->getParamType(0)) ||
1143 !isa<PointerType>(FT->getParamType(1)) ||
Eric Christopherf734be22009-12-22 01:23:51 +00001144 !isa<IntegerType>(FT->getParamType(3)) ||
1145 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eric Christopher80bf1d52009-11-21 01:01:30 +00001146 return 0;
1147
1148 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1149 if (!SizeCI)
1150 return 0;
1151 if (SizeCI->isAllOnesValue()) {
1152 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
1153 return CI->getOperand(1);
1154 }
1155
1156 return 0;
1157 }
1158};
1159
1160//===---------------------------------------===//
1161// 'memset_chk' Optimizations
1162
1163struct MemSetChkOpt : public LibCallOptimization {
1164 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1165 // These optimizations require TargetData.
1166 if (!TD) return 0;
1167
1168 const FunctionType *FT = Callee->getFunctionType();
1169 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1170 !isa<PointerType>(FT->getParamType(0)) ||
1171 !isa<IntegerType>(FT->getParamType(1)) ||
Eric Christopherf734be22009-12-22 01:23:51 +00001172 !isa<IntegerType>(FT->getParamType(3)) ||
Eric Christopher80bf1d52009-11-21 01:01:30 +00001173 FT->getParamType(2) != TD->getIntPtrType(*Context))
1174 return 0;
1175
1176 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1177 if (!SizeCI)
1178 return 0;
1179 if (SizeCI->isAllOnesValue()) {
1180 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1181 false);
1182 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
1183 return CI->getOperand(1);
1184 }
1185
1186 return 0;
1187 }
1188};
1189
1190//===---------------------------------------===//
1191// 'memmove_chk' Optimizations
1192
1193struct MemMoveChkOpt : public LibCallOptimization {
1194 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1195 // These optimizations require TargetData.
1196 if (!TD) return 0;
1197
1198 const FunctionType *FT = Callee->getFunctionType();
1199 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1200 !isa<PointerType>(FT->getParamType(0)) ||
1201 !isa<PointerType>(FT->getParamType(1)) ||
Eric Christopherf734be22009-12-22 01:23:51 +00001202 !isa<IntegerType>(FT->getParamType(3)) ||
Eric Christopher80bf1d52009-11-21 01:01:30 +00001203 FT->getParamType(2) != TD->getIntPtrType(*Context))
1204 return 0;
1205
1206 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1207 if (!SizeCI)
1208 return 0;
1209 if (SizeCI->isAllOnesValue()) {
1210 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
1211 1, B);
1212 return CI->getOperand(1);
1213 }
1214
1215 return 0;
1216 }
1217};
1218
1219//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001220// Math Library Optimizations
1221//===----------------------------------------------------------------------===//
1222
1223//===---------------------------------------===//
1224// 'pow*' Optimizations
1225
Chris Lattner3e8b6632009-09-02 06:11:42 +00001226struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001227 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001228 const FunctionType *FT = Callee->getFunctionType();
1229 // Just make sure this has 2 arguments of the same FP type, which match the
1230 // result type.
1231 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1232 FT->getParamType(0) != FT->getParamType(1) ||
1233 !FT->getParamType(0)->isFloatingPoint())
1234 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001235
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001236 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
1237 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1238 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
1239 return Op1C;
1240 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +00001241 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001242 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001243
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001244 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1245 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001246
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001247 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001248 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001249
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001250 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +00001251 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1252 // This is faster than calling pow, and still handles negative zero
1253 // and negative infinite correctly.
1254 // TODO: In fast-math mode, this could be just sqrt(x).
1255 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +00001256 Value *Inf = ConstantFP::getInfinity(CI->getType());
1257 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +00001258 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1259 Callee->getAttributes());
1260 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1261 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +00001262 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
1263 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
1264 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001265 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001266
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001267 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1268 return Op1;
1269 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001270 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001271 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001272 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001273 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001274 return 0;
1275 }
1276};
1277
1278//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +00001279// 'exp2' Optimizations
1280
Chris Lattner3e8b6632009-09-02 06:11:42 +00001281struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001282 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +00001283 const FunctionType *FT = Callee->getFunctionType();
1284 // Just make sure this has 1 argument of FP type, which matches the
1285 // result type.
1286 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1287 !FT->getParamType(0)->isFloatingPoint())
1288 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001289
Chris Lattnere818f772008-05-02 18:43:35 +00001290 Value *Op = CI->getOperand(1);
1291 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1292 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1293 Value *LdExpArg = 0;
1294 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1295 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001296 LdExpArg = B.CreateSExt(OpC->getOperand(0),
1297 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001298 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1299 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001300 LdExpArg = B.CreateZExt(OpC->getOperand(0),
1301 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001302 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001303
Chris Lattnere818f772008-05-02 18:43:35 +00001304 if (LdExpArg) {
1305 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001306 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001307 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001308 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001309 Name = "ldexp";
1310 else
1311 Name = "ldexpl";
1312
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001313 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001314 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001315 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +00001316
1317 Module *M = Caller->getParent();
1318 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +00001319 Op->getType(),
1320 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001321 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1322 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1323 CI->setCallingConv(F->getCallingConv());
1324
1325 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +00001326 }
1327 return 0;
1328 }
1329};
Chris Lattnere818f772008-05-02 18:43:35 +00001330
1331//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001332// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1333
Chris Lattner3e8b6632009-09-02 06:11:42 +00001334struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001335 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001336 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001337 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1338 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001339 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001340
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001341 // If this is something like 'floor((double)floatval)', convert to floorf.
1342 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001343 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001344 return 0;
1345
1346 // floor((double)floatval) -> (double)floorf(floatval)
1347 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +00001348 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
1349 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +00001350 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001351 }
1352};
1353
1354//===----------------------------------------------------------------------===//
1355// Integer Optimizations
1356//===----------------------------------------------------------------------===//
1357
1358//===---------------------------------------===//
1359// 'ffs*' Optimizations
1360
Chris Lattner3e8b6632009-09-02 06:11:42 +00001361struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001362 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001363 const FunctionType *FT = Callee->getFunctionType();
1364 // Just make sure this has 2 arguments of the same FP type, which match the
1365 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +00001366 if (FT->getNumParams() != 1 ||
1367 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001368 !isa<IntegerType>(FT->getParamType(0)))
1369 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001370
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001371 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001372
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001373 // Constant fold.
1374 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1375 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +00001376 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +00001377 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001378 CI->getValue().countTrailingZeros()+1);
1379 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001380
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001381 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1382 const Type *ArgType = Op->getType();
1383 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1384 Intrinsic::cttz, &ArgType, 1);
1385 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +00001386 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +00001387 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001388
Owen Andersona7235ea2009-07-31 20:28:14 +00001389 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001390 return B.CreateSelect(Cond, V,
1391 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001392 }
1393};
1394
1395//===---------------------------------------===//
1396// 'isdigit' Optimizations
1397
Chris Lattner3e8b6632009-09-02 06:11:42 +00001398struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001399 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001400 const FunctionType *FT = Callee->getFunctionType();
1401 // We require integer(i32)
1402 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001403 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001404 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001405
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001406 // isdigit(c) -> (c-'0') <u 10
1407 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001408 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001409 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001410 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001411 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001412 return B.CreateZExt(Op, CI->getType());
1413 }
1414};
1415
1416//===---------------------------------------===//
1417// 'isascii' Optimizations
1418
Chris Lattner3e8b6632009-09-02 06:11:42 +00001419struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001420 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001421 const FunctionType *FT = Callee->getFunctionType();
1422 // We require integer(i32)
1423 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001424 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001425 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001426
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001427 // isascii(c) -> c <u 128
1428 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001429 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001430 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001431 return B.CreateZExt(Op, CI->getType());
1432 }
1433};
Eric Christopher37c8b862009-10-07 21:14:25 +00001434
Chris Lattner313f0e62008-06-09 08:26:51 +00001435//===---------------------------------------===//
1436// 'abs', 'labs', 'llabs' Optimizations
1437
Chris Lattner3e8b6632009-09-02 06:11:42 +00001438struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001439 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001440 const FunctionType *FT = Callee->getFunctionType();
1441 // We require integer(integer) where the types agree.
1442 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
1443 FT->getParamType(0) != FT->getReturnType())
1444 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001445
Chris Lattner313f0e62008-06-09 08:26:51 +00001446 // abs(x) -> x >s -1 ? x : -x
1447 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001448 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001449 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001450 "ispos");
1451 Value *Neg = B.CreateNeg(Op, "neg");
1452 return B.CreateSelect(Pos, Op, Neg);
1453 }
1454};
Eric Christopher37c8b862009-10-07 21:14:25 +00001455
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001456
1457//===---------------------------------------===//
1458// 'toascii' Optimizations
1459
Chris Lattner3e8b6632009-09-02 06:11:42 +00001460struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001461 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001462 const FunctionType *FT = Callee->getFunctionType();
1463 // We require i32(i32)
1464 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001465 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001466 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001467
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001468 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001469 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +00001470 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001471 }
1472};
1473
1474//===----------------------------------------------------------------------===//
1475// Formatting and IO Optimizations
1476//===----------------------------------------------------------------------===//
1477
1478//===---------------------------------------===//
1479// 'printf' Optimizations
1480
Chris Lattner3e8b6632009-09-02 06:11:42 +00001481struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001482 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001483 // Require one fixed pointer argument and an integer/void result.
1484 const FunctionType *FT = Callee->getFunctionType();
1485 if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
1486 !(isa<IntegerType>(FT->getReturnType()) ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001487 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001488 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001489
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001490 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001491 std::string FormatStr;
1492 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1493 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001494
1495 // Empty format string -> noop.
1496 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001497 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001498 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001499
Chris Lattner74965f22009-11-09 04:57:04 +00001500 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1501 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001502 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001503 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
1504 FormatStr[0]), B);
1505 if (CI->use_empty()) return CI;
1506 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001507 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001508
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001509 // printf("foo\n") --> puts("foo")
1510 if (FormatStr[FormatStr.size()-1] == '\n' &&
1511 FormatStr.find('%') == std::string::npos) { // no format characters.
1512 // Create a string literal with no \n on it. We expect the constant merge
1513 // pass to be run after this pass, to merge duplicate strings.
1514 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001515 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001516 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1517 GlobalVariable::InternalLinkage, C, "str");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001518 EmitPutS(C, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001519 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001520 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001521 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001522
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001523 // Optimize specific format strings.
1524 // printf("%c", chr) --> putchar(*(i8*)dst)
1525 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
1526 isa<IntegerType>(CI->getOperand(2)->getType())) {
Chris Lattner74965f22009-11-09 04:57:04 +00001527 Value *Res = EmitPutChar(CI->getOperand(2), B);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001528
Chris Lattner74965f22009-11-09 04:57:04 +00001529 if (CI->use_empty()) return CI;
1530 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001531 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001532
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001533 // printf("%s\n", str) --> puts(str)
1534 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
1535 isa<PointerType>(CI->getOperand(2)->getType()) &&
1536 CI->use_empty()) {
1537 EmitPutS(CI->getOperand(2), B);
1538 return CI;
1539 }
1540 return 0;
1541 }
1542};
1543
1544//===---------------------------------------===//
1545// 'sprintf' Optimizations
1546
Chris Lattner3e8b6632009-09-02 06:11:42 +00001547struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001548 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001549 // Require two fixed pointer arguments and an integer result.
1550 const FunctionType *FT = Callee->getFunctionType();
1551 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1552 !isa<PointerType>(FT->getParamType(1)) ||
1553 !isa<IntegerType>(FT->getReturnType()))
1554 return 0;
1555
1556 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001557 std::string FormatStr;
1558 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1559 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001560
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001561 // If we just have a format string (nothing else crazy) transform it.
1562 if (CI->getNumOperands() == 3) {
1563 // Make sure there's no % in the constant array. We could try to handle
1564 // %% -> % in the future if we cared.
1565 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1566 if (FormatStr[i] == '%')
1567 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001568
1569 // These optimizations require TargetData.
1570 if (!TD) return 0;
1571
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001572 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1573 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Owen Anderson1d0be152009-08-13 21:58:54 +00001574 ConstantInt::get(TD->getIntPtrType(*Context), FormatStr.size()+1),1,B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001575 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001576 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001577
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001578 // The remaining optimizations require the format string to be "%s" or "%c"
1579 // and have an extra operand.
1580 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1581 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001582
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001583 // Decode the second character of the format string.
1584 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001585 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001586 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001587 Value *V = B.CreateTrunc(CI->getOperand(3),
1588 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001589 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1590 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001591 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1592 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001593 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001594
Owen Andersoneed707b2009-07-24 23:12:02 +00001595 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001596 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001597
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001598 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001599 // These optimizations require TargetData.
1600 if (!TD) return 0;
1601
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001602 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1603 if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
1604
1605 Value *Len = EmitStrLen(CI->getOperand(3), B);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001606 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001607 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001608 "leninc");
1609 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001610
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001611 // The sprintf result is the unincremented number of bytes in the string.
1612 return B.CreateIntCast(Len, CI->getType(), false);
1613 }
1614 return 0;
1615 }
1616};
1617
1618//===---------------------------------------===//
1619// 'fwrite' Optimizations
1620
Chris Lattner3e8b6632009-09-02 06:11:42 +00001621struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001622 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001623 // Require a pointer, an integer, an integer, a pointer, returning integer.
1624 const FunctionType *FT = Callee->getFunctionType();
1625 if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
1626 !isa<IntegerType>(FT->getParamType(1)) ||
1627 !isa<IntegerType>(FT->getParamType(2)) ||
1628 !isa<PointerType>(FT->getParamType(3)) ||
1629 !isa<IntegerType>(FT->getReturnType()))
1630 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001631
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001632 // Get the element size and count.
1633 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1634 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1635 if (!SizeC || !CountC) return 0;
1636 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001637
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001638 // If this is writing zero records, remove the call (it's a noop).
1639 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001640 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001641
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001642 // If this is writing one byte, turn it into fputc.
1643 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1644 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1645 EmitFPutC(Char, CI->getOperand(4), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001646 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001647 }
1648
1649 return 0;
1650 }
1651};
1652
1653//===---------------------------------------===//
1654// 'fputs' Optimizations
1655
Chris Lattner3e8b6632009-09-02 06:11:42 +00001656struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001657 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001658 // These optimizations require TargetData.
1659 if (!TD) return 0;
1660
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001661 // Require two pointers. Also, we can't optimize if return value is used.
1662 const FunctionType *FT = Callee->getFunctionType();
1663 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1664 !isa<PointerType>(FT->getParamType(1)) ||
1665 !CI->use_empty())
1666 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001667
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001668 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1669 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001670 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001671 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001672 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001673 CI->getOperand(2), B);
1674 return CI; // Known to have no uses (see above).
1675 }
1676};
1677
1678//===---------------------------------------===//
1679// 'fprintf' Optimizations
1680
Chris Lattner3e8b6632009-09-02 06:11:42 +00001681struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001682 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001683 // Require two fixed paramters as pointers and integer result.
1684 const FunctionType *FT = Callee->getFunctionType();
1685 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1686 !isa<PointerType>(FT->getParamType(1)) ||
1687 !isa<IntegerType>(FT->getReturnType()))
1688 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001689
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001690 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001691 std::string FormatStr;
1692 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1693 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001694
1695 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1696 if (CI->getNumOperands() == 3) {
1697 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1698 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001699 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001700
1701 // These optimizations require TargetData.
1702 if (!TD) return 0;
1703
Owen Anderson1d0be152009-08-13 21:58:54 +00001704 EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001705 FormatStr.size()),
1706 CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001707 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001708 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001709
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001710 // The remaining optimizations require the format string to be "%s" or "%c"
1711 // and have an extra operand.
1712 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1713 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001714
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001715 // Decode the second character of the format string.
1716 if (FormatStr[1] == 'c') {
1717 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1718 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1719 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001720 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001721 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001722
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001723 if (FormatStr[1] == 's') {
1724 // fprintf(F, "%s", str) -> fputs(str, F)
1725 if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
1726 return 0;
1727 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1728 return CI;
1729 }
1730 return 0;
1731 }
1732};
1733
Bill Wendlingac178222008-05-05 21:37:59 +00001734} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001735
1736//===----------------------------------------------------------------------===//
1737// SimplifyLibCalls Pass Implementation
1738//===----------------------------------------------------------------------===//
1739
1740namespace {
1741 /// This pass optimizes well known library functions from libc and libm.
1742 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001743 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001744 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001745 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001746 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1747 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001748 StrToOpt StrTo; StrStrOpt StrStr;
1749 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001750 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001751 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001752 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001753 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1754 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001755 // Formatting and IO Optimizations
1756 SPrintFOpt SPrintF; PrintFOpt PrintF;
1757 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001758
1759 // Object Size Checking
Eric Christopher7b5e6172009-10-27 00:52:25 +00001760 SizeOpt ObjectSize;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001761 MemCpyChkOpt MemCpyChk; MemSetChkOpt MemSetChk; MemMoveChkOpt MemMoveChk;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001762
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001763 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001764 public:
1765 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001766 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001767
1768 void InitOptimizations();
1769 bool runOnFunction(Function &F);
1770
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001771 void setDoesNotAccessMemory(Function &F);
1772 void setOnlyReadsMemory(Function &F);
1773 void setDoesNotThrow(Function &F);
1774 void setDoesNotCapture(Function &F, unsigned n);
1775 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001776 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001777
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001778 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001779 }
1780 };
1781 char SimplifyLibCalls::ID = 0;
1782} // end anonymous namespace.
1783
1784static RegisterPass<SimplifyLibCalls>
1785X("simplify-libcalls", "Simplify well-known library calls");
1786
1787// Public interface to the Simplify LibCalls pass.
1788FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001789 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001790}
1791
1792/// Optimizations - Populate the Optimizations map with all the optimizations
1793/// we know.
1794void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001795 // String and Memory LibCall Optimizations
1796 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001797 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001798 Optimizations["strchr"] = &StrChr;
1799 Optimizations["strcmp"] = &StrCmp;
1800 Optimizations["strncmp"] = &StrNCmp;
1801 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001802 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001803 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001804 Optimizations["strtol"] = &StrTo;
1805 Optimizations["strtod"] = &StrTo;
1806 Optimizations["strtof"] = &StrTo;
1807 Optimizations["strtoul"] = &StrTo;
1808 Optimizations["strtoll"] = &StrTo;
1809 Optimizations["strtold"] = &StrTo;
1810 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001811 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001812 Optimizations["memcmp"] = &MemCmp;
1813 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001814 Optimizations["memmove"] = &MemMove;
1815 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001816
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001817 // Math Library Optimizations
1818 Optimizations["powf"] = &Pow;
1819 Optimizations["pow"] = &Pow;
1820 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001821 Optimizations["llvm.pow.f32"] = &Pow;
1822 Optimizations["llvm.pow.f64"] = &Pow;
1823 Optimizations["llvm.pow.f80"] = &Pow;
1824 Optimizations["llvm.pow.f128"] = &Pow;
1825 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001826 Optimizations["exp2l"] = &Exp2;
1827 Optimizations["exp2"] = &Exp2;
1828 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001829 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1830 Optimizations["llvm.exp2.f128"] = &Exp2;
1831 Optimizations["llvm.exp2.f80"] = &Exp2;
1832 Optimizations["llvm.exp2.f64"] = &Exp2;
1833 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001834
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001835#ifdef HAVE_FLOORF
1836 Optimizations["floor"] = &UnaryDoubleFP;
1837#endif
1838#ifdef HAVE_CEILF
1839 Optimizations["ceil"] = &UnaryDoubleFP;
1840#endif
1841#ifdef HAVE_ROUNDF
1842 Optimizations["round"] = &UnaryDoubleFP;
1843#endif
1844#ifdef HAVE_RINTF
1845 Optimizations["rint"] = &UnaryDoubleFP;
1846#endif
1847#ifdef HAVE_NEARBYINTF
1848 Optimizations["nearbyint"] = &UnaryDoubleFP;
1849#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001850
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001851 // Integer Optimizations
1852 Optimizations["ffs"] = &FFS;
1853 Optimizations["ffsl"] = &FFS;
1854 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001855 Optimizations["abs"] = &Abs;
1856 Optimizations["labs"] = &Abs;
1857 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001858 Optimizations["isdigit"] = &IsDigit;
1859 Optimizations["isascii"] = &IsAscii;
1860 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001861
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001862 // Formatting and IO Optimizations
1863 Optimizations["sprintf"] = &SPrintF;
1864 Optimizations["printf"] = &PrintF;
1865 Optimizations["fwrite"] = &FWrite;
1866 Optimizations["fputs"] = &FPuts;
1867 Optimizations["fprintf"] = &FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001868
1869 // Object Size Checking
1870 Optimizations["llvm.objectsize.i32"] = &ObjectSize;
1871 Optimizations["llvm.objectsize.i64"] = &ObjectSize;
1872 Optimizations["__memcpy_chk"] = &MemCpyChk;
1873 Optimizations["__memset_chk"] = &MemSetChk;
1874 Optimizations["__memmove_chk"] = &MemMoveChk;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001875}
1876
1877
1878/// runOnFunction - Top level algorithm.
1879///
1880bool SimplifyLibCalls::runOnFunction(Function &F) {
1881 if (Optimizations.empty())
1882 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001883
Dan Gohmanf14d9192009-08-18 00:48:13 +00001884 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001885
Owen Andersone922c022009-07-22 00:24:57 +00001886 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001887
1888 bool Changed = false;
1889 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1890 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1891 // Ignore non-calls.
1892 CallInst *CI = dyn_cast<CallInst>(I++);
1893 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001894
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001895 // Ignore indirect calls and calls to non-external functions.
1896 Function *Callee = CI->getCalledFunction();
1897 if (Callee == 0 || !Callee->isDeclaration() ||
1898 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1899 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001900
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001901 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001902 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1903 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001904
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001905 // Set the builder to the instruction after the call.
1906 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001907
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001908 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001909 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001910 if (Result == 0) continue;
1911
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001912 DEBUG(errs() << "SimplifyLibCalls simplified: " << *CI;
1913 errs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001914
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001915 // Something changed!
1916 Changed = true;
1917 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001918
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001919 // Inspect the instruction after the call (which was potentially just
1920 // added) next.
1921 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001922
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001923 if (CI != Result && !CI->use_empty()) {
1924 CI->replaceAllUsesWith(Result);
1925 if (!Result->hasName())
1926 Result->takeName(CI);
1927 }
1928 CI->eraseFromParent();
1929 }
1930 }
1931 return Changed;
1932}
1933
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001934// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001935
1936void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1937 if (!F.doesNotAccessMemory()) {
1938 F.setDoesNotAccessMemory();
1939 ++NumAnnotated;
1940 Modified = true;
1941 }
1942}
1943void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1944 if (!F.onlyReadsMemory()) {
1945 F.setOnlyReadsMemory();
1946 ++NumAnnotated;
1947 Modified = true;
1948 }
1949}
1950void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1951 if (!F.doesNotThrow()) {
1952 F.setDoesNotThrow();
1953 ++NumAnnotated;
1954 Modified = true;
1955 }
1956}
1957void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1958 if (!F.doesNotCapture(n)) {
1959 F.setDoesNotCapture(n);
1960 ++NumAnnotated;
1961 Modified = true;
1962 }
1963}
1964void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1965 if (!F.doesNotAlias(n)) {
1966 F.setDoesNotAlias(n);
1967 ++NumAnnotated;
1968 Modified = true;
1969 }
1970}
1971
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001972/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001973///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001974bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001975 Modified = false;
1976 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1977 Function &F = *I;
1978 if (!F.isDeclaration())
1979 continue;
1980
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001981 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001982 continue;
1983
1984 const FunctionType *FTy = F.getFunctionType();
1985
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001986 StringRef Name = F.getName();
1987 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001988 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001989 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001990 if (FTy->getNumParams() != 1 ||
1991 !isa<PointerType>(FTy->getParamType(0)))
1992 continue;
1993 setOnlyReadsMemory(F);
1994 setDoesNotThrow(F);
1995 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001996 } else if (Name == "strcpy" ||
1997 Name == "stpcpy" ||
1998 Name == "strcat" ||
1999 Name == "strtol" ||
2000 Name == "strtod" ||
2001 Name == "strtof" ||
2002 Name == "strtoul" ||
2003 Name == "strtoll" ||
2004 Name == "strtold" ||
2005 Name == "strncat" ||
2006 Name == "strncpy" ||
2007 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002008 if (FTy->getNumParams() < 2 ||
2009 !isa<PointerType>(FTy->getParamType(1)))
2010 continue;
2011 setDoesNotThrow(F);
2012 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002013 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002014 if (FTy->getNumParams() != 3 ||
2015 !isa<PointerType>(FTy->getParamType(0)) ||
2016 !isa<PointerType>(FTy->getParamType(1)))
2017 continue;
2018 setDoesNotThrow(F);
2019 setDoesNotCapture(F, 1);
2020 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002021 } else if (Name == "strcmp" ||
2022 Name == "strspn" ||
2023 Name == "strncmp" ||
2024 Name ==" strcspn" ||
2025 Name == "strcoll" ||
2026 Name == "strcasecmp" ||
2027 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002028 if (FTy->getNumParams() < 2 ||
2029 !isa<PointerType>(FTy->getParamType(0)) ||
2030 !isa<PointerType>(FTy->getParamType(1)))
2031 continue;
2032 setOnlyReadsMemory(F);
2033 setDoesNotThrow(F);
2034 setDoesNotCapture(F, 1);
2035 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002036 } else if (Name == "strstr" ||
2037 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002038 if (FTy->getNumParams() != 2 ||
2039 !isa<PointerType>(FTy->getParamType(1)))
2040 continue;
2041 setOnlyReadsMemory(F);
2042 setDoesNotThrow(F);
2043 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002044 } else if (Name == "strtok" ||
2045 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002046 if (FTy->getNumParams() < 2 ||
2047 !isa<PointerType>(FTy->getParamType(1)))
2048 continue;
2049 setDoesNotThrow(F);
2050 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002051 } else if (Name == "scanf" ||
2052 Name == "setbuf" ||
2053 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002054 if (FTy->getNumParams() < 1 ||
2055 !isa<PointerType>(FTy->getParamType(0)))
2056 continue;
2057 setDoesNotThrow(F);
2058 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002059 } else if (Name == "strdup" ||
2060 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00002061 if (FTy->getNumParams() < 1 ||
2062 !isa<PointerType>(FTy->getReturnType()) ||
2063 !isa<PointerType>(FTy->getParamType(0)))
2064 continue;
2065 setDoesNotThrow(F);
2066 setDoesNotAlias(F, 0);
2067 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002068 } else if (Name == "stat" ||
2069 Name == "sscanf" ||
2070 Name == "sprintf" ||
2071 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002072 if (FTy->getNumParams() < 2 ||
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002073 !isa<PointerType>(FTy->getParamType(0)) ||
2074 !isa<PointerType>(FTy->getParamType(1)))
2075 continue;
2076 setDoesNotThrow(F);
2077 setDoesNotCapture(F, 1);
2078 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002079 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002080 if (FTy->getNumParams() != 3 ||
2081 !isa<PointerType>(FTy->getParamType(0)) ||
2082 !isa<PointerType>(FTy->getParamType(2)))
2083 continue;
2084 setDoesNotThrow(F);
2085 setDoesNotCapture(F, 1);
2086 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002087 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002088 if (FTy->getNumParams() != 3 ||
2089 !isa<PointerType>(FTy->getParamType(1)) ||
2090 !isa<PointerType>(FTy->getParamType(2)))
2091 continue;
2092 setDoesNotThrow(F);
2093 setDoesNotCapture(F, 2);
2094 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002095 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002096 if (FTy->getNumParams() != 1 ||
2097 !isa<PointerType>(FTy->getParamType(0)))
2098 continue;
2099 // May throw; "system" is a valid pthread cancellation point.
2100 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002101 }
2102 break;
2103 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00002104 if (Name == "malloc") {
2105 if (FTy->getNumParams() != 1 ||
2106 !isa<PointerType>(FTy->getReturnType()))
2107 continue;
2108 setDoesNotThrow(F);
2109 setDoesNotAlias(F, 0);
2110 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002111 if (FTy->getNumParams() != 3 ||
2112 !isa<PointerType>(FTy->getParamType(0)) ||
2113 !isa<PointerType>(FTy->getParamType(1)))
2114 continue;
2115 setOnlyReadsMemory(F);
2116 setDoesNotThrow(F);
2117 setDoesNotCapture(F, 1);
2118 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002119 } else if (Name == "memchr" ||
2120 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002121 if (FTy->getNumParams() != 3)
2122 continue;
2123 setOnlyReadsMemory(F);
2124 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002125 } else if (Name == "modf" ||
2126 Name == "modff" ||
2127 Name == "modfl" ||
2128 Name == "memcpy" ||
2129 Name == "memccpy" ||
2130 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002131 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002132 !isa<PointerType>(FTy->getParamType(1)))
2133 continue;
2134 setDoesNotThrow(F);
2135 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002136 } else if (Name == "memalign") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002137 if (!isa<PointerType>(FTy->getReturnType()))
2138 continue;
2139 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002140 } else if (Name == "mkdir" ||
2141 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002142 if (FTy->getNumParams() == 0 ||
2143 !isa<PointerType>(FTy->getParamType(0)))
2144 continue;
2145 setDoesNotThrow(F);
2146 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002147 }
2148 break;
2149 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002150 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002151 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002152 !isa<PointerType>(FTy->getParamType(0)) ||
2153 !isa<PointerType>(FTy->getReturnType()))
2154 continue;
2155 setDoesNotThrow(F);
2156 setDoesNotAlias(F, 0);
2157 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002158 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002159 if (FTy->getNumParams() != 3 ||
2160 !isa<PointerType>(FTy->getParamType(1)))
2161 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002162 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002163 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002164 } else if (Name == "rmdir" ||
2165 Name == "rewind" ||
2166 Name == "remove" ||
2167 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002168 if (FTy->getNumParams() < 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002169 !isa<PointerType>(FTy->getParamType(0)))
2170 continue;
2171 setDoesNotThrow(F);
2172 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002173 } else if (Name == "rename" ||
2174 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002175 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002176 !isa<PointerType>(FTy->getParamType(0)) ||
2177 !isa<PointerType>(FTy->getParamType(1)))
2178 continue;
2179 setDoesNotThrow(F);
2180 setDoesNotCapture(F, 1);
2181 setDoesNotCapture(F, 2);
2182 }
2183 break;
2184 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002185 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002186 if (FTy->getNumParams() != 3 ||
2187 !isa<PointerType>(FTy->getParamType(1)))
2188 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002189 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002190 setDoesNotCapture(F, 2);
2191 }
2192 break;
2193 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002194 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002195 if (FTy->getNumParams() != 3 ||
2196 !isa<PointerType>(FTy->getParamType(0)) ||
2197 !isa<PointerType>(FTy->getParamType(1)))
2198 continue;
2199 setDoesNotThrow(F);
2200 setDoesNotCapture(F, 1);
2201 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002202 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002203 if (FTy->getNumParams() != 3 ||
2204 !isa<PointerType>(FTy->getParamType(0)) ||
2205 !isa<PointerType>(FTy->getParamType(1)))
2206 continue;
2207 setDoesNotThrow(F);
2208 setOnlyReadsMemory(F);
2209 setDoesNotCapture(F, 1);
2210 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002211 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002212 if (FTy->getNumParams() != 2 ||
2213 !isa<PointerType>(FTy->getParamType(0)))
2214 continue;
2215 setDoesNotThrow(F);
2216 setDoesNotCapture(F, 1);
2217 }
2218 break;
2219 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002220 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002221 if (FTy->getNumParams() != 2 ||
2222 !isa<PointerType>(FTy->getReturnType()))
2223 continue;
2224 setDoesNotThrow(F);
2225 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002226 } else if (Name == "chmod" ||
2227 Name == "chown" ||
2228 Name == "ctermid" ||
2229 Name == "clearerr" ||
2230 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002231 if (FTy->getNumParams() == 0 ||
2232 !isa<PointerType>(FTy->getParamType(0)))
2233 continue;
2234 setDoesNotThrow(F);
2235 setDoesNotCapture(F, 1);
2236 }
2237 break;
2238 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002239 if (Name == "atoi" ||
2240 Name == "atol" ||
2241 Name == "atof" ||
2242 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002243 if (FTy->getNumParams() != 1 ||
2244 !isa<PointerType>(FTy->getParamType(0)))
2245 continue;
2246 setDoesNotThrow(F);
2247 setOnlyReadsMemory(F);
2248 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002249 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002250 if (FTy->getNumParams() != 2 ||
2251 !isa<PointerType>(FTy->getParamType(0)))
2252 continue;
2253 setDoesNotThrow(F);
2254 setDoesNotCapture(F, 1);
2255 }
2256 break;
2257 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002258 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002259 if (FTy->getNumParams() != 2 ||
2260 !isa<PointerType>(FTy->getReturnType()) ||
2261 !isa<PointerType>(FTy->getParamType(0)) ||
2262 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002263 continue;
2264 setDoesNotThrow(F);
2265 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002266 setDoesNotCapture(F, 1);
2267 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002268 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002269 if (FTy->getNumParams() != 2 ||
2270 !isa<PointerType>(FTy->getReturnType()) ||
2271 !isa<PointerType>(FTy->getParamType(1)))
2272 continue;
2273 setDoesNotThrow(F);
2274 setDoesNotAlias(F, 0);
2275 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002276 } else if (Name == "feof" ||
2277 Name == "free" ||
2278 Name == "fseek" ||
2279 Name == "ftell" ||
2280 Name == "fgetc" ||
2281 Name == "fseeko" ||
2282 Name == "ftello" ||
2283 Name == "fileno" ||
2284 Name == "fflush" ||
2285 Name == "fclose" ||
2286 Name == "fsetpos" ||
2287 Name == "flockfile" ||
2288 Name == "funlockfile" ||
2289 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002290 if (FTy->getNumParams() == 0 ||
2291 !isa<PointerType>(FTy->getParamType(0)))
2292 continue;
2293 setDoesNotThrow(F);
2294 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002295 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002296 if (FTy->getNumParams() != 1 ||
2297 !isa<PointerType>(FTy->getParamType(0)))
2298 continue;
2299 setDoesNotThrow(F);
2300 setDoesNotCapture(F, 1);
2301 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002302 } else if (Name == "fputc" ||
2303 Name == "fstat" ||
2304 Name == "frexp" ||
2305 Name == "frexpf" ||
2306 Name == "frexpl" ||
2307 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002308 if (FTy->getNumParams() != 2 ||
2309 !isa<PointerType>(FTy->getParamType(1)))
2310 continue;
2311 setDoesNotThrow(F);
2312 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002313 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002314 if (FTy->getNumParams() != 3 ||
2315 !isa<PointerType>(FTy->getParamType(0)) ||
2316 !isa<PointerType>(FTy->getParamType(2)))
2317 continue;
2318 setDoesNotThrow(F);
2319 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002320 } else if (Name == "fread" ||
2321 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002322 if (FTy->getNumParams() != 4 ||
2323 !isa<PointerType>(FTy->getParamType(0)) ||
2324 !isa<PointerType>(FTy->getParamType(3)))
2325 continue;
2326 setDoesNotThrow(F);
2327 setDoesNotCapture(F, 1);
2328 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002329 } else if (Name == "fputs" ||
2330 Name == "fscanf" ||
2331 Name == "fprintf" ||
2332 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002333 if (FTy->getNumParams() < 2 ||
2334 !isa<PointerType>(FTy->getParamType(0)) ||
2335 !isa<PointerType>(FTy->getParamType(1)))
2336 continue;
2337 setDoesNotThrow(F);
2338 setDoesNotCapture(F, 1);
2339 setDoesNotCapture(F, 2);
2340 }
2341 break;
2342 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002343 if (Name == "getc" ||
2344 Name == "getlogin_r" ||
2345 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002346 if (FTy->getNumParams() == 0 ||
2347 !isa<PointerType>(FTy->getParamType(0)))
2348 continue;
2349 setDoesNotThrow(F);
2350 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002351 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002352 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002353 !isa<PointerType>(FTy->getParamType(0)))
2354 continue;
2355 setDoesNotThrow(F);
2356 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002357 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002358 } else if (Name == "gets" ||
2359 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002360 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002361 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002362 if (FTy->getNumParams() != 2 ||
2363 !isa<PointerType>(FTy->getParamType(1)))
2364 continue;
2365 setDoesNotThrow(F);
2366 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002367 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002368 if (FTy->getNumParams() != 1 ||
2369 !isa<PointerType>(FTy->getParamType(0)))
2370 continue;
2371 setDoesNotThrow(F);
2372 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002373 }
2374 break;
2375 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002376 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002377 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002378 !isa<PointerType>(FTy->getParamType(1)))
2379 continue;
2380 setDoesNotThrow(F);
2381 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002382 } else if (Name == "uname" ||
2383 Name == "unlink" ||
2384 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002385 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002386 !isa<PointerType>(FTy->getParamType(0)))
2387 continue;
2388 setDoesNotThrow(F);
2389 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002390 } else if (Name == "utime" ||
2391 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002392 if (FTy->getNumParams() != 2 ||
2393 !isa<PointerType>(FTy->getParamType(0)) ||
2394 !isa<PointerType>(FTy->getParamType(1)))
2395 continue;
2396 setDoesNotThrow(F);
2397 setDoesNotCapture(F, 1);
2398 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002399 }
2400 break;
2401 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002402 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002403 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002404 !isa<PointerType>(FTy->getParamType(1)))
2405 continue;
2406 setDoesNotThrow(F);
2407 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002408 } else if (Name == "puts" ||
2409 Name == "printf" ||
2410 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002411 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002412 !isa<PointerType>(FTy->getParamType(0)))
2413 continue;
2414 setDoesNotThrow(F);
2415 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002416 } else if (Name == "pread" ||
2417 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002418 if (FTy->getNumParams() != 4 ||
2419 !isa<PointerType>(FTy->getParamType(1)))
2420 continue;
2421 // May throw; these are valid pthread cancellation points.
2422 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002423 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002424 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002425 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002426 if (FTy->getNumParams() != 2 ||
2427 !isa<PointerType>(FTy->getReturnType()) ||
2428 !isa<PointerType>(FTy->getParamType(0)) ||
2429 !isa<PointerType>(FTy->getParamType(1)))
2430 continue;
2431 setDoesNotThrow(F);
2432 setDoesNotAlias(F, 0);
2433 setDoesNotCapture(F, 1);
2434 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002435 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002436 if (FTy->getNumParams() != 1 ||
2437 !isa<PointerType>(FTy->getParamType(0)))
2438 continue;
2439 setDoesNotThrow(F);
2440 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002441 }
2442 break;
2443 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002444 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002445 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002446 !isa<PointerType>(FTy->getParamType(1)))
2447 continue;
2448 setDoesNotThrow(F);
2449 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002450 } else if (Name == "vsscanf" ||
2451 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002452 if (FTy->getNumParams() != 3 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002453 !isa<PointerType>(FTy->getParamType(1)) ||
2454 !isa<PointerType>(FTy->getParamType(2)))
2455 continue;
2456 setDoesNotThrow(F);
2457 setDoesNotCapture(F, 1);
2458 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002459 } else if (Name == "valloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002460 if (!isa<PointerType>(FTy->getReturnType()))
2461 continue;
2462 setDoesNotThrow(F);
2463 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002464 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002465 if (FTy->getNumParams() != 2 ||
2466 !isa<PointerType>(FTy->getParamType(0)))
2467 continue;
2468 setDoesNotThrow(F);
2469 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002470 } else if (Name == "vfprintf" ||
2471 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002472 if (FTy->getNumParams() != 3 ||
2473 !isa<PointerType>(FTy->getParamType(0)) ||
2474 !isa<PointerType>(FTy->getParamType(1)))
2475 continue;
2476 setDoesNotThrow(F);
2477 setDoesNotCapture(F, 1);
2478 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002479 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002480 if (FTy->getNumParams() != 4 ||
2481 !isa<PointerType>(FTy->getParamType(0)) ||
2482 !isa<PointerType>(FTy->getParamType(2)))
2483 continue;
2484 setDoesNotThrow(F);
2485 setDoesNotCapture(F, 1);
2486 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002487 }
2488 break;
2489 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002490 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002491 if (FTy->getNumParams() < 2 ||
2492 !isa<PointerType>(FTy->getParamType(0)))
2493 continue;
2494 // May throw; "open" is a valid pthread cancellation point.
2495 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002496 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002497 if (FTy->getNumParams() != 1 ||
2498 !isa<PointerType>(FTy->getReturnType()) ||
2499 !isa<PointerType>(FTy->getParamType(0)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002500 continue;
2501 setDoesNotThrow(F);
2502 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002503 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002504 }
2505 break;
2506 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002507 if (Name == "tmpfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002508 if (!isa<PointerType>(FTy->getReturnType()))
2509 continue;
2510 setDoesNotThrow(F);
2511 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002512 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002513 if (FTy->getNumParams() != 1 ||
2514 !isa<PointerType>(FTy->getParamType(0)))
2515 continue;
2516 setDoesNotThrow(F);
2517 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002518 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002519 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002520 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002521 if (Name == "htonl" ||
2522 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002523 setDoesNotThrow(F);
2524 setDoesNotAccessMemory(F);
2525 }
2526 break;
2527 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002528 if (Name == "ntohl" ||
2529 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002530 setDoesNotThrow(F);
2531 setDoesNotAccessMemory(F);
2532 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002533 break;
2534 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002535 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002536 if (FTy->getNumParams() != 2 ||
2537 !isa<PointerType>(FTy->getParamType(0)) ||
2538 !isa<PointerType>(FTy->getParamType(1)))
2539 continue;
2540 setDoesNotThrow(F);
2541 setDoesNotCapture(F, 1);
2542 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002543 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002544 if (FTy->getNumParams() != 3 ||
2545 !isa<PointerType>(FTy->getParamType(0)))
2546 continue;
2547 setDoesNotThrow(F);
2548 setDoesNotCapture(F, 1);
2549 }
2550 break;
2551 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002552 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002553 if (FTy->getNumParams() != 4 ||
2554 !isa<PointerType>(FTy->getParamType(3)))
2555 continue;
2556 // May throw; places call through function pointer.
2557 setDoesNotCapture(F, 4);
2558 }
2559 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002560 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002561 if (Name == "__strdup" ||
2562 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002563 if (FTy->getNumParams() < 1 ||
2564 !isa<PointerType>(FTy->getReturnType()) ||
2565 !isa<PointerType>(FTy->getParamType(0)))
2566 continue;
2567 setDoesNotThrow(F);
2568 setDoesNotAlias(F, 0);
2569 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002570 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002571 if (FTy->getNumParams() != 3 ||
2572 !isa<PointerType>(FTy->getParamType(1)))
2573 continue;
2574 setDoesNotThrow(F);
2575 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002576 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002577 if (FTy->getNumParams() != 1 ||
2578 !isa<PointerType>(FTy->getParamType(0)))
2579 continue;
2580 setDoesNotThrow(F);
2581 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002582 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002583 if (FTy->getNumParams() != 2 ||
2584 !isa<PointerType>(FTy->getParamType(1)))
2585 continue;
2586 setDoesNotThrow(F);
2587 setDoesNotCapture(F, 2);
2588 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002589 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002590 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002591 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002592 if (FTy->getNumParams() < 1 ||
2593 !isa<PointerType>(FTy->getParamType(0)))
2594 continue;
2595 setDoesNotThrow(F);
2596 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002597 } else if (Name == "\1stat64" ||
2598 Name == "\1lstat64" ||
2599 Name == "\1statvfs64" ||
2600 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002601 if (FTy->getNumParams() < 1 ||
Nick Lewycky225f7472009-02-15 22:47:25 +00002602 !isa<PointerType>(FTy->getParamType(0)) ||
2603 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002604 continue;
2605 setDoesNotThrow(F);
2606 setDoesNotCapture(F, 1);
2607 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002608 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002609 if (FTy->getNumParams() != 2 ||
2610 !isa<PointerType>(FTy->getReturnType()) ||
2611 !isa<PointerType>(FTy->getParamType(0)) ||
2612 !isa<PointerType>(FTy->getParamType(1)))
2613 continue;
2614 setDoesNotThrow(F);
2615 setDoesNotAlias(F, 0);
2616 setDoesNotCapture(F, 1);
2617 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002618 } else if (Name == "\1fseeko64" ||
2619 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002620 if (FTy->getNumParams() == 0 ||
2621 !isa<PointerType>(FTy->getParamType(0)))
2622 continue;
2623 setDoesNotThrow(F);
2624 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002625 } else if (Name == "\1tmpfile64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002626 if (!isa<PointerType>(FTy->getReturnType()))
2627 continue;
2628 setDoesNotThrow(F);
2629 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002630 } else if (Name == "\1fstat64" ||
2631 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002632 if (FTy->getNumParams() != 2 ||
2633 !isa<PointerType>(FTy->getParamType(1)))
2634 continue;
2635 setDoesNotThrow(F);
2636 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002637 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002638 if (FTy->getNumParams() < 2 ||
2639 !isa<PointerType>(FTy->getParamType(0)))
2640 continue;
2641 // May throw; "open" is a valid pthread cancellation point.
2642 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002643 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002644 break;
2645 }
2646 }
2647 return Modified;
2648}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002649
2650// TODO:
2651// Additional cases that we need to add to this file:
2652//
2653// cbrt:
2654// * cbrt(expN(X)) -> expN(x/3)
2655// * cbrt(sqrt(x)) -> pow(x,1/6)
2656// * cbrt(sqrt(x)) -> pow(x,1/9)
2657//
2658// cos, cosf, cosl:
2659// * cos(-x) -> cos(x)
2660//
2661// exp, expf, expl:
2662// * exp(log(x)) -> x
2663//
2664// log, logf, logl:
2665// * log(exp(x)) -> x
2666// * log(x**y) -> y*log(x)
2667// * log(exp(y)) -> y*log(e)
2668// * log(exp2(y)) -> y*log(2)
2669// * log(exp10(y)) -> y*log(10)
2670// * log(sqrt(x)) -> 0.5*log(x)
2671// * log(pow(x,y)) -> y*log(x)
2672//
2673// lround, lroundf, lroundl:
2674// * lround(cnst) -> cnst'
2675//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002676// pow, powf, powl:
2677// * pow(exp(x),y) -> exp(x*y)
2678// * pow(sqrt(x),y) -> pow(x,y*0.5)
2679// * pow(pow(x,y),z)-> pow(x,y*z)
2680//
2681// puts:
2682// * puts("") -> putchar("\n")
2683//
2684// round, roundf, roundl:
2685// * round(cnst) -> cnst'
2686//
2687// signbit:
2688// * signbit(cnst) -> cnst'
2689// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2690//
2691// sqrt, sqrtf, sqrtl:
2692// * sqrt(expN(x)) -> expN(x*0.5)
2693// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2694// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2695//
2696// stpcpy:
2697// * stpcpy(str, "literal") ->
2698// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2699// strrchr:
2700// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2701// (if c is a constant integer and s is a constant string)
2702// * strrchr(s1,0) -> strchr(s1,0)
2703//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002704// strpbrk:
2705// * strpbrk(s,a) -> offset_in_for(s,a)
2706// (if s and a are both constant strings)
2707// * strpbrk(s,"") -> 0
2708// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2709//
2710// strspn, strcspn:
2711// * strspn(s,a) -> const_int (if both args are constant)
2712// * strspn("",a) -> 0
2713// * strspn(s,"") -> 0
2714// * strcspn(s,a) -> const_int (if both args are constant)
2715// * strcspn("",a) -> 0
2716// * strcspn(s,"") -> strlen(a)
2717//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002718// tan, tanf, tanl:
2719// * tan(atan(x)) -> x
2720//
2721// trunc, truncf, truncl:
2722// * trunc(cnst) -> cnst'
2723//
2724//