blob: c67c9cf03f27c3ee68848d80ebc4df0d3d6cd230 [file] [log] [blame]
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001//===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a simple pass that applies a variety of small
11// optimizations for calls to specific well-known function calls (e.g. runtime
Chris Lattnere9f9a7e2009-09-03 05:19:59 +000012// library functions). Any optimization that takes the very simple form
13// "replace call to library function with simpler code that provides the same
14// result" belongs in this file.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000015//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "simplify-libcalls"
19#include "llvm/Transforms/Scalar.h"
20#include "llvm/Intrinsics.h"
Owen Andersonfa5cbd62009-07-03 19:42:02 +000021#include "llvm/LLVMContext.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000022#include "llvm/Module.h"
23#include "llvm/Pass.h"
24#include "llvm/Support/IRBuilder.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000025#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/ADT/SmallPtrSet.h"
28#include "llvm/ADT/StringMap.h"
29#include "llvm/ADT/Statistic.h"
Daniel Dunbar473955f2009-07-29 22:00:43 +000030#include "llvm/ADT/STLExtras.h"
Chris Lattner56b4f2b2008-05-01 06:39:12 +000031#include "llvm/Support/Debug.h"
Daniel Dunbarf0443c12009-07-26 08:34:35 +000032#include "llvm/Support/raw_ostream.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000033#include "llvm/Config/config.h"
34using namespace llvm;
35
36STATISTIC(NumSimplified, "Number of library calls simplified");
Nick Lewycky0f8df9a2009-01-04 20:27:34 +000037STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000038
39//===----------------------------------------------------------------------===//
40// Optimizer Base Class
41//===----------------------------------------------------------------------===//
42
43/// This class is the abstract base class for the set of optimizations that
44/// corresponds to one library call.
45namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +000046class LibCallOptimization {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000047protected:
48 Function *Caller;
49 const TargetData *TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000050 LLVMContext* Context;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000051public:
52 LibCallOptimization() { }
53 virtual ~LibCallOptimization() {}
54
55 /// CallOptimizer - This pure virtual method is implemented by base classes to
56 /// do various optimizations. If this returns null then no transformation was
57 /// performed. If it returns CI, then it transformed the call and CI is to be
58 /// deleted. If it returns something else, replace CI with the new value and
59 /// delete CI.
Eric Christopher37c8b862009-10-07 21:14:25 +000060 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
Eric Christopher7a61d702008-08-08 19:39:37 +000061 =0;
Eric Christopher37c8b862009-10-07 21:14:25 +000062
Dan Gohmanf14d9192009-08-18 00:48:13 +000063 Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000064 Caller = CI->getParent()->getParent();
Dan Gohmanf14d9192009-08-18 00:48:13 +000065 this->TD = TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000066 if (CI->getCalledFunction())
Owen Andersone922c022009-07-22 00:24:57 +000067 Context = &CI->getCalledFunction()->getContext();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000068 return CallOptimizer(CI->getCalledFunction(), CI, B);
69 }
70
71 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Eric Christopher7a61d702008-08-08 19:39:37 +000072 Value *CastToCStr(Value *V, IRBuilder<> &B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000073
74 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
75 /// specified pointer. Ptr is required to be some pointer type, and the
76 /// return value has 'intptr_t' type.
Eric Christopher7a61d702008-08-08 19:39:37 +000077 Value *EmitStrLen(Value *Ptr, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +000078
Chris Lattner24604112009-12-16 09:32:05 +000079 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
80 /// specified pointer and character. Ptr is required to be some pointer type,
81 /// and the return value has 'i8*' type.
82 Value *EmitStrChr(Value *Ptr, char C, IRBuilder<> &B);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +000083
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
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000104 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name'
105 /// (e.g. 'floor'). This function is known to take a single of type matching
106 /// 'Op' and returns one value with the same type. If 'Op' is a long double,
107 /// 'l' is added as the suffix of name, if 'Op' is a float, we add a 'f'
108 /// suffix.
Dan Gohman79cb8402009-09-25 23:10:17 +0000109 Value *EmitUnaryFloatFnCall(Value *Op, const char *Name, IRBuilder<> &B,
110 const AttrListPtr &Attrs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000111
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000112 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
113 /// is an integer.
Chris Lattner74965f22009-11-09 04:57:04 +0000114 Value *EmitPutChar(Value *Char, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000115
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000116 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
117 /// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000118 void EmitPutS(Value *Str, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000119
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000120 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
121 /// an i32, and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000122 void EmitFPutC(Value *Char, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000123
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000124 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
125 /// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000126 void EmitFPutS(Value *Str, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000127
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000128 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
129 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000130 void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000131
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000132};
133} // End anonymous namespace.
134
135/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Eric Christopher7a61d702008-08-08 19:39:37 +0000136Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) {
Chris Lattnerbf796322009-12-02 06:05:42 +0000137 return B.CreateBitCast(V, Type::getInt8PtrTy(*Context), "cstr");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000138}
139
140/// EmitStrLen - Emit a call to the strlen function to the builder, for the
141/// specified pointer. This always returns an integer value of size intptr_t.
Eric Christopher7a61d702008-08-08 19:39:37 +0000142Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000143 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000144 AttributeWithIndex AWI[2];
145 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
146 AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
147 Attribute::NoUnwind);
148
149 Constant *StrLen =M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000150 TD->getIntPtrType(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000151 Type::getInt8PtrTy(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000152 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000153 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
154 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
155 CI->setCallingConv(F->getCallingConv());
156
157 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000158}
159
Chris Lattner24604112009-12-16 09:32:05 +0000160/// EmitStrChr - Emit a call to the strchr function to the builder, for the
161/// specified pointer and character. Ptr is required to be some pointer type,
162/// and the return value has 'i8*' type.
163Value *LibCallOptimization::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B) {
164 Module *M = Caller->getParent();
165 AttributeWithIndex AWI =
166 AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000167
Chris Lattner24604112009-12-16 09:32:05 +0000168 const Type *I8Ptr = Type::getInt8PtrTy(*Context);
169 const Type *I32Ty = Type::getInt32Ty(*Context);
170 Constant *StrChr = M->getOrInsertFunction("strchr", AttrListPtr::get(&AWI, 1),
171 I8Ptr, I8Ptr, I32Ty, NULL);
172 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
173 ConstantInt::get(I32Ty, C), "strchr");
174 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
175 CI->setCallingConv(F->getCallingConv());
176 return CI;
177}
178
179
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000180/// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
181/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
182Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
Eric Christopher7a61d702008-08-08 19:39:37 +0000183 unsigned Align, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000184 Module *M = Caller->getParent();
Chris Lattnerbf796322009-12-02 06:05:42 +0000185 const Type *Ty = Len->getType();
186 Value *MemCpy = Intrinsic::getDeclaration(M, Intrinsic::memcpy, &Ty, 1);
187 Dst = CastToCStr(Dst, B);
188 Src = CastToCStr(Src, B);
189 return B.CreateCall4(MemCpy, Dst, Src, Len,
Owen Anderson1d0be152009-08-13 21:58:54 +0000190 ConstantInt::get(Type::getInt32Ty(*Context), Align));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000191}
192
Chris Lattnerbf796322009-12-02 06:05:42 +0000193/// EmitMemMove - Emit a call to the memmove function to the builder. This
Eric Christopher80bf1d52009-11-21 01:01:30 +0000194/// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
195Value *LibCallOptimization::EmitMemMove(Value *Dst, Value *Src, Value *Len,
196 unsigned Align, IRBuilder<> &B) {
197 Module *M = Caller->getParent();
Chris Lattnerbf796322009-12-02 06:05:42 +0000198 const Type *Ty = TD->getIntPtrType(*Context);
199 Value *MemMove = Intrinsic::getDeclaration(M, Intrinsic::memmove, &Ty, 1);
200 Dst = CastToCStr(Dst, B);
201 Src = CastToCStr(Src, B);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000202 Value *A = ConstantInt::get(Type::getInt32Ty(*Context), Align);
Chris Lattnerbf796322009-12-02 06:05:42 +0000203 return B.CreateCall4(MemMove, Dst, Src, Len, A);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000204}
205
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000206/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
207/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
208Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
Eric Christopher7a61d702008-08-08 19:39:37 +0000209 Value *Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000210 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000211 AttributeWithIndex AWI;
212 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
213
214 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
Eric Christopher37c8b862009-10-07 21:14:25 +0000215 Type::getInt8PtrTy(*Context),
216 Type::getInt8PtrTy(*Context),
217 Type::getInt32Ty(*Context),
218 TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000219 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000220 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
221
222 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
223 CI->setCallingConv(F->getCallingConv());
224
225 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000226}
227
Nick Lewycky13a09e22008-12-21 00:19:21 +0000228/// EmitMemCmp - Emit a call to the memcmp function.
229Value *LibCallOptimization::EmitMemCmp(Value *Ptr1, Value *Ptr2,
230 Value *Len, IRBuilder<> &B) {
231 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000232 AttributeWithIndex AWI[3];
233 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
234 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
235 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
236 Attribute::NoUnwind);
237
238 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000239 Type::getInt32Ty(*Context),
Mikhail Glushenkov0ecbdeb2010-01-06 09:20:39 +0000240 Type::getInt8PtrTy(*Context),
241 Type::getInt8PtrTy(*Context),
Owen Anderson1d0be152009-08-13 21:58:54 +0000242 TD->getIntPtrType(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000243 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
244 Len, "memcmp");
245
246 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
247 CI->setCallingConv(F->getCallingConv());
248
249 return CI;
Nick Lewycky13a09e22008-12-21 00:19:21 +0000250}
251
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000252/// EmitMemSet - Emit a call to the memset function
253Value *LibCallOptimization::EmitMemSet(Value *Dst, Value *Val,
254 Value *Len, IRBuilder<> &B) {
255 Module *M = Caller->getParent();
256 Intrinsic::ID IID = Intrinsic::memset;
257 const Type *Tys[1];
258 Tys[0] = Len->getType();
259 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000260 Value *Align = ConstantInt::get(Type::getInt32Ty(*Context), 1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000261 return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
262}
263
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000264/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
265/// 'floor'). This function is known to take a single of type matching 'Op' and
266/// returns one value with the same type. If 'Op' is a long double, 'l' is
267/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
268Value *LibCallOptimization::EmitUnaryFloatFnCall(Value *Op, const char *Name,
Dan Gohman79cb8402009-09-25 23:10:17 +0000269 IRBuilder<> &B,
270 const AttrListPtr &Attrs) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000271 char NameBuffer[20];
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000272 if (!Op->getType()->isDoubleTy()) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000273 // If we need to add a suffix, copy into NameBuffer.
274 unsigned NameLen = strlen(Name);
275 assert(NameLen < sizeof(NameBuffer)-2);
276 memcpy(NameBuffer, Name, NameLen);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000277 if (Op->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000278 NameBuffer[NameLen] = 'f'; // floorf
279 else
280 NameBuffer[NameLen] = 'l'; // floorl
281 NameBuffer[NameLen+1] = 0;
282 Name = NameBuffer;
283 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000284
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000285 Module *M = Caller->getParent();
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000286 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000287 Op->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000288 CallInst *CI = B.CreateCall(Callee, Op, Name);
Dan Gohman79cb8402009-09-25 23:10:17 +0000289 CI->setAttributes(Attrs);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000290 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
291 CI->setCallingConv(F->getCallingConv());
292
293 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000294}
295
296/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
297/// is an integer.
Chris Lattner74965f22009-11-09 04:57:04 +0000298Value *LibCallOptimization::EmitPutChar(Value *Char, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000299 Module *M = Caller->getParent();
Owen Anderson1d0be152009-08-13 21:58:54 +0000300 Value *PutChar = M->getOrInsertFunction("putchar", Type::getInt32Ty(*Context),
301 Type::getInt32Ty(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000302 CallInst *CI = B.CreateCall(PutChar,
Eric Christopher37c8b862009-10-07 21:14:25 +0000303 B.CreateIntCast(Char,
304 Type::getInt32Ty(*Context),
Duncan Sandsf63c4102009-11-16 12:32:28 +0000305 /*isSigned*/true,
Eric Christopher37c8b862009-10-07 21:14:25 +0000306 "chari"),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000307 "putchar");
308
309 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
310 CI->setCallingConv(F->getCallingConv());
Chris Lattner74965f22009-11-09 04:57:04 +0000311 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000312}
313
314/// EmitPutS - Emit a call to the puts function. This assumes that Str is
315/// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000316void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000317 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000318 AttributeWithIndex AWI[2];
319 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
320 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
321
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000322 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000323 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000324 Type::getInt8PtrTy(*Context),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000325 NULL);
326 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
327 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
328 CI->setCallingConv(F->getCallingConv());
329
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000330}
331
332/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
333/// an integer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000334void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000335 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000336 AttributeWithIndex AWI[2];
337 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
338 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
339 Constant *F;
340 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000341 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
342 Type::getInt32Ty(*Context),
343 Type::getInt32Ty(*Context), File->getType(),
344 NULL);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000345 else
Eric Christopher37c8b862009-10-07 21:14:25 +0000346 F = M->getOrInsertFunction("fputc",
347 Type::getInt32Ty(*Context),
348 Type::getInt32Ty(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000349 File->getType(), NULL);
Duncan Sandsf63c4102009-11-16 12:32:28 +0000350 Char = B.CreateIntCast(Char, Type::getInt32Ty(*Context), /*isSigned*/true,
351 "chari");
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000352 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
353
354 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
355 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000356}
357
358/// EmitFPutS - Emit a call to the puts function. Str is required to be a
359/// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000360void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000361 Module *M = Caller->getParent();
Nick Lewycky225f7472009-02-15 22:47:25 +0000362 AttributeWithIndex AWI[3];
363 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
364 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
365 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000366 Constant *F;
367 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000368 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
369 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000370 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000371 File->getType(), NULL);
372 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000373 F = M->getOrInsertFunction("fputs", Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000374 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000375 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000376 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
377
378 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
379 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000380}
381
382/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
383/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
384void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Eric Christopher7a61d702008-08-08 19:39:37 +0000385 IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000386 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000387 AttributeWithIndex AWI[3];
388 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
389 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
390 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
391 Constant *F;
392 if (isa<PointerType>(File->getType()))
393 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000394 TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000395 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000396 TD->getIntPtrType(*Context),
397 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000398 File->getType(), NULL);
399 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000400 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000401 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000402 TD->getIntPtrType(*Context),
403 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000404 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000405 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Owen Anderson1d0be152009-08-13 21:58:54 +0000406 ConstantInt::get(TD->getIntPtrType(*Context), 1), File);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000407
408 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
409 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000410}
411
412//===----------------------------------------------------------------------===//
413// Helper Functions
414//===----------------------------------------------------------------------===//
415
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000416/// GetStringLengthH - If we can compute the length of the string pointed to by
417/// the specified pointer, return 'len+1'. If we can't, return 0.
418static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
419 // Look through noop bitcast instructions.
420 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
421 return GetStringLengthH(BCI->getOperand(0), PHIs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000422
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000423 // If this is a PHI node, there are two cases: either we have already seen it
424 // or we haven't.
425 if (PHINode *PN = dyn_cast<PHINode>(V)) {
426 if (!PHIs.insert(PN))
427 return ~0ULL; // already in the set.
Eric Christopher37c8b862009-10-07 21:14:25 +0000428
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000429 // If it was new, see if all the input strings are the same length.
430 uint64_t LenSoFar = ~0ULL;
431 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
432 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
433 if (Len == 0) return 0; // Unknown length -> unknown.
Eric Christopher37c8b862009-10-07 21:14:25 +0000434
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000435 if (Len == ~0ULL) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +0000436
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000437 if (Len != LenSoFar && LenSoFar != ~0ULL)
438 return 0; // Disagree -> unknown.
439 LenSoFar = Len;
440 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000441
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000442 // Success, all agree.
443 return LenSoFar;
444 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000445
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000446 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
447 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
448 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
449 if (Len1 == 0) return 0;
450 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
451 if (Len2 == 0) return 0;
452 if (Len1 == ~0ULL) return Len2;
453 if (Len2 == ~0ULL) return Len1;
454 if (Len1 != Len2) return 0;
455 return Len1;
456 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000457
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000458 // If the value is not a GEP instruction nor a constant expression with a
459 // GEP instruction, then return unknown.
460 User *GEP = 0;
461 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
462 GEP = GEPI;
463 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
464 if (CE->getOpcode() != Instruction::GetElementPtr)
465 return 0;
466 GEP = CE;
467 } else {
468 return 0;
469 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000470
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000471 // Make sure the GEP has exactly three arguments.
472 if (GEP->getNumOperands() != 3)
473 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000474
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000475 // Check to make sure that the first operand of the GEP is an integer and
476 // has value 0 so that we are sure we're indexing into the initializer.
477 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
478 if (!Idx->isZero())
479 return 0;
480 } else
481 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000482
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000483 // If the second index isn't a ConstantInt, then this is a variable index
484 // into the array. If this occurs, we can't say anything meaningful about
485 // the string.
486 uint64_t StartIdx = 0;
487 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
488 StartIdx = CI->getZExtValue();
489 else
490 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000491
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000492 // The GEP instruction, constant or instruction, must reference a global
493 // variable that is a constant and is initialized. The referenced constant
494 // initializer is the array that we'll use for optimization.
495 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
Dan Gohman107f41f2009-08-19 00:11:12 +0000496 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
497 GV->mayBeOverridden())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000498 return 0;
499 Constant *GlobalInit = GV->getInitializer();
Eric Christopher37c8b862009-10-07 21:14:25 +0000500
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000501 // Handle the ConstantAggregateZero case, which is a degenerate case. The
502 // initializer is constant zero so the length of the string must be zero.
503 if (isa<ConstantAggregateZero>(GlobalInit))
504 return 1; // Len = 0 offset by 1.
Eric Christopher37c8b862009-10-07 21:14:25 +0000505
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000506 // Must be a Constant Array
507 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +0000508 if (!Array || !Array->getType()->getElementType()->isInteger(8))
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);
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +0000680 if (Len == 0 || !FT->getParamType(1)->isInteger(32)) // memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000681 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000682
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000683 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Owen Anderson1d0be152009-08-13 21:58:54 +0000684 ConstantInt::get(TD->getIntPtrType(*Context), Len), B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000685 }
686
687 // Otherwise, the character is a constant, see if the first argument is
688 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000689 std::string Str;
690 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000691 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000692
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000693 // strchr can find the nul character.
694 Str += '\0';
695 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000696
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000697 // Compute the offset.
698 uint64_t i = 0;
699 while (1) {
700 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000701 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000702 // Did we find our match?
703 if (Str[i] == CharValue)
704 break;
705 ++i;
706 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000707
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000708 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000709 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000710 return B.CreateGEP(SrcStr, Idx, "strchr");
711 }
712};
713
714//===---------------------------------------===//
715// 'strcmp' Optimizations
716
Chris Lattner3e8b6632009-09-02 06:11:42 +0000717struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000718 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000719 // Verify the "strcmp" function prototype.
720 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000721 if (FT->getNumParams() != 2 ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +0000722 !FT->getReturnType()->isInteger(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000723 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000724 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000725 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000726
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000727 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
728 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000729 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000730
Bill Wendling0582ae92009-03-13 04:39:26 +0000731 std::string Str1, Str2;
732 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
733 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000734
Bill Wendling0582ae92009-03-13 04:39:26 +0000735 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000736 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000737
Bill Wendling0582ae92009-03-13 04:39:26 +0000738 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000739 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000740
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000741 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000742 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000743 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000744 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000745
746 // strcmp(P, "x") -> memcmp(P, "x", 2)
747 uint64_t Len1 = GetStringLength(Str1P);
748 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000749 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000750 // These optimizations require TargetData.
751 if (!TD) return 0;
752
Nick Lewycky13a09e22008-12-21 00:19:21 +0000753 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000754 ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattner849832c2009-06-19 04:17:36 +0000755 std::min(Len1, Len2)), B);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000756 }
757
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000758 return 0;
759 }
760};
761
762//===---------------------------------------===//
763// 'strncmp' Optimizations
764
Chris Lattner3e8b6632009-09-02 06:11:42 +0000765struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000766 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000767 // Verify the "strncmp" function prototype.
768 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000769 if (FT->getNumParams() != 3 ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +0000770 !FT->getReturnType()->isInteger(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000771 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000772 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000773 !isa<IntegerType>(FT->getParamType(2)))
774 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000775
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000776 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
777 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000778 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000779
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000780 // Get the length argument if it is constant.
781 uint64_t Length;
782 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
783 Length = LengthArg->getZExtValue();
784 else
785 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000786
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000787 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000788 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000789
Bill Wendling0582ae92009-03-13 04:39:26 +0000790 std::string Str1, Str2;
791 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
792 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000793
Bill Wendling0582ae92009-03-13 04:39:26 +0000794 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000795 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000796
Bill Wendling0582ae92009-03-13 04:39:26 +0000797 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000798 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000799
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000800 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000801 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000802 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000803 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000804 return 0;
805 }
806};
807
808
809//===---------------------------------------===//
810// 'strcpy' Optimizations
811
Chris Lattner3e8b6632009-09-02 06:11:42 +0000812struct StrCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000813 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000814 // Verify the "strcpy" function prototype.
815 const FunctionType *FT = Callee->getFunctionType();
816 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
817 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000818 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000819 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000820
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000821 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
822 if (Dst == Src) // strcpy(x,x) -> x
823 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000824
Dan Gohmanf14d9192009-08-18 00:48:13 +0000825 // These optimizations require TargetData.
826 if (!TD) return 0;
827
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000828 // See if we can get the length of the input string.
829 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000830 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000831
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000832 // We have enough information to now generate the memcpy call to do the
833 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000834 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000835 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000836 return Dst;
837 }
838};
839
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000840//===---------------------------------------===//
841// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000842
Chris Lattner3e8b6632009-09-02 06:11:42 +0000843struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000844 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
845 const FunctionType *FT = Callee->getFunctionType();
846 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
847 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000848 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000849 !isa<IntegerType>(FT->getParamType(2)))
850 return 0;
851
852 Value *Dst = CI->getOperand(1);
853 Value *Src = CI->getOperand(2);
854 Value *LenOp = CI->getOperand(3);
855
856 // See if we can get the length of the input string.
857 uint64_t SrcLen = GetStringLength(Src);
858 if (SrcLen == 0) return 0;
859 --SrcLen;
860
861 if (SrcLen == 0) {
862 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000863 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
864 B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000865 return Dst;
866 }
867
868 uint64_t Len;
869 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
870 Len = LengthArg->getZExtValue();
871 else
872 return 0;
873
874 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
875
Dan Gohmanf14d9192009-08-18 00:48:13 +0000876 // These optimizations require TargetData.
877 if (!TD) return 0;
878
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000879 // Let strncpy handle the zero padding
880 if (Len > SrcLen+1) return 0;
881
882 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000883 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000884 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000885
886 return Dst;
887 }
888};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000889
890//===---------------------------------------===//
891// 'strlen' Optimizations
892
Chris Lattner3e8b6632009-09-02 06:11:42 +0000893struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000894 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000895 const FunctionType *FT = Callee->getFunctionType();
896 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000897 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000898 !isa<IntegerType>(FT->getReturnType()))
899 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000900
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000901 Value *Src = CI->getOperand(1);
902
903 // Constant folding: strlen("xyz") -> 3
904 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000905 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000906
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000907 // strlen(x) != 0 --> *x != 0
908 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000909 if (IsOnlyUsedInZeroEqualityComparison(CI))
910 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
911 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000912 }
913};
914
915//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000916// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000917
Chris Lattner3e8b6632009-09-02 06:11:42 +0000918struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000919 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
920 const FunctionType *FT = Callee->getFunctionType();
921 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
922 !isa<PointerType>(FT->getParamType(0)) ||
923 !isa<PointerType>(FT->getParamType(1)))
924 return 0;
925
926 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000927 if (isa<ConstantPointerNull>(EndPtr)) {
928 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000929 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000930 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000931
932 return 0;
933 }
934};
935
Chris Lattner24604112009-12-16 09:32:05 +0000936//===---------------------------------------===//
937// 'strstr' Optimizations
938
939struct StrStrOpt : public LibCallOptimization {
940 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
941 const FunctionType *FT = Callee->getFunctionType();
942 if (FT->getNumParams() != 2 ||
943 !isa<PointerType>(FT->getParamType(0)) ||
944 !isa<PointerType>(FT->getParamType(1)) ||
945 !isa<PointerType>(FT->getReturnType()))
946 return 0;
947
948 // fold strstr(x, x) -> x.
949 if (CI->getOperand(1) == CI->getOperand(2))
950 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000951
Chris Lattner24604112009-12-16 09:32:05 +0000952 // See if either input string is a constant string.
953 std::string SearchStr, ToFindStr;
954 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
955 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000956
Chris Lattner24604112009-12-16 09:32:05 +0000957 // fold strstr(x, "") -> x.
958 if (HasStr2 && ToFindStr.empty())
959 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000960
Chris Lattner24604112009-12-16 09:32:05 +0000961 // If both strings are known, constant fold it.
962 if (HasStr1 && HasStr2) {
963 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000964
Chris Lattner24604112009-12-16 09:32:05 +0000965 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
966 return Constant::getNullValue(CI->getType());
967
968 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
969 Value *Result = CastToCStr(CI->getOperand(1), B);
970 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
971 return B.CreateBitCast(Result, CI->getType());
972 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000973
Chris Lattner24604112009-12-16 09:32:05 +0000974 // fold strstr(x, "y") -> strchr(x, 'y').
975 if (HasStr2 && ToFindStr.size() == 1)
976 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B),
977 CI->getType());
978 return 0;
979 }
980};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000981
Nick Lewycky4c498412009-02-13 15:31:46 +0000982
983//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000984// 'memcmp' Optimizations
985
Chris Lattner3e8b6632009-09-02 06:11:42 +0000986struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000987 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000988 const FunctionType *FT = Callee->getFunctionType();
989 if (FT->getNumParams() != 3 || !isa<PointerType>(FT->getParamType(0)) ||
990 !isa<PointerType>(FT->getParamType(1)) ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +0000991 !FT->getReturnType()->isInteger(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000992 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000993
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000994 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000995
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000996 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000997 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000998
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000999 // Make sure we have a constant length.
1000 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001001 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001002 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +00001003
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001004 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +00001005 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001006
1007 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
1008 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
1009 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +00001010 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001011 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +00001012
Benjamin Kramer992a6372009-11-05 17:44:22 +00001013 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
1014 std::string LHSStr, RHSStr;
1015 if (GetConstantStringInfo(LHS, LHSStr) &&
1016 GetConstantStringInfo(RHS, RHSStr)) {
1017 // Make sure we're not reading out-of-bounds memory.
1018 if (Len > LHSStr.length() || Len > RHSStr.length())
1019 return 0;
1020 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
1021 return ConstantInt::get(CI->getType(), Ret);
1022 }
1023
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001024 return 0;
1025 }
1026};
1027
1028//===---------------------------------------===//
1029// 'memcpy' Optimizations
1030
Chris Lattner3e8b6632009-09-02 06:11:42 +00001031struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001032 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001033 // These optimizations require TargetData.
1034 if (!TD) return 0;
1035
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001036 const FunctionType *FT = Callee->getFunctionType();
1037 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1038 !isa<PointerType>(FT->getParamType(0)) ||
1039 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001040 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001041 return 0;
1042
1043 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
1044 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
1045 return CI->getOperand(1);
1046 }
1047};
1048
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001049//===---------------------------------------===//
1050// 'memmove' Optimizations
1051
Chris Lattner3e8b6632009-09-02 06:11:42 +00001052struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001053 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001054 // These optimizations require TargetData.
1055 if (!TD) return 0;
1056
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001057 const FunctionType *FT = Callee->getFunctionType();
1058 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1059 !isa<PointerType>(FT->getParamType(0)) ||
1060 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001061 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001062 return 0;
1063
1064 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopher80bf1d52009-11-21 01:01:30 +00001065 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001066 return CI->getOperand(1);
1067 }
1068};
1069
1070//===---------------------------------------===//
1071// 'memset' Optimizations
1072
Chris Lattner3e8b6632009-09-02 06:11:42 +00001073struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001074 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001075 // These optimizations require TargetData.
1076 if (!TD) return 0;
1077
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001078 const FunctionType *FT = Callee->getFunctionType();
1079 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1080 !isa<PointerType>(FT->getParamType(0)) ||
Eli Friedman62bb4132009-07-18 08:34:51 +00001081 !isa<IntegerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001082 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001083 return 0;
1084
1085 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +00001086 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1087 false);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001088 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001089 return CI->getOperand(1);
1090 }
1091};
1092
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001093//===----------------------------------------------------------------------===//
Eric Christopher80bf1d52009-11-21 01:01:30 +00001094// Object Size Checking Optimizations
1095//===----------------------------------------------------------------------===//
1096
1097//===---------------------------------------===//
1098// 'object size'
1099namespace {
1100struct SizeOpt : public LibCallOptimization {
1101 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1102 // TODO: We can do more with this, but delaying to here should be no change
1103 // in behavior.
1104 ConstantInt *Const = dyn_cast<ConstantInt>(CI->getOperand(2));
1105
1106 if (!Const) return 0;
1107
1108 const Type *Ty = Callee->getFunctionType()->getReturnType();
1109
Eric Christopherd060b252009-12-23 02:51:48 +00001110 if (Const->getZExtValue() == 0)
Eric Christopher80bf1d52009-11-21 01:01:30 +00001111 return Constant::getAllOnesValue(Ty);
1112 else
1113 return ConstantInt::get(Ty, 0);
1114 }
1115};
1116}
1117
1118//===---------------------------------------===//
1119// 'memcpy_chk' Optimizations
1120
1121struct MemCpyChkOpt : public LibCallOptimization {
1122 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1123 // These optimizations require TargetData.
1124 if (!TD) return 0;
1125
1126 const FunctionType *FT = Callee->getFunctionType();
1127 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1128 !isa<PointerType>(FT->getParamType(0)) ||
1129 !isa<PointerType>(FT->getParamType(1)) ||
Eric Christopherf734be22009-12-22 01:23:51 +00001130 !isa<IntegerType>(FT->getParamType(3)) ||
1131 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eric Christopher80bf1d52009-11-21 01:01:30 +00001132 return 0;
1133
1134 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1135 if (!SizeCI)
1136 return 0;
1137 if (SizeCI->isAllOnesValue()) {
1138 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
1139 return CI->getOperand(1);
1140 }
1141
1142 return 0;
1143 }
1144};
1145
1146//===---------------------------------------===//
1147// 'memset_chk' Optimizations
1148
1149struct MemSetChkOpt : public LibCallOptimization {
1150 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1151 // These optimizations require TargetData.
1152 if (!TD) return 0;
1153
1154 const FunctionType *FT = Callee->getFunctionType();
1155 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1156 !isa<PointerType>(FT->getParamType(0)) ||
1157 !isa<IntegerType>(FT->getParamType(1)) ||
Eric Christopherf734be22009-12-22 01:23:51 +00001158 !isa<IntegerType>(FT->getParamType(3)) ||
Eric Christopher80bf1d52009-11-21 01:01:30 +00001159 FT->getParamType(2) != TD->getIntPtrType(*Context))
1160 return 0;
1161
1162 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1163 if (!SizeCI)
1164 return 0;
1165 if (SizeCI->isAllOnesValue()) {
1166 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1167 false);
1168 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
1169 return CI->getOperand(1);
1170 }
1171
1172 return 0;
1173 }
1174};
1175
1176//===---------------------------------------===//
1177// 'memmove_chk' Optimizations
1178
1179struct MemMoveChkOpt : public LibCallOptimization {
1180 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1181 // These optimizations require TargetData.
1182 if (!TD) return 0;
1183
1184 const FunctionType *FT = Callee->getFunctionType();
1185 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1186 !isa<PointerType>(FT->getParamType(0)) ||
1187 !isa<PointerType>(FT->getParamType(1)) ||
Eric Christopherf734be22009-12-22 01:23:51 +00001188 !isa<IntegerType>(FT->getParamType(3)) ||
Eric Christopher80bf1d52009-11-21 01:01:30 +00001189 FT->getParamType(2) != TD->getIntPtrType(*Context))
1190 return 0;
1191
1192 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1193 if (!SizeCI)
1194 return 0;
1195 if (SizeCI->isAllOnesValue()) {
1196 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
1197 1, B);
1198 return CI->getOperand(1);
1199 }
1200
1201 return 0;
1202 }
1203};
1204
1205//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001206// Math Library Optimizations
1207//===----------------------------------------------------------------------===//
1208
1209//===---------------------------------------===//
1210// 'pow*' Optimizations
1211
Chris Lattner3e8b6632009-09-02 06:11:42 +00001212struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001213 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001214 const FunctionType *FT = Callee->getFunctionType();
1215 // Just make sure this has 2 arguments of the same FP type, which match the
1216 // result type.
1217 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1218 FT->getParamType(0) != FT->getParamType(1) ||
1219 !FT->getParamType(0)->isFloatingPoint())
1220 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001221
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001222 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
1223 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1224 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
1225 return Op1C;
1226 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +00001227 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001228 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001229
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001230 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1231 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001232
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001233 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001234 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001235
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001236 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +00001237 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1238 // This is faster than calling pow, and still handles negative zero
1239 // and negative infinite correctly.
1240 // TODO: In fast-math mode, this could be just sqrt(x).
1241 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +00001242 Value *Inf = ConstantFP::getInfinity(CI->getType());
1243 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +00001244 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1245 Callee->getAttributes());
1246 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1247 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +00001248 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
1249 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
1250 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001251 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001252
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001253 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1254 return Op1;
1255 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001256 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001257 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001258 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001259 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001260 return 0;
1261 }
1262};
1263
1264//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +00001265// 'exp2' Optimizations
1266
Chris Lattner3e8b6632009-09-02 06:11:42 +00001267struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001268 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +00001269 const FunctionType *FT = Callee->getFunctionType();
1270 // Just make sure this has 1 argument of FP type, which matches the
1271 // result type.
1272 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1273 !FT->getParamType(0)->isFloatingPoint())
1274 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001275
Chris Lattnere818f772008-05-02 18:43:35 +00001276 Value *Op = CI->getOperand(1);
1277 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1278 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1279 Value *LdExpArg = 0;
1280 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1281 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001282 LdExpArg = B.CreateSExt(OpC->getOperand(0),
1283 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001284 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1285 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001286 LdExpArg = B.CreateZExt(OpC->getOperand(0),
1287 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001288 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001289
Chris Lattnere818f772008-05-02 18:43:35 +00001290 if (LdExpArg) {
1291 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001292 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001293 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001294 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001295 Name = "ldexp";
1296 else
1297 Name = "ldexpl";
1298
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001299 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001300 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001301 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +00001302
1303 Module *M = Caller->getParent();
1304 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +00001305 Op->getType(),
1306 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001307 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1308 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1309 CI->setCallingConv(F->getCallingConv());
1310
1311 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +00001312 }
1313 return 0;
1314 }
1315};
Chris Lattnere818f772008-05-02 18:43:35 +00001316
1317//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001318// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1319
Chris Lattner3e8b6632009-09-02 06:11:42 +00001320struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001321 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001322 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001323 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1324 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001325 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001326
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001327 // If this is something like 'floor((double)floatval)', convert to floorf.
1328 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001329 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001330 return 0;
1331
1332 // floor((double)floatval) -> (double)floorf(floatval)
1333 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +00001334 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
1335 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +00001336 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001337 }
1338};
1339
1340//===----------------------------------------------------------------------===//
1341// Integer Optimizations
1342//===----------------------------------------------------------------------===//
1343
1344//===---------------------------------------===//
1345// 'ffs*' Optimizations
1346
Chris Lattner3e8b6632009-09-02 06:11:42 +00001347struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001348 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001349 const FunctionType *FT = Callee->getFunctionType();
1350 // Just make sure this has 2 arguments of the same FP type, which match the
1351 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +00001352 if (FT->getNumParams() != 1 ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001353 !FT->getReturnType()->isInteger(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001354 !isa<IntegerType>(FT->getParamType(0)))
1355 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001356
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001357 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001358
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001359 // Constant fold.
1360 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1361 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +00001362 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +00001363 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001364 CI->getValue().countTrailingZeros()+1);
1365 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001366
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001367 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1368 const Type *ArgType = Op->getType();
1369 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1370 Intrinsic::cttz, &ArgType, 1);
1371 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +00001372 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +00001373 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001374
Owen Andersona7235ea2009-07-31 20:28:14 +00001375 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001376 return B.CreateSelect(Cond, V,
1377 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001378 }
1379};
1380
1381//===---------------------------------------===//
1382// 'isdigit' Optimizations
1383
Chris Lattner3e8b6632009-09-02 06:11:42 +00001384struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001385 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001386 const FunctionType *FT = Callee->getFunctionType();
1387 // We require integer(i32)
1388 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001389 !FT->getParamType(0)->isInteger(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001390 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001391
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001392 // isdigit(c) -> (c-'0') <u 10
1393 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001394 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001395 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001396 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001397 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001398 return B.CreateZExt(Op, CI->getType());
1399 }
1400};
1401
1402//===---------------------------------------===//
1403// 'isascii' Optimizations
1404
Chris Lattner3e8b6632009-09-02 06:11:42 +00001405struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001406 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001407 const FunctionType *FT = Callee->getFunctionType();
1408 // We require integer(i32)
1409 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001410 !FT->getParamType(0)->isInteger(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001411 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001412
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001413 // isascii(c) -> c <u 128
1414 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001415 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001416 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001417 return B.CreateZExt(Op, CI->getType());
1418 }
1419};
Eric Christopher37c8b862009-10-07 21:14:25 +00001420
Chris Lattner313f0e62008-06-09 08:26:51 +00001421//===---------------------------------------===//
1422// 'abs', 'labs', 'llabs' Optimizations
1423
Chris Lattner3e8b6632009-09-02 06:11:42 +00001424struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001425 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001426 const FunctionType *FT = Callee->getFunctionType();
1427 // We require integer(integer) where the types agree.
1428 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
1429 FT->getParamType(0) != FT->getReturnType())
1430 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001431
Chris Lattner313f0e62008-06-09 08:26:51 +00001432 // abs(x) -> x >s -1 ? x : -x
1433 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001434 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001435 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001436 "ispos");
1437 Value *Neg = B.CreateNeg(Op, "neg");
1438 return B.CreateSelect(Pos, Op, Neg);
1439 }
1440};
Eric Christopher37c8b862009-10-07 21:14:25 +00001441
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001442
1443//===---------------------------------------===//
1444// 'toascii' Optimizations
1445
Chris Lattner3e8b6632009-09-02 06:11:42 +00001446struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001447 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001448 const FunctionType *FT = Callee->getFunctionType();
1449 // We require i32(i32)
1450 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001451 !FT->getParamType(0)->isInteger(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001452 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001453
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001454 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001455 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +00001456 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001457 }
1458};
1459
1460//===----------------------------------------------------------------------===//
1461// Formatting and IO Optimizations
1462//===----------------------------------------------------------------------===//
1463
1464//===---------------------------------------===//
1465// 'printf' Optimizations
1466
Chris Lattner3e8b6632009-09-02 06:11:42 +00001467struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001468 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001469 // Require one fixed pointer argument and an integer/void result.
1470 const FunctionType *FT = Callee->getFunctionType();
1471 if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
1472 !(isa<IntegerType>(FT->getReturnType()) ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001473 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001474 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001475
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001476 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001477 std::string FormatStr;
1478 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1479 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001480
1481 // Empty format string -> noop.
1482 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001483 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001484 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001485
Chris Lattner74965f22009-11-09 04:57:04 +00001486 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1487 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001488 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001489 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
1490 FormatStr[0]), B);
1491 if (CI->use_empty()) return CI;
1492 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001493 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001494
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001495 // printf("foo\n") --> puts("foo")
1496 if (FormatStr[FormatStr.size()-1] == '\n' &&
1497 FormatStr.find('%') == std::string::npos) { // no format characters.
1498 // Create a string literal with no \n on it. We expect the constant merge
1499 // pass to be run after this pass, to merge duplicate strings.
1500 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001501 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001502 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1503 GlobalVariable::InternalLinkage, C, "str");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001504 EmitPutS(C, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001505 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001506 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001507 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001508
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001509 // Optimize specific format strings.
1510 // printf("%c", chr) --> putchar(*(i8*)dst)
1511 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
1512 isa<IntegerType>(CI->getOperand(2)->getType())) {
Chris Lattner74965f22009-11-09 04:57:04 +00001513 Value *Res = EmitPutChar(CI->getOperand(2), B);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001514
Chris Lattner74965f22009-11-09 04:57:04 +00001515 if (CI->use_empty()) return CI;
1516 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001517 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001518
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001519 // printf("%s\n", str) --> puts(str)
1520 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
1521 isa<PointerType>(CI->getOperand(2)->getType()) &&
1522 CI->use_empty()) {
1523 EmitPutS(CI->getOperand(2), B);
1524 return CI;
1525 }
1526 return 0;
1527 }
1528};
1529
1530//===---------------------------------------===//
1531// 'sprintf' Optimizations
1532
Chris Lattner3e8b6632009-09-02 06:11:42 +00001533struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001534 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001535 // Require two fixed pointer arguments and an integer result.
1536 const FunctionType *FT = Callee->getFunctionType();
1537 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1538 !isa<PointerType>(FT->getParamType(1)) ||
1539 !isa<IntegerType>(FT->getReturnType()))
1540 return 0;
1541
1542 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001543 std::string FormatStr;
1544 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1545 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001546
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001547 // If we just have a format string (nothing else crazy) transform it.
1548 if (CI->getNumOperands() == 3) {
1549 // Make sure there's no % in the constant array. We could try to handle
1550 // %% -> % in the future if we cared.
1551 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1552 if (FormatStr[i] == '%')
1553 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001554
1555 // These optimizations require TargetData.
1556 if (!TD) return 0;
1557
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001558 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1559 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001560 ConstantInt::get
1561 (TD->getIntPtrType(*Context), FormatStr.size()+1),1,B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001562 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001563 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001564
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001565 // The remaining optimizations require the format string to be "%s" or "%c"
1566 // and have an extra operand.
1567 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1568 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001569
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001570 // Decode the second character of the format string.
1571 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001572 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001573 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001574 Value *V = B.CreateTrunc(CI->getOperand(3),
1575 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001576 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1577 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001578 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1579 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001580 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001581
Owen Andersoneed707b2009-07-24 23:12:02 +00001582 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001583 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001584
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001585 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001586 // These optimizations require TargetData.
1587 if (!TD) return 0;
1588
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001589 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1590 if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
1591
1592 Value *Len = EmitStrLen(CI->getOperand(3), B);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001593 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001594 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001595 "leninc");
1596 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001597
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001598 // The sprintf result is the unincremented number of bytes in the string.
1599 return B.CreateIntCast(Len, CI->getType(), false);
1600 }
1601 return 0;
1602 }
1603};
1604
1605//===---------------------------------------===//
1606// 'fwrite' Optimizations
1607
Chris Lattner3e8b6632009-09-02 06:11:42 +00001608struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001609 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001610 // Require a pointer, an integer, an integer, a pointer, returning integer.
1611 const FunctionType *FT = Callee->getFunctionType();
1612 if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
1613 !isa<IntegerType>(FT->getParamType(1)) ||
1614 !isa<IntegerType>(FT->getParamType(2)) ||
1615 !isa<PointerType>(FT->getParamType(3)) ||
1616 !isa<IntegerType>(FT->getReturnType()))
1617 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001618
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001619 // Get the element size and count.
1620 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1621 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1622 if (!SizeC || !CountC) return 0;
1623 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001624
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001625 // If this is writing zero records, remove the call (it's a noop).
1626 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001627 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001628
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001629 // If this is writing one byte, turn it into fputc.
1630 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1631 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1632 EmitFPutC(Char, CI->getOperand(4), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001633 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001634 }
1635
1636 return 0;
1637 }
1638};
1639
1640//===---------------------------------------===//
1641// 'fputs' Optimizations
1642
Chris Lattner3e8b6632009-09-02 06:11:42 +00001643struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001644 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001645 // These optimizations require TargetData.
1646 if (!TD) return 0;
1647
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001648 // Require two pointers. Also, we can't optimize if return value is used.
1649 const FunctionType *FT = Callee->getFunctionType();
1650 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1651 !isa<PointerType>(FT->getParamType(1)) ||
1652 !CI->use_empty())
1653 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001654
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001655 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1656 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001657 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001658 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001659 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001660 CI->getOperand(2), B);
1661 return CI; // Known to have no uses (see above).
1662 }
1663};
1664
1665//===---------------------------------------===//
1666// 'fprintf' Optimizations
1667
Chris Lattner3e8b6632009-09-02 06:11:42 +00001668struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001669 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001670 // Require two fixed paramters as pointers and integer result.
1671 const FunctionType *FT = Callee->getFunctionType();
1672 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1673 !isa<PointerType>(FT->getParamType(1)) ||
1674 !isa<IntegerType>(FT->getReturnType()))
1675 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001676
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001677 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001678 std::string FormatStr;
1679 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1680 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001681
1682 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1683 if (CI->getNumOperands() == 3) {
1684 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1685 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001686 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001687
1688 // These optimizations require TargetData.
1689 if (!TD) return 0;
1690
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001691 EmitFWrite(CI->getOperand(2),
1692 ConstantInt::get(TD->getIntPtrType(*Context),
1693 FormatStr.size()),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001694 CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001695 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001696 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001697
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001698 // The remaining optimizations require the format string to be "%s" or "%c"
1699 // and have an extra operand.
1700 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1701 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001702
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001703 // Decode the second character of the format string.
1704 if (FormatStr[1] == 'c') {
1705 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1706 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1707 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001708 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001709 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001710
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001711 if (FormatStr[1] == 's') {
1712 // fprintf(F, "%s", str) -> fputs(str, F)
1713 if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
1714 return 0;
1715 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1716 return CI;
1717 }
1718 return 0;
1719 }
1720};
1721
Bill Wendlingac178222008-05-05 21:37:59 +00001722} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001723
1724//===----------------------------------------------------------------------===//
1725// SimplifyLibCalls Pass Implementation
1726//===----------------------------------------------------------------------===//
1727
1728namespace {
1729 /// This pass optimizes well known library functions from libc and libm.
1730 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001731 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001732 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001733 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001734 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1735 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001736 StrToOpt StrTo; StrStrOpt StrStr;
1737 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001738 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001739 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001740 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001741 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1742 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001743 // Formatting and IO Optimizations
1744 SPrintFOpt SPrintF; PrintFOpt PrintF;
1745 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001746
1747 // Object Size Checking
Eric Christopher7b5e6172009-10-27 00:52:25 +00001748 SizeOpt ObjectSize;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001749 MemCpyChkOpt MemCpyChk; MemSetChkOpt MemSetChk; MemMoveChkOpt MemMoveChk;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001750
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001751 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001752 public:
1753 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001754 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001755
1756 void InitOptimizations();
1757 bool runOnFunction(Function &F);
1758
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001759 void setDoesNotAccessMemory(Function &F);
1760 void setOnlyReadsMemory(Function &F);
1761 void setDoesNotThrow(Function &F);
1762 void setDoesNotCapture(Function &F, unsigned n);
1763 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001764 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001765
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001766 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001767 }
1768 };
1769 char SimplifyLibCalls::ID = 0;
1770} // end anonymous namespace.
1771
1772static RegisterPass<SimplifyLibCalls>
1773X("simplify-libcalls", "Simplify well-known library calls");
1774
1775// Public interface to the Simplify LibCalls pass.
1776FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001777 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001778}
1779
1780/// Optimizations - Populate the Optimizations map with all the optimizations
1781/// we know.
1782void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001783 // String and Memory LibCall Optimizations
1784 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001785 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001786 Optimizations["strchr"] = &StrChr;
1787 Optimizations["strcmp"] = &StrCmp;
1788 Optimizations["strncmp"] = &StrNCmp;
1789 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001790 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001791 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001792 Optimizations["strtol"] = &StrTo;
1793 Optimizations["strtod"] = &StrTo;
1794 Optimizations["strtof"] = &StrTo;
1795 Optimizations["strtoul"] = &StrTo;
1796 Optimizations["strtoll"] = &StrTo;
1797 Optimizations["strtold"] = &StrTo;
1798 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001799 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001800 Optimizations["memcmp"] = &MemCmp;
1801 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001802 Optimizations["memmove"] = &MemMove;
1803 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001804
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001805 // Math Library Optimizations
1806 Optimizations["powf"] = &Pow;
1807 Optimizations["pow"] = &Pow;
1808 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001809 Optimizations["llvm.pow.f32"] = &Pow;
1810 Optimizations["llvm.pow.f64"] = &Pow;
1811 Optimizations["llvm.pow.f80"] = &Pow;
1812 Optimizations["llvm.pow.f128"] = &Pow;
1813 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001814 Optimizations["exp2l"] = &Exp2;
1815 Optimizations["exp2"] = &Exp2;
1816 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001817 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1818 Optimizations["llvm.exp2.f128"] = &Exp2;
1819 Optimizations["llvm.exp2.f80"] = &Exp2;
1820 Optimizations["llvm.exp2.f64"] = &Exp2;
1821 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001822
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001823#ifdef HAVE_FLOORF
1824 Optimizations["floor"] = &UnaryDoubleFP;
1825#endif
1826#ifdef HAVE_CEILF
1827 Optimizations["ceil"] = &UnaryDoubleFP;
1828#endif
1829#ifdef HAVE_ROUNDF
1830 Optimizations["round"] = &UnaryDoubleFP;
1831#endif
1832#ifdef HAVE_RINTF
1833 Optimizations["rint"] = &UnaryDoubleFP;
1834#endif
1835#ifdef HAVE_NEARBYINTF
1836 Optimizations["nearbyint"] = &UnaryDoubleFP;
1837#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001838
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001839 // Integer Optimizations
1840 Optimizations["ffs"] = &FFS;
1841 Optimizations["ffsl"] = &FFS;
1842 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001843 Optimizations["abs"] = &Abs;
1844 Optimizations["labs"] = &Abs;
1845 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001846 Optimizations["isdigit"] = &IsDigit;
1847 Optimizations["isascii"] = &IsAscii;
1848 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001849
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001850 // Formatting and IO Optimizations
1851 Optimizations["sprintf"] = &SPrintF;
1852 Optimizations["printf"] = &PrintF;
1853 Optimizations["fwrite"] = &FWrite;
1854 Optimizations["fputs"] = &FPuts;
1855 Optimizations["fprintf"] = &FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001856
1857 // Object Size Checking
1858 Optimizations["llvm.objectsize.i32"] = &ObjectSize;
1859 Optimizations["llvm.objectsize.i64"] = &ObjectSize;
1860 Optimizations["__memcpy_chk"] = &MemCpyChk;
1861 Optimizations["__memset_chk"] = &MemSetChk;
1862 Optimizations["__memmove_chk"] = &MemMoveChk;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001863}
1864
1865
1866/// runOnFunction - Top level algorithm.
1867///
1868bool SimplifyLibCalls::runOnFunction(Function &F) {
1869 if (Optimizations.empty())
1870 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001871
Dan Gohmanf14d9192009-08-18 00:48:13 +00001872 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001873
Owen Andersone922c022009-07-22 00:24:57 +00001874 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001875
1876 bool Changed = false;
1877 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1878 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1879 // Ignore non-calls.
1880 CallInst *CI = dyn_cast<CallInst>(I++);
1881 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001882
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001883 // Ignore indirect calls and calls to non-external functions.
1884 Function *Callee = CI->getCalledFunction();
1885 if (Callee == 0 || !Callee->isDeclaration() ||
1886 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1887 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001888
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001889 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001890 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1891 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001892
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001893 // Set the builder to the instruction after the call.
1894 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001895
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001896 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001897 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001898 if (Result == 0) continue;
1899
David Greene6a6b90e2010-01-05 01:27:21 +00001900 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1901 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001902
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001903 // Something changed!
1904 Changed = true;
1905 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001906
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001907 // Inspect the instruction after the call (which was potentially just
1908 // added) next.
1909 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001910
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001911 if (CI != Result && !CI->use_empty()) {
1912 CI->replaceAllUsesWith(Result);
1913 if (!Result->hasName())
1914 Result->takeName(CI);
1915 }
1916 CI->eraseFromParent();
1917 }
1918 }
1919 return Changed;
1920}
1921
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001922// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001923
1924void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1925 if (!F.doesNotAccessMemory()) {
1926 F.setDoesNotAccessMemory();
1927 ++NumAnnotated;
1928 Modified = true;
1929 }
1930}
1931void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1932 if (!F.onlyReadsMemory()) {
1933 F.setOnlyReadsMemory();
1934 ++NumAnnotated;
1935 Modified = true;
1936 }
1937}
1938void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1939 if (!F.doesNotThrow()) {
1940 F.setDoesNotThrow();
1941 ++NumAnnotated;
1942 Modified = true;
1943 }
1944}
1945void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1946 if (!F.doesNotCapture(n)) {
1947 F.setDoesNotCapture(n);
1948 ++NumAnnotated;
1949 Modified = true;
1950 }
1951}
1952void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1953 if (!F.doesNotAlias(n)) {
1954 F.setDoesNotAlias(n);
1955 ++NumAnnotated;
1956 Modified = true;
1957 }
1958}
1959
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001960/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001961///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001962bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001963 Modified = false;
1964 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1965 Function &F = *I;
1966 if (!F.isDeclaration())
1967 continue;
1968
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001969 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001970 continue;
1971
1972 const FunctionType *FTy = F.getFunctionType();
1973
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001974 StringRef Name = F.getName();
1975 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001976 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001977 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001978 if (FTy->getNumParams() != 1 ||
1979 !isa<PointerType>(FTy->getParamType(0)))
1980 continue;
1981 setOnlyReadsMemory(F);
1982 setDoesNotThrow(F);
1983 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001984 } else if (Name == "strcpy" ||
1985 Name == "stpcpy" ||
1986 Name == "strcat" ||
1987 Name == "strtol" ||
1988 Name == "strtod" ||
1989 Name == "strtof" ||
1990 Name == "strtoul" ||
1991 Name == "strtoll" ||
1992 Name == "strtold" ||
1993 Name == "strncat" ||
1994 Name == "strncpy" ||
1995 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001996 if (FTy->getNumParams() < 2 ||
1997 !isa<PointerType>(FTy->getParamType(1)))
1998 continue;
1999 setDoesNotThrow(F);
2000 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002001 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002002 if (FTy->getNumParams() != 3 ||
2003 !isa<PointerType>(FTy->getParamType(0)) ||
2004 !isa<PointerType>(FTy->getParamType(1)))
2005 continue;
2006 setDoesNotThrow(F);
2007 setDoesNotCapture(F, 1);
2008 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002009 } else if (Name == "strcmp" ||
2010 Name == "strspn" ||
2011 Name == "strncmp" ||
2012 Name ==" strcspn" ||
2013 Name == "strcoll" ||
2014 Name == "strcasecmp" ||
2015 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002016 if (FTy->getNumParams() < 2 ||
2017 !isa<PointerType>(FTy->getParamType(0)) ||
2018 !isa<PointerType>(FTy->getParamType(1)))
2019 continue;
2020 setOnlyReadsMemory(F);
2021 setDoesNotThrow(F);
2022 setDoesNotCapture(F, 1);
2023 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002024 } else if (Name == "strstr" ||
2025 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002026 if (FTy->getNumParams() != 2 ||
2027 !isa<PointerType>(FTy->getParamType(1)))
2028 continue;
2029 setOnlyReadsMemory(F);
2030 setDoesNotThrow(F);
2031 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002032 } else if (Name == "strtok" ||
2033 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002034 if (FTy->getNumParams() < 2 ||
2035 !isa<PointerType>(FTy->getParamType(1)))
2036 continue;
2037 setDoesNotThrow(F);
2038 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002039 } else if (Name == "scanf" ||
2040 Name == "setbuf" ||
2041 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002042 if (FTy->getNumParams() < 1 ||
2043 !isa<PointerType>(FTy->getParamType(0)))
2044 continue;
2045 setDoesNotThrow(F);
2046 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002047 } else if (Name == "strdup" ||
2048 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00002049 if (FTy->getNumParams() < 1 ||
2050 !isa<PointerType>(FTy->getReturnType()) ||
2051 !isa<PointerType>(FTy->getParamType(0)))
2052 continue;
2053 setDoesNotThrow(F);
2054 setDoesNotAlias(F, 0);
2055 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002056 } else if (Name == "stat" ||
2057 Name == "sscanf" ||
2058 Name == "sprintf" ||
2059 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002060 if (FTy->getNumParams() < 2 ||
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002061 !isa<PointerType>(FTy->getParamType(0)) ||
2062 !isa<PointerType>(FTy->getParamType(1)))
2063 continue;
2064 setDoesNotThrow(F);
2065 setDoesNotCapture(F, 1);
2066 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002067 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002068 if (FTy->getNumParams() != 3 ||
2069 !isa<PointerType>(FTy->getParamType(0)) ||
2070 !isa<PointerType>(FTy->getParamType(2)))
2071 continue;
2072 setDoesNotThrow(F);
2073 setDoesNotCapture(F, 1);
2074 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002075 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002076 if (FTy->getNumParams() != 3 ||
2077 !isa<PointerType>(FTy->getParamType(1)) ||
2078 !isa<PointerType>(FTy->getParamType(2)))
2079 continue;
2080 setDoesNotThrow(F);
2081 setDoesNotCapture(F, 2);
2082 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002083 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002084 if (FTy->getNumParams() != 1 ||
2085 !isa<PointerType>(FTy->getParamType(0)))
2086 continue;
2087 // May throw; "system" is a valid pthread cancellation point.
2088 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002089 }
2090 break;
2091 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00002092 if (Name == "malloc") {
2093 if (FTy->getNumParams() != 1 ||
2094 !isa<PointerType>(FTy->getReturnType()))
2095 continue;
2096 setDoesNotThrow(F);
2097 setDoesNotAlias(F, 0);
2098 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002099 if (FTy->getNumParams() != 3 ||
2100 !isa<PointerType>(FTy->getParamType(0)) ||
2101 !isa<PointerType>(FTy->getParamType(1)))
2102 continue;
2103 setOnlyReadsMemory(F);
2104 setDoesNotThrow(F);
2105 setDoesNotCapture(F, 1);
2106 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002107 } else if (Name == "memchr" ||
2108 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002109 if (FTy->getNumParams() != 3)
2110 continue;
2111 setOnlyReadsMemory(F);
2112 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002113 } else if (Name == "modf" ||
2114 Name == "modff" ||
2115 Name == "modfl" ||
2116 Name == "memcpy" ||
2117 Name == "memccpy" ||
2118 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002119 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002120 !isa<PointerType>(FTy->getParamType(1)))
2121 continue;
2122 setDoesNotThrow(F);
2123 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002124 } else if (Name == "memalign") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002125 if (!isa<PointerType>(FTy->getReturnType()))
2126 continue;
2127 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002128 } else if (Name == "mkdir" ||
2129 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002130 if (FTy->getNumParams() == 0 ||
2131 !isa<PointerType>(FTy->getParamType(0)))
2132 continue;
2133 setDoesNotThrow(F);
2134 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002135 }
2136 break;
2137 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002138 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002139 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002140 !isa<PointerType>(FTy->getParamType(0)) ||
2141 !isa<PointerType>(FTy->getReturnType()))
2142 continue;
2143 setDoesNotThrow(F);
2144 setDoesNotAlias(F, 0);
2145 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002146 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002147 if (FTy->getNumParams() != 3 ||
2148 !isa<PointerType>(FTy->getParamType(1)))
2149 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002150 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002151 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002152 } else if (Name == "rmdir" ||
2153 Name == "rewind" ||
2154 Name == "remove" ||
2155 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002156 if (FTy->getNumParams() < 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002157 !isa<PointerType>(FTy->getParamType(0)))
2158 continue;
2159 setDoesNotThrow(F);
2160 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002161 } else if (Name == "rename" ||
2162 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002163 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002164 !isa<PointerType>(FTy->getParamType(0)) ||
2165 !isa<PointerType>(FTy->getParamType(1)))
2166 continue;
2167 setDoesNotThrow(F);
2168 setDoesNotCapture(F, 1);
2169 setDoesNotCapture(F, 2);
2170 }
2171 break;
2172 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002173 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002174 if (FTy->getNumParams() != 3 ||
2175 !isa<PointerType>(FTy->getParamType(1)))
2176 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002177 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002178 setDoesNotCapture(F, 2);
2179 }
2180 break;
2181 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002182 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002183 if (FTy->getNumParams() != 3 ||
2184 !isa<PointerType>(FTy->getParamType(0)) ||
2185 !isa<PointerType>(FTy->getParamType(1)))
2186 continue;
2187 setDoesNotThrow(F);
2188 setDoesNotCapture(F, 1);
2189 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002190 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002191 if (FTy->getNumParams() != 3 ||
2192 !isa<PointerType>(FTy->getParamType(0)) ||
2193 !isa<PointerType>(FTy->getParamType(1)))
2194 continue;
2195 setDoesNotThrow(F);
2196 setOnlyReadsMemory(F);
2197 setDoesNotCapture(F, 1);
2198 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002199 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002200 if (FTy->getNumParams() != 2 ||
2201 !isa<PointerType>(FTy->getParamType(0)))
2202 continue;
2203 setDoesNotThrow(F);
2204 setDoesNotCapture(F, 1);
2205 }
2206 break;
2207 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002208 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002209 if (FTy->getNumParams() != 2 ||
2210 !isa<PointerType>(FTy->getReturnType()))
2211 continue;
2212 setDoesNotThrow(F);
2213 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002214 } else if (Name == "chmod" ||
2215 Name == "chown" ||
2216 Name == "ctermid" ||
2217 Name == "clearerr" ||
2218 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002219 if (FTy->getNumParams() == 0 ||
2220 !isa<PointerType>(FTy->getParamType(0)))
2221 continue;
2222 setDoesNotThrow(F);
2223 setDoesNotCapture(F, 1);
2224 }
2225 break;
2226 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002227 if (Name == "atoi" ||
2228 Name == "atol" ||
2229 Name == "atof" ||
2230 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002231 if (FTy->getNumParams() != 1 ||
2232 !isa<PointerType>(FTy->getParamType(0)))
2233 continue;
2234 setDoesNotThrow(F);
2235 setOnlyReadsMemory(F);
2236 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002237 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002238 if (FTy->getNumParams() != 2 ||
2239 !isa<PointerType>(FTy->getParamType(0)))
2240 continue;
2241 setDoesNotThrow(F);
2242 setDoesNotCapture(F, 1);
2243 }
2244 break;
2245 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002246 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002247 if (FTy->getNumParams() != 2 ||
2248 !isa<PointerType>(FTy->getReturnType()) ||
2249 !isa<PointerType>(FTy->getParamType(0)) ||
2250 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002251 continue;
2252 setDoesNotThrow(F);
2253 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002254 setDoesNotCapture(F, 1);
2255 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002256 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002257 if (FTy->getNumParams() != 2 ||
2258 !isa<PointerType>(FTy->getReturnType()) ||
2259 !isa<PointerType>(FTy->getParamType(1)))
2260 continue;
2261 setDoesNotThrow(F);
2262 setDoesNotAlias(F, 0);
2263 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002264 } else if (Name == "feof" ||
2265 Name == "free" ||
2266 Name == "fseek" ||
2267 Name == "ftell" ||
2268 Name == "fgetc" ||
2269 Name == "fseeko" ||
2270 Name == "ftello" ||
2271 Name == "fileno" ||
2272 Name == "fflush" ||
2273 Name == "fclose" ||
2274 Name == "fsetpos" ||
2275 Name == "flockfile" ||
2276 Name == "funlockfile" ||
2277 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002278 if (FTy->getNumParams() == 0 ||
2279 !isa<PointerType>(FTy->getParamType(0)))
2280 continue;
2281 setDoesNotThrow(F);
2282 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002283 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002284 if (FTy->getNumParams() != 1 ||
2285 !isa<PointerType>(FTy->getParamType(0)))
2286 continue;
2287 setDoesNotThrow(F);
2288 setDoesNotCapture(F, 1);
2289 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002290 } else if (Name == "fputc" ||
2291 Name == "fstat" ||
2292 Name == "frexp" ||
2293 Name == "frexpf" ||
2294 Name == "frexpl" ||
2295 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002296 if (FTy->getNumParams() != 2 ||
2297 !isa<PointerType>(FTy->getParamType(1)))
2298 continue;
2299 setDoesNotThrow(F);
2300 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002301 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002302 if (FTy->getNumParams() != 3 ||
2303 !isa<PointerType>(FTy->getParamType(0)) ||
2304 !isa<PointerType>(FTy->getParamType(2)))
2305 continue;
2306 setDoesNotThrow(F);
2307 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002308 } else if (Name == "fread" ||
2309 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002310 if (FTy->getNumParams() != 4 ||
2311 !isa<PointerType>(FTy->getParamType(0)) ||
2312 !isa<PointerType>(FTy->getParamType(3)))
2313 continue;
2314 setDoesNotThrow(F);
2315 setDoesNotCapture(F, 1);
2316 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002317 } else if (Name == "fputs" ||
2318 Name == "fscanf" ||
2319 Name == "fprintf" ||
2320 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002321 if (FTy->getNumParams() < 2 ||
2322 !isa<PointerType>(FTy->getParamType(0)) ||
2323 !isa<PointerType>(FTy->getParamType(1)))
2324 continue;
2325 setDoesNotThrow(F);
2326 setDoesNotCapture(F, 1);
2327 setDoesNotCapture(F, 2);
2328 }
2329 break;
2330 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002331 if (Name == "getc" ||
2332 Name == "getlogin_r" ||
2333 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002334 if (FTy->getNumParams() == 0 ||
2335 !isa<PointerType>(FTy->getParamType(0)))
2336 continue;
2337 setDoesNotThrow(F);
2338 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002339 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002340 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002341 !isa<PointerType>(FTy->getParamType(0)))
2342 continue;
2343 setDoesNotThrow(F);
2344 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002345 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002346 } else if (Name == "gets" ||
2347 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002348 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002349 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002350 if (FTy->getNumParams() != 2 ||
2351 !isa<PointerType>(FTy->getParamType(1)))
2352 continue;
2353 setDoesNotThrow(F);
2354 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002355 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002356 if (FTy->getNumParams() != 1 ||
2357 !isa<PointerType>(FTy->getParamType(0)))
2358 continue;
2359 setDoesNotThrow(F);
2360 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002361 }
2362 break;
2363 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002364 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002365 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002366 !isa<PointerType>(FTy->getParamType(1)))
2367 continue;
2368 setDoesNotThrow(F);
2369 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002370 } else if (Name == "uname" ||
2371 Name == "unlink" ||
2372 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002373 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002374 !isa<PointerType>(FTy->getParamType(0)))
2375 continue;
2376 setDoesNotThrow(F);
2377 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002378 } else if (Name == "utime" ||
2379 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002380 if (FTy->getNumParams() != 2 ||
2381 !isa<PointerType>(FTy->getParamType(0)) ||
2382 !isa<PointerType>(FTy->getParamType(1)))
2383 continue;
2384 setDoesNotThrow(F);
2385 setDoesNotCapture(F, 1);
2386 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002387 }
2388 break;
2389 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002390 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002391 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002392 !isa<PointerType>(FTy->getParamType(1)))
2393 continue;
2394 setDoesNotThrow(F);
2395 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002396 } else if (Name == "puts" ||
2397 Name == "printf" ||
2398 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002399 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002400 !isa<PointerType>(FTy->getParamType(0)))
2401 continue;
2402 setDoesNotThrow(F);
2403 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002404 } else if (Name == "pread" ||
2405 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002406 if (FTy->getNumParams() != 4 ||
2407 !isa<PointerType>(FTy->getParamType(1)))
2408 continue;
2409 // May throw; these are valid pthread cancellation points.
2410 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002411 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002412 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002413 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002414 if (FTy->getNumParams() != 2 ||
2415 !isa<PointerType>(FTy->getReturnType()) ||
2416 !isa<PointerType>(FTy->getParamType(0)) ||
2417 !isa<PointerType>(FTy->getParamType(1)))
2418 continue;
2419 setDoesNotThrow(F);
2420 setDoesNotAlias(F, 0);
2421 setDoesNotCapture(F, 1);
2422 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002423 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002424 if (FTy->getNumParams() != 1 ||
2425 !isa<PointerType>(FTy->getParamType(0)))
2426 continue;
2427 setDoesNotThrow(F);
2428 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002429 }
2430 break;
2431 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002432 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002433 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002434 !isa<PointerType>(FTy->getParamType(1)))
2435 continue;
2436 setDoesNotThrow(F);
2437 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002438 } else if (Name == "vsscanf" ||
2439 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002440 if (FTy->getNumParams() != 3 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002441 !isa<PointerType>(FTy->getParamType(1)) ||
2442 !isa<PointerType>(FTy->getParamType(2)))
2443 continue;
2444 setDoesNotThrow(F);
2445 setDoesNotCapture(F, 1);
2446 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002447 } else if (Name == "valloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002448 if (!isa<PointerType>(FTy->getReturnType()))
2449 continue;
2450 setDoesNotThrow(F);
2451 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002452 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002453 if (FTy->getNumParams() != 2 ||
2454 !isa<PointerType>(FTy->getParamType(0)))
2455 continue;
2456 setDoesNotThrow(F);
2457 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002458 } else if (Name == "vfprintf" ||
2459 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002460 if (FTy->getNumParams() != 3 ||
2461 !isa<PointerType>(FTy->getParamType(0)) ||
2462 !isa<PointerType>(FTy->getParamType(1)))
2463 continue;
2464 setDoesNotThrow(F);
2465 setDoesNotCapture(F, 1);
2466 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002467 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002468 if (FTy->getNumParams() != 4 ||
2469 !isa<PointerType>(FTy->getParamType(0)) ||
2470 !isa<PointerType>(FTy->getParamType(2)))
2471 continue;
2472 setDoesNotThrow(F);
2473 setDoesNotCapture(F, 1);
2474 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002475 }
2476 break;
2477 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002478 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002479 if (FTy->getNumParams() < 2 ||
2480 !isa<PointerType>(FTy->getParamType(0)))
2481 continue;
2482 // May throw; "open" is a valid pthread cancellation point.
2483 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002484 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002485 if (FTy->getNumParams() != 1 ||
2486 !isa<PointerType>(FTy->getReturnType()) ||
2487 !isa<PointerType>(FTy->getParamType(0)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002488 continue;
2489 setDoesNotThrow(F);
2490 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002491 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002492 }
2493 break;
2494 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002495 if (Name == "tmpfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002496 if (!isa<PointerType>(FTy->getReturnType()))
2497 continue;
2498 setDoesNotThrow(F);
2499 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002500 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002501 if (FTy->getNumParams() != 1 ||
2502 !isa<PointerType>(FTy->getParamType(0)))
2503 continue;
2504 setDoesNotThrow(F);
2505 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002506 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002507 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002508 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002509 if (Name == "htonl" ||
2510 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002511 setDoesNotThrow(F);
2512 setDoesNotAccessMemory(F);
2513 }
2514 break;
2515 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002516 if (Name == "ntohl" ||
2517 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002518 setDoesNotThrow(F);
2519 setDoesNotAccessMemory(F);
2520 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002521 break;
2522 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002523 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002524 if (FTy->getNumParams() != 2 ||
2525 !isa<PointerType>(FTy->getParamType(0)) ||
2526 !isa<PointerType>(FTy->getParamType(1)))
2527 continue;
2528 setDoesNotThrow(F);
2529 setDoesNotCapture(F, 1);
2530 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002531 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002532 if (FTy->getNumParams() != 3 ||
2533 !isa<PointerType>(FTy->getParamType(0)))
2534 continue;
2535 setDoesNotThrow(F);
2536 setDoesNotCapture(F, 1);
2537 }
2538 break;
2539 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002540 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002541 if (FTy->getNumParams() != 4 ||
2542 !isa<PointerType>(FTy->getParamType(3)))
2543 continue;
2544 // May throw; places call through function pointer.
2545 setDoesNotCapture(F, 4);
2546 }
2547 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002548 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002549 if (Name == "__strdup" ||
2550 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002551 if (FTy->getNumParams() < 1 ||
2552 !isa<PointerType>(FTy->getReturnType()) ||
2553 !isa<PointerType>(FTy->getParamType(0)))
2554 continue;
2555 setDoesNotThrow(F);
2556 setDoesNotAlias(F, 0);
2557 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002558 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002559 if (FTy->getNumParams() != 3 ||
2560 !isa<PointerType>(FTy->getParamType(1)))
2561 continue;
2562 setDoesNotThrow(F);
2563 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002564 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002565 if (FTy->getNumParams() != 1 ||
2566 !isa<PointerType>(FTy->getParamType(0)))
2567 continue;
2568 setDoesNotThrow(F);
2569 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002570 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002571 if (FTy->getNumParams() != 2 ||
2572 !isa<PointerType>(FTy->getParamType(1)))
2573 continue;
2574 setDoesNotThrow(F);
2575 setDoesNotCapture(F, 2);
2576 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002577 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002578 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002579 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002580 if (FTy->getNumParams() < 1 ||
2581 !isa<PointerType>(FTy->getParamType(0)))
2582 continue;
2583 setDoesNotThrow(F);
2584 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002585 } else if (Name == "\1stat64" ||
2586 Name == "\1lstat64" ||
2587 Name == "\1statvfs64" ||
2588 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002589 if (FTy->getNumParams() < 1 ||
Nick Lewycky225f7472009-02-15 22:47:25 +00002590 !isa<PointerType>(FTy->getParamType(0)) ||
2591 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002592 continue;
2593 setDoesNotThrow(F);
2594 setDoesNotCapture(F, 1);
2595 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002596 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002597 if (FTy->getNumParams() != 2 ||
2598 !isa<PointerType>(FTy->getReturnType()) ||
2599 !isa<PointerType>(FTy->getParamType(0)) ||
2600 !isa<PointerType>(FTy->getParamType(1)))
2601 continue;
2602 setDoesNotThrow(F);
2603 setDoesNotAlias(F, 0);
2604 setDoesNotCapture(F, 1);
2605 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002606 } else if (Name == "\1fseeko64" ||
2607 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002608 if (FTy->getNumParams() == 0 ||
2609 !isa<PointerType>(FTy->getParamType(0)))
2610 continue;
2611 setDoesNotThrow(F);
2612 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002613 } else if (Name == "\1tmpfile64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002614 if (!isa<PointerType>(FTy->getReturnType()))
2615 continue;
2616 setDoesNotThrow(F);
2617 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002618 } else if (Name == "\1fstat64" ||
2619 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002620 if (FTy->getNumParams() != 2 ||
2621 !isa<PointerType>(FTy->getParamType(1)))
2622 continue;
2623 setDoesNotThrow(F);
2624 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002625 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002626 if (FTy->getNumParams() < 2 ||
2627 !isa<PointerType>(FTy->getParamType(0)))
2628 continue;
2629 // May throw; "open" is a valid pthread cancellation point.
2630 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002631 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002632 break;
2633 }
2634 }
2635 return Modified;
2636}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002637
2638// TODO:
2639// Additional cases that we need to add to this file:
2640//
2641// cbrt:
2642// * cbrt(expN(X)) -> expN(x/3)
2643// * cbrt(sqrt(x)) -> pow(x,1/6)
2644// * cbrt(sqrt(x)) -> pow(x,1/9)
2645//
2646// cos, cosf, cosl:
2647// * cos(-x) -> cos(x)
2648//
2649// exp, expf, expl:
2650// * exp(log(x)) -> x
2651//
2652// log, logf, logl:
2653// * log(exp(x)) -> x
2654// * log(x**y) -> y*log(x)
2655// * log(exp(y)) -> y*log(e)
2656// * log(exp2(y)) -> y*log(2)
2657// * log(exp10(y)) -> y*log(10)
2658// * log(sqrt(x)) -> 0.5*log(x)
2659// * log(pow(x,y)) -> y*log(x)
2660//
2661// lround, lroundf, lroundl:
2662// * lround(cnst) -> cnst'
2663//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002664// pow, powf, powl:
2665// * pow(exp(x),y) -> exp(x*y)
2666// * pow(sqrt(x),y) -> pow(x,y*0.5)
2667// * pow(pow(x,y),z)-> pow(x,y*z)
2668//
2669// puts:
2670// * puts("") -> putchar("\n")
2671//
2672// round, roundf, roundl:
2673// * round(cnst) -> cnst'
2674//
2675// signbit:
2676// * signbit(cnst) -> cnst'
2677// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2678//
2679// sqrt, sqrtf, sqrtl:
2680// * sqrt(expN(x)) -> expN(x*0.5)
2681// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2682// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2683//
2684// stpcpy:
2685// * stpcpy(str, "literal") ->
2686// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2687// strrchr:
2688// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2689// (if c is a constant integer and s is a constant string)
2690// * strrchr(s1,0) -> strchr(s1,0)
2691//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002692// strpbrk:
2693// * strpbrk(s,a) -> offset_in_for(s,a)
2694// (if s and a are both constant strings)
2695// * strpbrk(s,"") -> 0
2696// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2697//
2698// strspn, strcspn:
2699// * strspn(s,a) -> const_int (if both args are constant)
2700// * strspn("",a) -> 0
2701// * strspn(s,"") -> 0
2702// * strcspn(s,a) -> const_int (if both args are constant)
2703// * strcspn("",a) -> 0
2704// * strcspn(s,"") -> strlen(a)
2705//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002706// tan, tanf, tanl:
2707// * tan(atan(x)) -> x
2708//
2709// trunc, truncf, truncl:
2710// * trunc(cnst) -> cnst'
2711//
2712//