blob: 9183f3aac1fb1c2c57fffef2b1df22b5ee1678cd [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//===---------------------------------------===//
Eric Christopher80bf1d52009-11-21 01:01:30 +00001098// 'memcpy_chk' Optimizations
1099
1100struct MemCpyChkOpt : public LibCallOptimization {
1101 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1102 // These optimizations require TargetData.
1103 if (!TD) return 0;
1104
1105 const FunctionType *FT = Callee->getFunctionType();
1106 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1107 !isa<PointerType>(FT->getParamType(0)) ||
1108 !isa<PointerType>(FT->getParamType(1)) ||
Eric Christopherf734be22009-12-22 01:23:51 +00001109 !isa<IntegerType>(FT->getParamType(3)) ||
1110 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eric Christopher80bf1d52009-11-21 01:01:30 +00001111 return 0;
1112
1113 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1114 if (!SizeCI)
1115 return 0;
1116 if (SizeCI->isAllOnesValue()) {
1117 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
1118 return CI->getOperand(1);
1119 }
1120
1121 return 0;
1122 }
1123};
1124
1125//===---------------------------------------===//
1126// 'memset_chk' Optimizations
1127
1128struct MemSetChkOpt : public LibCallOptimization {
1129 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1130 // These optimizations require TargetData.
1131 if (!TD) return 0;
1132
1133 const FunctionType *FT = Callee->getFunctionType();
1134 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1135 !isa<PointerType>(FT->getParamType(0)) ||
1136 !isa<IntegerType>(FT->getParamType(1)) ||
Eric Christopherf734be22009-12-22 01:23:51 +00001137 !isa<IntegerType>(FT->getParamType(3)) ||
Eric Christopher80bf1d52009-11-21 01:01:30 +00001138 FT->getParamType(2) != TD->getIntPtrType(*Context))
1139 return 0;
1140
1141 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1142 if (!SizeCI)
1143 return 0;
1144 if (SizeCI->isAllOnesValue()) {
1145 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1146 false);
1147 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
1148 return CI->getOperand(1);
1149 }
1150
1151 return 0;
1152 }
1153};
1154
1155//===---------------------------------------===//
1156// 'memmove_chk' Optimizations
1157
1158struct MemMoveChkOpt : public LibCallOptimization {
1159 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1160 // These optimizations require TargetData.
1161 if (!TD) return 0;
1162
1163 const FunctionType *FT = Callee->getFunctionType();
1164 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1165 !isa<PointerType>(FT->getParamType(0)) ||
1166 !isa<PointerType>(FT->getParamType(1)) ||
Eric Christopherf734be22009-12-22 01:23:51 +00001167 !isa<IntegerType>(FT->getParamType(3)) ||
Eric Christopher80bf1d52009-11-21 01:01:30 +00001168 FT->getParamType(2) != TD->getIntPtrType(*Context))
1169 return 0;
1170
1171 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1172 if (!SizeCI)
1173 return 0;
1174 if (SizeCI->isAllOnesValue()) {
1175 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
1176 1, B);
1177 return CI->getOperand(1);
1178 }
1179
1180 return 0;
1181 }
1182};
1183
1184//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001185// Math Library Optimizations
1186//===----------------------------------------------------------------------===//
1187
1188//===---------------------------------------===//
1189// 'pow*' Optimizations
1190
Chris Lattner3e8b6632009-09-02 06:11:42 +00001191struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001192 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001193 const FunctionType *FT = Callee->getFunctionType();
1194 // Just make sure this has 2 arguments of the same FP type, which match the
1195 // result type.
1196 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1197 FT->getParamType(0) != FT->getParamType(1) ||
1198 !FT->getParamType(0)->isFloatingPoint())
1199 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001200
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001201 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
1202 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1203 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
1204 return Op1C;
1205 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +00001206 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001207 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001208
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001209 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1210 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001211
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001212 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001213 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001214
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001215 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +00001216 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1217 // This is faster than calling pow, and still handles negative zero
1218 // and negative infinite correctly.
1219 // TODO: In fast-math mode, this could be just sqrt(x).
1220 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +00001221 Value *Inf = ConstantFP::getInfinity(CI->getType());
1222 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +00001223 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1224 Callee->getAttributes());
1225 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1226 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +00001227 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
1228 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
1229 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001230 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001231
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001232 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1233 return Op1;
1234 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001235 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001236 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001237 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001238 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001239 return 0;
1240 }
1241};
1242
1243//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +00001244// 'exp2' Optimizations
1245
Chris Lattner3e8b6632009-09-02 06:11:42 +00001246struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001247 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +00001248 const FunctionType *FT = Callee->getFunctionType();
1249 // Just make sure this has 1 argument of FP type, which matches the
1250 // result type.
1251 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1252 !FT->getParamType(0)->isFloatingPoint())
1253 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001254
Chris Lattnere818f772008-05-02 18:43:35 +00001255 Value *Op = CI->getOperand(1);
1256 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1257 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1258 Value *LdExpArg = 0;
1259 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1260 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001261 LdExpArg = B.CreateSExt(OpC->getOperand(0),
1262 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001263 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1264 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001265 LdExpArg = B.CreateZExt(OpC->getOperand(0),
1266 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001267 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001268
Chris Lattnere818f772008-05-02 18:43:35 +00001269 if (LdExpArg) {
1270 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001271 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001272 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001273 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001274 Name = "ldexp";
1275 else
1276 Name = "ldexpl";
1277
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001278 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001279 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001280 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +00001281
1282 Module *M = Caller->getParent();
1283 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +00001284 Op->getType(),
1285 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001286 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1287 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1288 CI->setCallingConv(F->getCallingConv());
1289
1290 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +00001291 }
1292 return 0;
1293 }
1294};
Chris Lattnere818f772008-05-02 18:43:35 +00001295
1296//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001297// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1298
Chris Lattner3e8b6632009-09-02 06:11:42 +00001299struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001300 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001301 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001302 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1303 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001304 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001305
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001306 // If this is something like 'floor((double)floatval)', convert to floorf.
1307 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001308 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001309 return 0;
1310
1311 // floor((double)floatval) -> (double)floorf(floatval)
1312 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +00001313 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
1314 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +00001315 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001316 }
1317};
1318
1319//===----------------------------------------------------------------------===//
1320// Integer Optimizations
1321//===----------------------------------------------------------------------===//
1322
1323//===---------------------------------------===//
1324// 'ffs*' Optimizations
1325
Chris Lattner3e8b6632009-09-02 06:11:42 +00001326struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001327 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001328 const FunctionType *FT = Callee->getFunctionType();
1329 // Just make sure this has 2 arguments of the same FP type, which match the
1330 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +00001331 if (FT->getNumParams() != 1 ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001332 !FT->getReturnType()->isInteger(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001333 !isa<IntegerType>(FT->getParamType(0)))
1334 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001335
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001336 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001337
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001338 // Constant fold.
1339 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1340 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +00001341 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +00001342 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001343 CI->getValue().countTrailingZeros()+1);
1344 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001345
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001346 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1347 const Type *ArgType = Op->getType();
1348 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1349 Intrinsic::cttz, &ArgType, 1);
1350 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +00001351 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +00001352 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001353
Owen Andersona7235ea2009-07-31 20:28:14 +00001354 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001355 return B.CreateSelect(Cond, V,
1356 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001357 }
1358};
1359
1360//===---------------------------------------===//
1361// 'isdigit' Optimizations
1362
Chris Lattner3e8b6632009-09-02 06:11:42 +00001363struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001364 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001365 const FunctionType *FT = Callee->getFunctionType();
1366 // We require integer(i32)
1367 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001368 !FT->getParamType(0)->isInteger(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001369 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001370
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001371 // isdigit(c) -> (c-'0') <u 10
1372 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001373 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001374 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001375 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001376 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001377 return B.CreateZExt(Op, CI->getType());
1378 }
1379};
1380
1381//===---------------------------------------===//
1382// 'isascii' Optimizations
1383
Chris Lattner3e8b6632009-09-02 06:11:42 +00001384struct IsAsciiOpt : 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 // isascii(c) -> c <u 128
1393 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001394 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001395 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001396 return B.CreateZExt(Op, CI->getType());
1397 }
1398};
Eric Christopher37c8b862009-10-07 21:14:25 +00001399
Chris Lattner313f0e62008-06-09 08:26:51 +00001400//===---------------------------------------===//
1401// 'abs', 'labs', 'llabs' Optimizations
1402
Chris Lattner3e8b6632009-09-02 06:11:42 +00001403struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001404 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001405 const FunctionType *FT = Callee->getFunctionType();
1406 // We require integer(integer) where the types agree.
1407 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
1408 FT->getParamType(0) != FT->getReturnType())
1409 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001410
Chris Lattner313f0e62008-06-09 08:26:51 +00001411 // abs(x) -> x >s -1 ? x : -x
1412 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001413 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001414 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001415 "ispos");
1416 Value *Neg = B.CreateNeg(Op, "neg");
1417 return B.CreateSelect(Pos, Op, Neg);
1418 }
1419};
Eric Christopher37c8b862009-10-07 21:14:25 +00001420
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001421
1422//===---------------------------------------===//
1423// 'toascii' Optimizations
1424
Chris Lattner3e8b6632009-09-02 06:11:42 +00001425struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001426 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001427 const FunctionType *FT = Callee->getFunctionType();
1428 // We require i32(i32)
1429 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001430 !FT->getParamType(0)->isInteger(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001431 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001432
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001433 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001434 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +00001435 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001436 }
1437};
1438
1439//===----------------------------------------------------------------------===//
1440// Formatting and IO Optimizations
1441//===----------------------------------------------------------------------===//
1442
1443//===---------------------------------------===//
1444// 'printf' Optimizations
1445
Chris Lattner3e8b6632009-09-02 06:11:42 +00001446struct PrintFOpt : 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 // Require one fixed pointer argument and an integer/void result.
1449 const FunctionType *FT = Callee->getFunctionType();
1450 if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
1451 !(isa<IntegerType>(FT->getReturnType()) ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001452 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001453 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001454
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001455 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001456 std::string FormatStr;
1457 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1458 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001459
1460 // Empty format string -> noop.
1461 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001462 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001463 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001464
Chris Lattner74965f22009-11-09 04:57:04 +00001465 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1466 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001467 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001468 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
1469 FormatStr[0]), B);
1470 if (CI->use_empty()) return CI;
1471 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001472 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001473
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001474 // printf("foo\n") --> puts("foo")
1475 if (FormatStr[FormatStr.size()-1] == '\n' &&
1476 FormatStr.find('%') == std::string::npos) { // no format characters.
1477 // Create a string literal with no \n on it. We expect the constant merge
1478 // pass to be run after this pass, to merge duplicate strings.
1479 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001480 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001481 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1482 GlobalVariable::InternalLinkage, C, "str");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001483 EmitPutS(C, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001484 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001485 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001486 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001487
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001488 // Optimize specific format strings.
1489 // printf("%c", chr) --> putchar(*(i8*)dst)
1490 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
1491 isa<IntegerType>(CI->getOperand(2)->getType())) {
Chris Lattner74965f22009-11-09 04:57:04 +00001492 Value *Res = EmitPutChar(CI->getOperand(2), B);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001493
Chris Lattner74965f22009-11-09 04:57:04 +00001494 if (CI->use_empty()) return CI;
1495 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001496 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001497
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001498 // printf("%s\n", str) --> puts(str)
1499 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
1500 isa<PointerType>(CI->getOperand(2)->getType()) &&
1501 CI->use_empty()) {
1502 EmitPutS(CI->getOperand(2), B);
1503 return CI;
1504 }
1505 return 0;
1506 }
1507};
1508
1509//===---------------------------------------===//
1510// 'sprintf' Optimizations
1511
Chris Lattner3e8b6632009-09-02 06:11:42 +00001512struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001513 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001514 // Require two fixed pointer arguments and an integer result.
1515 const FunctionType *FT = Callee->getFunctionType();
1516 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1517 !isa<PointerType>(FT->getParamType(1)) ||
1518 !isa<IntegerType>(FT->getReturnType()))
1519 return 0;
1520
1521 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001522 std::string FormatStr;
1523 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1524 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001525
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001526 // If we just have a format string (nothing else crazy) transform it.
1527 if (CI->getNumOperands() == 3) {
1528 // Make sure there's no % in the constant array. We could try to handle
1529 // %% -> % in the future if we cared.
1530 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1531 if (FormatStr[i] == '%')
1532 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001533
1534 // These optimizations require TargetData.
1535 if (!TD) return 0;
1536
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001537 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1538 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001539 ConstantInt::get
1540 (TD->getIntPtrType(*Context), FormatStr.size()+1),1,B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001541 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001542 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001543
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001544 // The remaining optimizations require the format string to be "%s" or "%c"
1545 // and have an extra operand.
1546 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1547 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001548
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001549 // Decode the second character of the format string.
1550 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001551 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001552 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001553 Value *V = B.CreateTrunc(CI->getOperand(3),
1554 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001555 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1556 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001557 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1558 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001559 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001560
Owen Andersoneed707b2009-07-24 23:12:02 +00001561 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001562 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001563
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001564 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001565 // These optimizations require TargetData.
1566 if (!TD) return 0;
1567
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001568 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1569 if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
1570
1571 Value *Len = EmitStrLen(CI->getOperand(3), B);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001572 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001573 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001574 "leninc");
1575 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001576
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001577 // The sprintf result is the unincremented number of bytes in the string.
1578 return B.CreateIntCast(Len, CI->getType(), false);
1579 }
1580 return 0;
1581 }
1582};
1583
1584//===---------------------------------------===//
1585// 'fwrite' Optimizations
1586
Chris Lattner3e8b6632009-09-02 06:11:42 +00001587struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001588 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001589 // Require a pointer, an integer, an integer, a pointer, returning integer.
1590 const FunctionType *FT = Callee->getFunctionType();
1591 if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
1592 !isa<IntegerType>(FT->getParamType(1)) ||
1593 !isa<IntegerType>(FT->getParamType(2)) ||
1594 !isa<PointerType>(FT->getParamType(3)) ||
1595 !isa<IntegerType>(FT->getReturnType()))
1596 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001597
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001598 // Get the element size and count.
1599 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1600 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1601 if (!SizeC || !CountC) return 0;
1602 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001603
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001604 // If this is writing zero records, remove the call (it's a noop).
1605 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001606 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001607
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001608 // If this is writing one byte, turn it into fputc.
1609 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1610 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1611 EmitFPutC(Char, CI->getOperand(4), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001612 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001613 }
1614
1615 return 0;
1616 }
1617};
1618
1619//===---------------------------------------===//
1620// 'fputs' Optimizations
1621
Chris Lattner3e8b6632009-09-02 06:11:42 +00001622struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001623 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001624 // These optimizations require TargetData.
1625 if (!TD) return 0;
1626
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001627 // Require two pointers. Also, we can't optimize if return value is used.
1628 const FunctionType *FT = Callee->getFunctionType();
1629 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1630 !isa<PointerType>(FT->getParamType(1)) ||
1631 !CI->use_empty())
1632 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001633
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001634 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1635 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001636 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001637 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001638 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001639 CI->getOperand(2), B);
1640 return CI; // Known to have no uses (see above).
1641 }
1642};
1643
1644//===---------------------------------------===//
1645// 'fprintf' Optimizations
1646
Chris Lattner3e8b6632009-09-02 06:11:42 +00001647struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001648 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001649 // Require two fixed paramters as pointers and integer result.
1650 const FunctionType *FT = Callee->getFunctionType();
1651 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1652 !isa<PointerType>(FT->getParamType(1)) ||
1653 !isa<IntegerType>(FT->getReturnType()))
1654 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001655
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001656 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001657 std::string FormatStr;
1658 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1659 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001660
1661 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1662 if (CI->getNumOperands() == 3) {
1663 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1664 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001665 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001666
1667 // These optimizations require TargetData.
1668 if (!TD) return 0;
1669
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001670 EmitFWrite(CI->getOperand(2),
1671 ConstantInt::get(TD->getIntPtrType(*Context),
1672 FormatStr.size()),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001673 CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001674 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001675 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001676
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001677 // The remaining optimizations require the format string to be "%s" or "%c"
1678 // and have an extra operand.
1679 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1680 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001681
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001682 // Decode the second character of the format string.
1683 if (FormatStr[1] == 'c') {
1684 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1685 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1686 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001687 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001688 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001689
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001690 if (FormatStr[1] == 's') {
1691 // fprintf(F, "%s", str) -> fputs(str, F)
1692 if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
1693 return 0;
1694 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1695 return CI;
1696 }
1697 return 0;
1698 }
1699};
1700
Bill Wendlingac178222008-05-05 21:37:59 +00001701} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001702
1703//===----------------------------------------------------------------------===//
1704// SimplifyLibCalls Pass Implementation
1705//===----------------------------------------------------------------------===//
1706
1707namespace {
1708 /// This pass optimizes well known library functions from libc and libm.
1709 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001710 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001711 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001712 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001713 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1714 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001715 StrToOpt StrTo; StrStrOpt StrStr;
1716 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001717 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001718 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001719 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001720 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1721 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001722 // Formatting and IO Optimizations
1723 SPrintFOpt SPrintF; PrintFOpt PrintF;
1724 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001725
1726 // Object Size Checking
Eric Christopher80bf1d52009-11-21 01:01:30 +00001727 MemCpyChkOpt MemCpyChk; MemSetChkOpt MemSetChk; MemMoveChkOpt MemMoveChk;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001728
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001729 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001730 public:
1731 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001732 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001733
1734 void InitOptimizations();
1735 bool runOnFunction(Function &F);
1736
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001737 void setDoesNotAccessMemory(Function &F);
1738 void setOnlyReadsMemory(Function &F);
1739 void setDoesNotThrow(Function &F);
1740 void setDoesNotCapture(Function &F, unsigned n);
1741 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001742 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001743
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001744 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001745 }
1746 };
1747 char SimplifyLibCalls::ID = 0;
1748} // end anonymous namespace.
1749
1750static RegisterPass<SimplifyLibCalls>
1751X("simplify-libcalls", "Simplify well-known library calls");
1752
1753// Public interface to the Simplify LibCalls pass.
1754FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001755 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001756}
1757
1758/// Optimizations - Populate the Optimizations map with all the optimizations
1759/// we know.
1760void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001761 // String and Memory LibCall Optimizations
1762 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001763 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001764 Optimizations["strchr"] = &StrChr;
1765 Optimizations["strcmp"] = &StrCmp;
1766 Optimizations["strncmp"] = &StrNCmp;
1767 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001768 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001769 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001770 Optimizations["strtol"] = &StrTo;
1771 Optimizations["strtod"] = &StrTo;
1772 Optimizations["strtof"] = &StrTo;
1773 Optimizations["strtoul"] = &StrTo;
1774 Optimizations["strtoll"] = &StrTo;
1775 Optimizations["strtold"] = &StrTo;
1776 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001777 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001778 Optimizations["memcmp"] = &MemCmp;
1779 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001780 Optimizations["memmove"] = &MemMove;
1781 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001782
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001783 // Math Library Optimizations
1784 Optimizations["powf"] = &Pow;
1785 Optimizations["pow"] = &Pow;
1786 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001787 Optimizations["llvm.pow.f32"] = &Pow;
1788 Optimizations["llvm.pow.f64"] = &Pow;
1789 Optimizations["llvm.pow.f80"] = &Pow;
1790 Optimizations["llvm.pow.f128"] = &Pow;
1791 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001792 Optimizations["exp2l"] = &Exp2;
1793 Optimizations["exp2"] = &Exp2;
1794 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001795 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1796 Optimizations["llvm.exp2.f128"] = &Exp2;
1797 Optimizations["llvm.exp2.f80"] = &Exp2;
1798 Optimizations["llvm.exp2.f64"] = &Exp2;
1799 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001800
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001801#ifdef HAVE_FLOORF
1802 Optimizations["floor"] = &UnaryDoubleFP;
1803#endif
1804#ifdef HAVE_CEILF
1805 Optimizations["ceil"] = &UnaryDoubleFP;
1806#endif
1807#ifdef HAVE_ROUNDF
1808 Optimizations["round"] = &UnaryDoubleFP;
1809#endif
1810#ifdef HAVE_RINTF
1811 Optimizations["rint"] = &UnaryDoubleFP;
1812#endif
1813#ifdef HAVE_NEARBYINTF
1814 Optimizations["nearbyint"] = &UnaryDoubleFP;
1815#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001816
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001817 // Integer Optimizations
1818 Optimizations["ffs"] = &FFS;
1819 Optimizations["ffsl"] = &FFS;
1820 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001821 Optimizations["abs"] = &Abs;
1822 Optimizations["labs"] = &Abs;
1823 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001824 Optimizations["isdigit"] = &IsDigit;
1825 Optimizations["isascii"] = &IsAscii;
1826 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001827
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001828 // Formatting and IO Optimizations
1829 Optimizations["sprintf"] = &SPrintF;
1830 Optimizations["printf"] = &PrintF;
1831 Optimizations["fwrite"] = &FWrite;
1832 Optimizations["fputs"] = &FPuts;
1833 Optimizations["fprintf"] = &FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001834
1835 // Object Size Checking
Eric Christopher80bf1d52009-11-21 01:01:30 +00001836 Optimizations["__memcpy_chk"] = &MemCpyChk;
1837 Optimizations["__memset_chk"] = &MemSetChk;
1838 Optimizations["__memmove_chk"] = &MemMoveChk;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001839}
1840
1841
1842/// runOnFunction - Top level algorithm.
1843///
1844bool SimplifyLibCalls::runOnFunction(Function &F) {
1845 if (Optimizations.empty())
1846 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001847
Dan Gohmanf14d9192009-08-18 00:48:13 +00001848 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001849
Owen Andersone922c022009-07-22 00:24:57 +00001850 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001851
1852 bool Changed = false;
1853 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1854 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1855 // Ignore non-calls.
1856 CallInst *CI = dyn_cast<CallInst>(I++);
1857 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001858
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001859 // Ignore indirect calls and calls to non-external functions.
1860 Function *Callee = CI->getCalledFunction();
1861 if (Callee == 0 || !Callee->isDeclaration() ||
1862 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1863 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001864
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001865 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001866 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1867 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001868
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001869 // Set the builder to the instruction after the call.
1870 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001871
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001872 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001873 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001874 if (Result == 0) continue;
1875
David Greene6a6b90e2010-01-05 01:27:21 +00001876 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1877 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001878
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001879 // Something changed!
1880 Changed = true;
1881 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001882
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001883 // Inspect the instruction after the call (which was potentially just
1884 // added) next.
1885 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001886
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001887 if (CI != Result && !CI->use_empty()) {
1888 CI->replaceAllUsesWith(Result);
1889 if (!Result->hasName())
1890 Result->takeName(CI);
1891 }
1892 CI->eraseFromParent();
1893 }
1894 }
1895 return Changed;
1896}
1897
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001898// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001899
1900void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1901 if (!F.doesNotAccessMemory()) {
1902 F.setDoesNotAccessMemory();
1903 ++NumAnnotated;
1904 Modified = true;
1905 }
1906}
1907void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1908 if (!F.onlyReadsMemory()) {
1909 F.setOnlyReadsMemory();
1910 ++NumAnnotated;
1911 Modified = true;
1912 }
1913}
1914void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1915 if (!F.doesNotThrow()) {
1916 F.setDoesNotThrow();
1917 ++NumAnnotated;
1918 Modified = true;
1919 }
1920}
1921void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1922 if (!F.doesNotCapture(n)) {
1923 F.setDoesNotCapture(n);
1924 ++NumAnnotated;
1925 Modified = true;
1926 }
1927}
1928void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1929 if (!F.doesNotAlias(n)) {
1930 F.setDoesNotAlias(n);
1931 ++NumAnnotated;
1932 Modified = true;
1933 }
1934}
1935
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001936/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001937///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001938bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001939 Modified = false;
1940 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1941 Function &F = *I;
1942 if (!F.isDeclaration())
1943 continue;
1944
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001945 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001946 continue;
1947
1948 const FunctionType *FTy = F.getFunctionType();
1949
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001950 StringRef Name = F.getName();
1951 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001952 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001953 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001954 if (FTy->getNumParams() != 1 ||
1955 !isa<PointerType>(FTy->getParamType(0)))
1956 continue;
1957 setOnlyReadsMemory(F);
1958 setDoesNotThrow(F);
1959 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001960 } else if (Name == "strcpy" ||
1961 Name == "stpcpy" ||
1962 Name == "strcat" ||
1963 Name == "strtol" ||
1964 Name == "strtod" ||
1965 Name == "strtof" ||
1966 Name == "strtoul" ||
1967 Name == "strtoll" ||
1968 Name == "strtold" ||
1969 Name == "strncat" ||
1970 Name == "strncpy" ||
1971 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001972 if (FTy->getNumParams() < 2 ||
1973 !isa<PointerType>(FTy->getParamType(1)))
1974 continue;
1975 setDoesNotThrow(F);
1976 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001977 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001978 if (FTy->getNumParams() != 3 ||
1979 !isa<PointerType>(FTy->getParamType(0)) ||
1980 !isa<PointerType>(FTy->getParamType(1)))
1981 continue;
1982 setDoesNotThrow(F);
1983 setDoesNotCapture(F, 1);
1984 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001985 } else if (Name == "strcmp" ||
1986 Name == "strspn" ||
1987 Name == "strncmp" ||
1988 Name ==" strcspn" ||
1989 Name == "strcoll" ||
1990 Name == "strcasecmp" ||
1991 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001992 if (FTy->getNumParams() < 2 ||
1993 !isa<PointerType>(FTy->getParamType(0)) ||
1994 !isa<PointerType>(FTy->getParamType(1)))
1995 continue;
1996 setOnlyReadsMemory(F);
1997 setDoesNotThrow(F);
1998 setDoesNotCapture(F, 1);
1999 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002000 } else if (Name == "strstr" ||
2001 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002002 if (FTy->getNumParams() != 2 ||
2003 !isa<PointerType>(FTy->getParamType(1)))
2004 continue;
2005 setOnlyReadsMemory(F);
2006 setDoesNotThrow(F);
2007 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002008 } else if (Name == "strtok" ||
2009 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002010 if (FTy->getNumParams() < 2 ||
2011 !isa<PointerType>(FTy->getParamType(1)))
2012 continue;
2013 setDoesNotThrow(F);
2014 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002015 } else if (Name == "scanf" ||
2016 Name == "setbuf" ||
2017 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002018 if (FTy->getNumParams() < 1 ||
2019 !isa<PointerType>(FTy->getParamType(0)))
2020 continue;
2021 setDoesNotThrow(F);
2022 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002023 } else if (Name == "strdup" ||
2024 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00002025 if (FTy->getNumParams() < 1 ||
2026 !isa<PointerType>(FTy->getReturnType()) ||
2027 !isa<PointerType>(FTy->getParamType(0)))
2028 continue;
2029 setDoesNotThrow(F);
2030 setDoesNotAlias(F, 0);
2031 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002032 } else if (Name == "stat" ||
2033 Name == "sscanf" ||
2034 Name == "sprintf" ||
2035 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002036 if (FTy->getNumParams() < 2 ||
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002037 !isa<PointerType>(FTy->getParamType(0)) ||
2038 !isa<PointerType>(FTy->getParamType(1)))
2039 continue;
2040 setDoesNotThrow(F);
2041 setDoesNotCapture(F, 1);
2042 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002043 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002044 if (FTy->getNumParams() != 3 ||
2045 !isa<PointerType>(FTy->getParamType(0)) ||
2046 !isa<PointerType>(FTy->getParamType(2)))
2047 continue;
2048 setDoesNotThrow(F);
2049 setDoesNotCapture(F, 1);
2050 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002051 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002052 if (FTy->getNumParams() != 3 ||
2053 !isa<PointerType>(FTy->getParamType(1)) ||
2054 !isa<PointerType>(FTy->getParamType(2)))
2055 continue;
2056 setDoesNotThrow(F);
2057 setDoesNotCapture(F, 2);
2058 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002059 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002060 if (FTy->getNumParams() != 1 ||
2061 !isa<PointerType>(FTy->getParamType(0)))
2062 continue;
2063 // May throw; "system" is a valid pthread cancellation point.
2064 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002065 }
2066 break;
2067 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00002068 if (Name == "malloc") {
2069 if (FTy->getNumParams() != 1 ||
2070 !isa<PointerType>(FTy->getReturnType()))
2071 continue;
2072 setDoesNotThrow(F);
2073 setDoesNotAlias(F, 0);
2074 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002075 if (FTy->getNumParams() != 3 ||
2076 !isa<PointerType>(FTy->getParamType(0)) ||
2077 !isa<PointerType>(FTy->getParamType(1)))
2078 continue;
2079 setOnlyReadsMemory(F);
2080 setDoesNotThrow(F);
2081 setDoesNotCapture(F, 1);
2082 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002083 } else if (Name == "memchr" ||
2084 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002085 if (FTy->getNumParams() != 3)
2086 continue;
2087 setOnlyReadsMemory(F);
2088 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002089 } else if (Name == "modf" ||
2090 Name == "modff" ||
2091 Name == "modfl" ||
2092 Name == "memcpy" ||
2093 Name == "memccpy" ||
2094 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002095 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002096 !isa<PointerType>(FTy->getParamType(1)))
2097 continue;
2098 setDoesNotThrow(F);
2099 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002100 } else if (Name == "memalign") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002101 if (!isa<PointerType>(FTy->getReturnType()))
2102 continue;
2103 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002104 } else if (Name == "mkdir" ||
2105 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002106 if (FTy->getNumParams() == 0 ||
2107 !isa<PointerType>(FTy->getParamType(0)))
2108 continue;
2109 setDoesNotThrow(F);
2110 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002111 }
2112 break;
2113 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002114 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002115 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002116 !isa<PointerType>(FTy->getParamType(0)) ||
2117 !isa<PointerType>(FTy->getReturnType()))
2118 continue;
2119 setDoesNotThrow(F);
2120 setDoesNotAlias(F, 0);
2121 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002122 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002123 if (FTy->getNumParams() != 3 ||
2124 !isa<PointerType>(FTy->getParamType(1)))
2125 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002126 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002127 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002128 } else if (Name == "rmdir" ||
2129 Name == "rewind" ||
2130 Name == "remove" ||
2131 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002132 if (FTy->getNumParams() < 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002133 !isa<PointerType>(FTy->getParamType(0)))
2134 continue;
2135 setDoesNotThrow(F);
2136 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002137 } else if (Name == "rename" ||
2138 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002139 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002140 !isa<PointerType>(FTy->getParamType(0)) ||
2141 !isa<PointerType>(FTy->getParamType(1)))
2142 continue;
2143 setDoesNotThrow(F);
2144 setDoesNotCapture(F, 1);
2145 setDoesNotCapture(F, 2);
2146 }
2147 break;
2148 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002149 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002150 if (FTy->getNumParams() != 3 ||
2151 !isa<PointerType>(FTy->getParamType(1)))
2152 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002153 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002154 setDoesNotCapture(F, 2);
2155 }
2156 break;
2157 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002158 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002159 if (FTy->getNumParams() != 3 ||
2160 !isa<PointerType>(FTy->getParamType(0)) ||
2161 !isa<PointerType>(FTy->getParamType(1)))
2162 continue;
2163 setDoesNotThrow(F);
2164 setDoesNotCapture(F, 1);
2165 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002166 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002167 if (FTy->getNumParams() != 3 ||
2168 !isa<PointerType>(FTy->getParamType(0)) ||
2169 !isa<PointerType>(FTy->getParamType(1)))
2170 continue;
2171 setDoesNotThrow(F);
2172 setOnlyReadsMemory(F);
2173 setDoesNotCapture(F, 1);
2174 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002175 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002176 if (FTy->getNumParams() != 2 ||
2177 !isa<PointerType>(FTy->getParamType(0)))
2178 continue;
2179 setDoesNotThrow(F);
2180 setDoesNotCapture(F, 1);
2181 }
2182 break;
2183 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002184 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002185 if (FTy->getNumParams() != 2 ||
2186 !isa<PointerType>(FTy->getReturnType()))
2187 continue;
2188 setDoesNotThrow(F);
2189 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002190 } else if (Name == "chmod" ||
2191 Name == "chown" ||
2192 Name == "ctermid" ||
2193 Name == "clearerr" ||
2194 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002195 if (FTy->getNumParams() == 0 ||
2196 !isa<PointerType>(FTy->getParamType(0)))
2197 continue;
2198 setDoesNotThrow(F);
2199 setDoesNotCapture(F, 1);
2200 }
2201 break;
2202 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002203 if (Name == "atoi" ||
2204 Name == "atol" ||
2205 Name == "atof" ||
2206 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002207 if (FTy->getNumParams() != 1 ||
2208 !isa<PointerType>(FTy->getParamType(0)))
2209 continue;
2210 setDoesNotThrow(F);
2211 setOnlyReadsMemory(F);
2212 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002213 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002214 if (FTy->getNumParams() != 2 ||
2215 !isa<PointerType>(FTy->getParamType(0)))
2216 continue;
2217 setDoesNotThrow(F);
2218 setDoesNotCapture(F, 1);
2219 }
2220 break;
2221 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002222 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002223 if (FTy->getNumParams() != 2 ||
2224 !isa<PointerType>(FTy->getReturnType()) ||
2225 !isa<PointerType>(FTy->getParamType(0)) ||
2226 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002227 continue;
2228 setDoesNotThrow(F);
2229 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002230 setDoesNotCapture(F, 1);
2231 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002232 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002233 if (FTy->getNumParams() != 2 ||
2234 !isa<PointerType>(FTy->getReturnType()) ||
2235 !isa<PointerType>(FTy->getParamType(1)))
2236 continue;
2237 setDoesNotThrow(F);
2238 setDoesNotAlias(F, 0);
2239 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002240 } else if (Name == "feof" ||
2241 Name == "free" ||
2242 Name == "fseek" ||
2243 Name == "ftell" ||
2244 Name == "fgetc" ||
2245 Name == "fseeko" ||
2246 Name == "ftello" ||
2247 Name == "fileno" ||
2248 Name == "fflush" ||
2249 Name == "fclose" ||
2250 Name == "fsetpos" ||
2251 Name == "flockfile" ||
2252 Name == "funlockfile" ||
2253 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002254 if (FTy->getNumParams() == 0 ||
2255 !isa<PointerType>(FTy->getParamType(0)))
2256 continue;
2257 setDoesNotThrow(F);
2258 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002259 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002260 if (FTy->getNumParams() != 1 ||
2261 !isa<PointerType>(FTy->getParamType(0)))
2262 continue;
2263 setDoesNotThrow(F);
2264 setDoesNotCapture(F, 1);
2265 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002266 } else if (Name == "fputc" ||
2267 Name == "fstat" ||
2268 Name == "frexp" ||
2269 Name == "frexpf" ||
2270 Name == "frexpl" ||
2271 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002272 if (FTy->getNumParams() != 2 ||
2273 !isa<PointerType>(FTy->getParamType(1)))
2274 continue;
2275 setDoesNotThrow(F);
2276 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002277 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002278 if (FTy->getNumParams() != 3 ||
2279 !isa<PointerType>(FTy->getParamType(0)) ||
2280 !isa<PointerType>(FTy->getParamType(2)))
2281 continue;
2282 setDoesNotThrow(F);
2283 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002284 } else if (Name == "fread" ||
2285 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002286 if (FTy->getNumParams() != 4 ||
2287 !isa<PointerType>(FTy->getParamType(0)) ||
2288 !isa<PointerType>(FTy->getParamType(3)))
2289 continue;
2290 setDoesNotThrow(F);
2291 setDoesNotCapture(F, 1);
2292 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002293 } else if (Name == "fputs" ||
2294 Name == "fscanf" ||
2295 Name == "fprintf" ||
2296 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002297 if (FTy->getNumParams() < 2 ||
2298 !isa<PointerType>(FTy->getParamType(0)) ||
2299 !isa<PointerType>(FTy->getParamType(1)))
2300 continue;
2301 setDoesNotThrow(F);
2302 setDoesNotCapture(F, 1);
2303 setDoesNotCapture(F, 2);
2304 }
2305 break;
2306 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002307 if (Name == "getc" ||
2308 Name == "getlogin_r" ||
2309 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002310 if (FTy->getNumParams() == 0 ||
2311 !isa<PointerType>(FTy->getParamType(0)))
2312 continue;
2313 setDoesNotThrow(F);
2314 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002315 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002316 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002317 !isa<PointerType>(FTy->getParamType(0)))
2318 continue;
2319 setDoesNotThrow(F);
2320 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002321 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002322 } else if (Name == "gets" ||
2323 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002324 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002325 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002326 if (FTy->getNumParams() != 2 ||
2327 !isa<PointerType>(FTy->getParamType(1)))
2328 continue;
2329 setDoesNotThrow(F);
2330 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002331 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002332 if (FTy->getNumParams() != 1 ||
2333 !isa<PointerType>(FTy->getParamType(0)))
2334 continue;
2335 setDoesNotThrow(F);
2336 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002337 }
2338 break;
2339 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002340 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002341 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002342 !isa<PointerType>(FTy->getParamType(1)))
2343 continue;
2344 setDoesNotThrow(F);
2345 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002346 } else if (Name == "uname" ||
2347 Name == "unlink" ||
2348 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002349 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002350 !isa<PointerType>(FTy->getParamType(0)))
2351 continue;
2352 setDoesNotThrow(F);
2353 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002354 } else if (Name == "utime" ||
2355 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002356 if (FTy->getNumParams() != 2 ||
2357 !isa<PointerType>(FTy->getParamType(0)) ||
2358 !isa<PointerType>(FTy->getParamType(1)))
2359 continue;
2360 setDoesNotThrow(F);
2361 setDoesNotCapture(F, 1);
2362 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002363 }
2364 break;
2365 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002366 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002367 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002368 !isa<PointerType>(FTy->getParamType(1)))
2369 continue;
2370 setDoesNotThrow(F);
2371 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002372 } else if (Name == "puts" ||
2373 Name == "printf" ||
2374 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002375 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002376 !isa<PointerType>(FTy->getParamType(0)))
2377 continue;
2378 setDoesNotThrow(F);
2379 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002380 } else if (Name == "pread" ||
2381 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002382 if (FTy->getNumParams() != 4 ||
2383 !isa<PointerType>(FTy->getParamType(1)))
2384 continue;
2385 // May throw; these are valid pthread cancellation points.
2386 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002387 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002388 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002389 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002390 if (FTy->getNumParams() != 2 ||
2391 !isa<PointerType>(FTy->getReturnType()) ||
2392 !isa<PointerType>(FTy->getParamType(0)) ||
2393 !isa<PointerType>(FTy->getParamType(1)))
2394 continue;
2395 setDoesNotThrow(F);
2396 setDoesNotAlias(F, 0);
2397 setDoesNotCapture(F, 1);
2398 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002399 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002400 if (FTy->getNumParams() != 1 ||
2401 !isa<PointerType>(FTy->getParamType(0)))
2402 continue;
2403 setDoesNotThrow(F);
2404 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002405 }
2406 break;
2407 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002408 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002409 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002410 !isa<PointerType>(FTy->getParamType(1)))
2411 continue;
2412 setDoesNotThrow(F);
2413 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002414 } else if (Name == "vsscanf" ||
2415 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002416 if (FTy->getNumParams() != 3 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002417 !isa<PointerType>(FTy->getParamType(1)) ||
2418 !isa<PointerType>(FTy->getParamType(2)))
2419 continue;
2420 setDoesNotThrow(F);
2421 setDoesNotCapture(F, 1);
2422 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002423 } else if (Name == "valloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002424 if (!isa<PointerType>(FTy->getReturnType()))
2425 continue;
2426 setDoesNotThrow(F);
2427 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002428 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002429 if (FTy->getNumParams() != 2 ||
2430 !isa<PointerType>(FTy->getParamType(0)))
2431 continue;
2432 setDoesNotThrow(F);
2433 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002434 } else if (Name == "vfprintf" ||
2435 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002436 if (FTy->getNumParams() != 3 ||
2437 !isa<PointerType>(FTy->getParamType(0)) ||
2438 !isa<PointerType>(FTy->getParamType(1)))
2439 continue;
2440 setDoesNotThrow(F);
2441 setDoesNotCapture(F, 1);
2442 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002443 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002444 if (FTy->getNumParams() != 4 ||
2445 !isa<PointerType>(FTy->getParamType(0)) ||
2446 !isa<PointerType>(FTy->getParamType(2)))
2447 continue;
2448 setDoesNotThrow(F);
2449 setDoesNotCapture(F, 1);
2450 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002451 }
2452 break;
2453 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002454 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002455 if (FTy->getNumParams() < 2 ||
2456 !isa<PointerType>(FTy->getParamType(0)))
2457 continue;
2458 // May throw; "open" is a valid pthread cancellation point.
2459 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002460 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002461 if (FTy->getNumParams() != 1 ||
2462 !isa<PointerType>(FTy->getReturnType()) ||
2463 !isa<PointerType>(FTy->getParamType(0)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002464 continue;
2465 setDoesNotThrow(F);
2466 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002467 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002468 }
2469 break;
2470 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002471 if (Name == "tmpfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002472 if (!isa<PointerType>(FTy->getReturnType()))
2473 continue;
2474 setDoesNotThrow(F);
2475 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002476 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002477 if (FTy->getNumParams() != 1 ||
2478 !isa<PointerType>(FTy->getParamType(0)))
2479 continue;
2480 setDoesNotThrow(F);
2481 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002482 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002483 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002484 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002485 if (Name == "htonl" ||
2486 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002487 setDoesNotThrow(F);
2488 setDoesNotAccessMemory(F);
2489 }
2490 break;
2491 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002492 if (Name == "ntohl" ||
2493 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002494 setDoesNotThrow(F);
2495 setDoesNotAccessMemory(F);
2496 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002497 break;
2498 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002499 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002500 if (FTy->getNumParams() != 2 ||
2501 !isa<PointerType>(FTy->getParamType(0)) ||
2502 !isa<PointerType>(FTy->getParamType(1)))
2503 continue;
2504 setDoesNotThrow(F);
2505 setDoesNotCapture(F, 1);
2506 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002507 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002508 if (FTy->getNumParams() != 3 ||
2509 !isa<PointerType>(FTy->getParamType(0)))
2510 continue;
2511 setDoesNotThrow(F);
2512 setDoesNotCapture(F, 1);
2513 }
2514 break;
2515 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002516 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002517 if (FTy->getNumParams() != 4 ||
2518 !isa<PointerType>(FTy->getParamType(3)))
2519 continue;
2520 // May throw; places call through function pointer.
2521 setDoesNotCapture(F, 4);
2522 }
2523 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002524 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002525 if (Name == "__strdup" ||
2526 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002527 if (FTy->getNumParams() < 1 ||
2528 !isa<PointerType>(FTy->getReturnType()) ||
2529 !isa<PointerType>(FTy->getParamType(0)))
2530 continue;
2531 setDoesNotThrow(F);
2532 setDoesNotAlias(F, 0);
2533 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002534 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002535 if (FTy->getNumParams() != 3 ||
2536 !isa<PointerType>(FTy->getParamType(1)))
2537 continue;
2538 setDoesNotThrow(F);
2539 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002540 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002541 if (FTy->getNumParams() != 1 ||
2542 !isa<PointerType>(FTy->getParamType(0)))
2543 continue;
2544 setDoesNotThrow(F);
2545 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002546 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002547 if (FTy->getNumParams() != 2 ||
2548 !isa<PointerType>(FTy->getParamType(1)))
2549 continue;
2550 setDoesNotThrow(F);
2551 setDoesNotCapture(F, 2);
2552 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002553 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002554 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002555 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002556 if (FTy->getNumParams() < 1 ||
2557 !isa<PointerType>(FTy->getParamType(0)))
2558 continue;
2559 setDoesNotThrow(F);
2560 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002561 } else if (Name == "\1stat64" ||
2562 Name == "\1lstat64" ||
2563 Name == "\1statvfs64" ||
2564 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002565 if (FTy->getNumParams() < 1 ||
Nick Lewycky225f7472009-02-15 22:47:25 +00002566 !isa<PointerType>(FTy->getParamType(0)) ||
2567 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002568 continue;
2569 setDoesNotThrow(F);
2570 setDoesNotCapture(F, 1);
2571 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002572 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002573 if (FTy->getNumParams() != 2 ||
2574 !isa<PointerType>(FTy->getReturnType()) ||
2575 !isa<PointerType>(FTy->getParamType(0)) ||
2576 !isa<PointerType>(FTy->getParamType(1)))
2577 continue;
2578 setDoesNotThrow(F);
2579 setDoesNotAlias(F, 0);
2580 setDoesNotCapture(F, 1);
2581 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002582 } else if (Name == "\1fseeko64" ||
2583 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002584 if (FTy->getNumParams() == 0 ||
2585 !isa<PointerType>(FTy->getParamType(0)))
2586 continue;
2587 setDoesNotThrow(F);
2588 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002589 } else if (Name == "\1tmpfile64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002590 if (!isa<PointerType>(FTy->getReturnType()))
2591 continue;
2592 setDoesNotThrow(F);
2593 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002594 } else if (Name == "\1fstat64" ||
2595 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002596 if (FTy->getNumParams() != 2 ||
2597 !isa<PointerType>(FTy->getParamType(1)))
2598 continue;
2599 setDoesNotThrow(F);
2600 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002601 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002602 if (FTy->getNumParams() < 2 ||
2603 !isa<PointerType>(FTy->getParamType(0)))
2604 continue;
2605 // May throw; "open" is a valid pthread cancellation point.
2606 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002607 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002608 break;
2609 }
2610 }
2611 return Modified;
2612}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002613
2614// TODO:
2615// Additional cases that we need to add to this file:
2616//
2617// cbrt:
2618// * cbrt(expN(X)) -> expN(x/3)
2619// * cbrt(sqrt(x)) -> pow(x,1/6)
2620// * cbrt(sqrt(x)) -> pow(x,1/9)
2621//
2622// cos, cosf, cosl:
2623// * cos(-x) -> cos(x)
2624//
2625// exp, expf, expl:
2626// * exp(log(x)) -> x
2627//
2628// log, logf, logl:
2629// * log(exp(x)) -> x
2630// * log(x**y) -> y*log(x)
2631// * log(exp(y)) -> y*log(e)
2632// * log(exp2(y)) -> y*log(2)
2633// * log(exp10(y)) -> y*log(10)
2634// * log(sqrt(x)) -> 0.5*log(x)
2635// * log(pow(x,y)) -> y*log(x)
2636//
2637// lround, lroundf, lroundl:
2638// * lround(cnst) -> cnst'
2639//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002640// pow, powf, powl:
2641// * pow(exp(x),y) -> exp(x*y)
2642// * pow(sqrt(x),y) -> pow(x,y*0.5)
2643// * pow(pow(x,y),z)-> pow(x,y*z)
2644//
2645// puts:
2646// * puts("") -> putchar("\n")
2647//
2648// round, roundf, roundl:
2649// * round(cnst) -> cnst'
2650//
2651// signbit:
2652// * signbit(cnst) -> cnst'
2653// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2654//
2655// sqrt, sqrtf, sqrtl:
2656// * sqrt(expN(x)) -> expN(x*0.5)
2657// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2658// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2659//
2660// stpcpy:
2661// * stpcpy(str, "literal") ->
2662// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2663// strrchr:
2664// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2665// (if c is a constant integer and s is a constant string)
2666// * strrchr(s1,0) -> strchr(s1,0)
2667//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002668// strpbrk:
2669// * strpbrk(s,a) -> offset_in_for(s,a)
2670// (if s and a are both constant strings)
2671// * strpbrk(s,"") -> 0
2672// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2673//
2674// strspn, strcspn:
2675// * strspn(s,a) -> const_int (if both args are constant)
2676// * strspn("",a) -> 0
2677// * strspn(s,"") -> 0
2678// * strcspn(s,a) -> const_int (if both args are constant)
2679// * strcspn("",a) -> 0
2680// * strcspn(s,"") -> strlen(a)
2681//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002682// tan, tanf, tanl:
2683// * tan(atan(x)) -> x
2684//
2685// trunc, truncf, truncl:
2686// * trunc(cnst) -> cnst'
2687//
2688//