blob: ecca3c73b906ab664ce0bbc9bf09aabd4092f7fa [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 Lattnerfd1cbbe2008-05-01 06:25:24 +000079 /// EmitMemCpy - Emit a call to the memcpy function to the builder. This
80 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
Eric Christopher37c8b862009-10-07 21:14:25 +000081 Value *EmitMemCpy(Value *Dst, Value *Src, Value *Len,
Eric Christopher7a61d702008-08-08 19:39:37 +000082 unsigned Align, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +000083
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000084 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
85 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
Eric Christopher7a61d702008-08-08 19:39:37 +000086 Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B);
Nick Lewycky13a09e22008-12-21 00:19:21 +000087
88 /// EmitMemCmp - Emit a call to the memcmp function.
89 Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B);
90
Chris Lattnerf5b6bc72009-04-12 05:06:39 +000091 /// EmitMemSet - Emit a call to the memset function
92 Value *EmitMemSet(Value *Dst, Value *Val, Value *Len, IRBuilder<> &B);
93
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000094 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
95 /// 'floor'). This function is known to take a single of type matching 'Op'
96 /// and returns one value with the same type. If 'Op' is a long double, 'l'
97 /// is added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
Dan Gohman79cb8402009-09-25 23:10:17 +000098 Value *EmitUnaryFloatFnCall(Value *Op, const char *Name, IRBuilder<> &B,
99 const AttrListPtr &Attrs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000100
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000101 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
102 /// is an integer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000103 void EmitPutChar(Value *Char, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000104
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000105 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
106 /// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000107 void EmitPutS(Value *Str, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000108
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000109 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
110 /// an i32, and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000111 void EmitFPutC(Value *Char, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000112
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000113 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
114 /// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000115 void EmitFPutS(Value *Str, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000116
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000117 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
118 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000119 void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000120
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000121};
122} // End anonymous namespace.
123
124/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Eric Christopher7a61d702008-08-08 19:39:37 +0000125Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) {
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000126 return
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000127 B.CreateBitCast(V, Type::getInt8PtrTy(*Context), "cstr");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000128}
129
130/// EmitStrLen - Emit a call to the strlen function to the builder, for the
131/// specified pointer. This always returns an integer value of size intptr_t.
Eric Christopher7a61d702008-08-08 19:39:37 +0000132Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000133 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000134 AttributeWithIndex AWI[2];
135 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
136 AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
137 Attribute::NoUnwind);
138
139 Constant *StrLen =M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000140 TD->getIntPtrType(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000141 Type::getInt8PtrTy(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000142 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000143 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
144 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
145 CI->setCallingConv(F->getCallingConv());
146
147 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000148}
149
150/// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
151/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
152Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
Eric Christopher7a61d702008-08-08 19:39:37 +0000153 unsigned Align, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000154 Module *M = Caller->getParent();
Chris Lattner824b9582008-11-21 16:42:48 +0000155 Intrinsic::ID IID = Intrinsic::memcpy;
156 const Type *Tys[1];
157 Tys[0] = Len->getType();
158 Value *MemCpy = Intrinsic::getDeclaration(M, IID, Tys, 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000159 return B.CreateCall4(MemCpy, CastToCStr(Dst, B), CastToCStr(Src, B), Len,
Owen Anderson1d0be152009-08-13 21:58:54 +0000160 ConstantInt::get(Type::getInt32Ty(*Context), Align));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000161}
162
163/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
164/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
165Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
Eric Christopher7a61d702008-08-08 19:39:37 +0000166 Value *Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000167 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000168 AttributeWithIndex AWI;
169 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
170
171 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
Eric Christopher37c8b862009-10-07 21:14:25 +0000172 Type::getInt8PtrTy(*Context),
173 Type::getInt8PtrTy(*Context),
174 Type::getInt32Ty(*Context),
175 TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000176 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000177 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
178
179 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
180 CI->setCallingConv(F->getCallingConv());
181
182 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000183}
184
Nick Lewycky13a09e22008-12-21 00:19:21 +0000185/// EmitMemCmp - Emit a call to the memcmp function.
186Value *LibCallOptimization::EmitMemCmp(Value *Ptr1, Value *Ptr2,
187 Value *Len, IRBuilder<> &B) {
188 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000189 AttributeWithIndex AWI[3];
190 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
191 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
192 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
193 Attribute::NoUnwind);
194
195 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000196 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000197 Type::getInt8PtrTy(*Context),
198 Type::getInt8PtrTy(*Context),
Owen Anderson1d0be152009-08-13 21:58:54 +0000199 TD->getIntPtrType(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000200 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
201 Len, "memcmp");
202
203 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
204 CI->setCallingConv(F->getCallingConv());
205
206 return CI;
Nick Lewycky13a09e22008-12-21 00:19:21 +0000207}
208
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000209/// EmitMemSet - Emit a call to the memset function
210Value *LibCallOptimization::EmitMemSet(Value *Dst, Value *Val,
211 Value *Len, IRBuilder<> &B) {
212 Module *M = Caller->getParent();
213 Intrinsic::ID IID = Intrinsic::memset;
214 const Type *Tys[1];
215 Tys[0] = Len->getType();
216 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000217 Value *Align = ConstantInt::get(Type::getInt32Ty(*Context), 1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000218 return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
219}
220
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000221/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
222/// 'floor'). This function is known to take a single of type matching 'Op' and
223/// returns one value with the same type. If 'Op' is a long double, 'l' is
224/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
225Value *LibCallOptimization::EmitUnaryFloatFnCall(Value *Op, const char *Name,
Dan Gohman79cb8402009-09-25 23:10:17 +0000226 IRBuilder<> &B,
227 const AttrListPtr &Attrs) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000228 char NameBuffer[20];
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000229 if (!Op->getType()->isDoubleTy()) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000230 // If we need to add a suffix, copy into NameBuffer.
231 unsigned NameLen = strlen(Name);
232 assert(NameLen < sizeof(NameBuffer)-2);
233 memcpy(NameBuffer, Name, NameLen);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000234 if (Op->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000235 NameBuffer[NameLen] = 'f'; // floorf
236 else
237 NameBuffer[NameLen] = 'l'; // floorl
238 NameBuffer[NameLen+1] = 0;
239 Name = NameBuffer;
240 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000241
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000242 Module *M = Caller->getParent();
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000243 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000244 Op->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000245 CallInst *CI = B.CreateCall(Callee, Op, Name);
Dan Gohman79cb8402009-09-25 23:10:17 +0000246 CI->setAttributes(Attrs);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000247 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
248 CI->setCallingConv(F->getCallingConv());
249
250 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000251}
252
253/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
254/// is an integer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000255void LibCallOptimization::EmitPutChar(Value *Char, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000256 Module *M = Caller->getParent();
Owen Anderson1d0be152009-08-13 21:58:54 +0000257 Value *PutChar = M->getOrInsertFunction("putchar", Type::getInt32Ty(*Context),
258 Type::getInt32Ty(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000259 CallInst *CI = B.CreateCall(PutChar,
Eric Christopher37c8b862009-10-07 21:14:25 +0000260 B.CreateIntCast(Char,
261 Type::getInt32Ty(*Context),
262 "chari"),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000263 "putchar");
264
265 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
266 CI->setCallingConv(F->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000267}
268
269/// EmitPutS - Emit a call to the puts function. This assumes that Str is
270/// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000271void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000272 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000273 AttributeWithIndex AWI[2];
274 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
275 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
276
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000277 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000278 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000279 Type::getInt8PtrTy(*Context),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000280 NULL);
281 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
282 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
283 CI->setCallingConv(F->getCallingConv());
284
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000285}
286
287/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
288/// an integer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000289void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000290 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000291 AttributeWithIndex AWI[2];
292 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
293 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
294 Constant *F;
295 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000296 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
297 Type::getInt32Ty(*Context),
298 Type::getInt32Ty(*Context), File->getType(),
299 NULL);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000300 else
Eric Christopher37c8b862009-10-07 21:14:25 +0000301 F = M->getOrInsertFunction("fputc",
302 Type::getInt32Ty(*Context),
303 Type::getInt32Ty(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000304 File->getType(), NULL);
Owen Anderson1d0be152009-08-13 21:58:54 +0000305 Char = B.CreateIntCast(Char, Type::getInt32Ty(*Context), "chari");
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000306 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
307
308 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
309 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000310}
311
312/// EmitFPutS - Emit a call to the puts function. Str is required to be a
313/// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000314void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000315 Module *M = Caller->getParent();
Nick Lewycky225f7472009-02-15 22:47:25 +0000316 AttributeWithIndex AWI[3];
317 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
318 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
319 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000320 Constant *F;
321 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000322 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
323 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000324 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000325 File->getType(), NULL);
326 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000327 F = M->getOrInsertFunction("fputs", Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000328 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000329 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000330 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
331
332 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
333 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000334}
335
336/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
337/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
338void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Eric Christopher7a61d702008-08-08 19:39:37 +0000339 IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000340 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000341 AttributeWithIndex AWI[3];
342 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
343 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
344 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
345 Constant *F;
346 if (isa<PointerType>(File->getType()))
347 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000348 TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000349 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000350 TD->getIntPtrType(*Context),
351 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000352 File->getType(), NULL);
353 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000354 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000355 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000356 TD->getIntPtrType(*Context),
357 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000358 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000359 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Owen Anderson1d0be152009-08-13 21:58:54 +0000360 ConstantInt::get(TD->getIntPtrType(*Context), 1), File);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000361
362 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
363 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000364}
365
366//===----------------------------------------------------------------------===//
367// Helper Functions
368//===----------------------------------------------------------------------===//
369
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000370/// GetStringLengthH - If we can compute the length of the string pointed to by
371/// the specified pointer, return 'len+1'. If we can't, return 0.
372static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
373 // Look through noop bitcast instructions.
374 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
375 return GetStringLengthH(BCI->getOperand(0), PHIs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000376
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000377 // If this is a PHI node, there are two cases: either we have already seen it
378 // or we haven't.
379 if (PHINode *PN = dyn_cast<PHINode>(V)) {
380 if (!PHIs.insert(PN))
381 return ~0ULL; // already in the set.
Eric Christopher37c8b862009-10-07 21:14:25 +0000382
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000383 // If it was new, see if all the input strings are the same length.
384 uint64_t LenSoFar = ~0ULL;
385 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
386 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
387 if (Len == 0) return 0; // Unknown length -> unknown.
Eric Christopher37c8b862009-10-07 21:14:25 +0000388
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000389 if (Len == ~0ULL) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +0000390
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000391 if (Len != LenSoFar && LenSoFar != ~0ULL)
392 return 0; // Disagree -> unknown.
393 LenSoFar = Len;
394 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000395
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000396 // Success, all agree.
397 return LenSoFar;
398 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000399
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000400 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
401 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
402 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
403 if (Len1 == 0) return 0;
404 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
405 if (Len2 == 0) return 0;
406 if (Len1 == ~0ULL) return Len2;
407 if (Len2 == ~0ULL) return Len1;
408 if (Len1 != Len2) return 0;
409 return Len1;
410 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000411
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000412 // If the value is not a GEP instruction nor a constant expression with a
413 // GEP instruction, then return unknown.
414 User *GEP = 0;
415 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
416 GEP = GEPI;
417 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
418 if (CE->getOpcode() != Instruction::GetElementPtr)
419 return 0;
420 GEP = CE;
421 } else {
422 return 0;
423 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000424
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000425 // Make sure the GEP has exactly three arguments.
426 if (GEP->getNumOperands() != 3)
427 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000428
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000429 // Check to make sure that the first operand of the GEP is an integer and
430 // has value 0 so that we are sure we're indexing into the initializer.
431 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
432 if (!Idx->isZero())
433 return 0;
434 } else
435 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000436
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000437 // If the second index isn't a ConstantInt, then this is a variable index
438 // into the array. If this occurs, we can't say anything meaningful about
439 // the string.
440 uint64_t StartIdx = 0;
441 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
442 StartIdx = CI->getZExtValue();
443 else
444 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000445
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000446 // The GEP instruction, constant or instruction, must reference a global
447 // variable that is a constant and is initialized. The referenced constant
448 // initializer is the array that we'll use for optimization.
449 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
Dan Gohman107f41f2009-08-19 00:11:12 +0000450 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
451 GV->mayBeOverridden())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000452 return 0;
453 Constant *GlobalInit = GV->getInitializer();
Eric Christopher37c8b862009-10-07 21:14:25 +0000454
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000455 // Handle the ConstantAggregateZero case, which is a degenerate case. The
456 // initializer is constant zero so the length of the string must be zero.
457 if (isa<ConstantAggregateZero>(GlobalInit))
458 return 1; // Len = 0 offset by 1.
Eric Christopher37c8b862009-10-07 21:14:25 +0000459
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000460 // Must be a Constant Array
461 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
Owen Anderson1d0be152009-08-13 21:58:54 +0000462 if (!Array ||
463 Array->getType()->getElementType() != Type::getInt8Ty(V->getContext()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000464 return false;
Eric Christopher37c8b862009-10-07 21:14:25 +0000465
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000466 // Get the number of elements in the array
467 uint64_t NumElts = Array->getType()->getNumElements();
Eric Christopher37c8b862009-10-07 21:14:25 +0000468
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000469 // Traverse the constant array from StartIdx (derived above) which is
470 // the place the GEP refers to in the array.
471 for (unsigned i = StartIdx; i != NumElts; ++i) {
472 Constant *Elt = Array->getOperand(i);
473 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
474 if (!CI) // This array isn't suitable, non-int initializer.
475 return 0;
476 if (CI->isZero())
477 return i-StartIdx+1; // We found end of string, success!
478 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000479
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000480 return 0; // The array isn't null terminated, conservatively return 'unknown'.
481}
482
483/// GetStringLength - If we can compute the length of the string pointed to by
484/// the specified pointer, return 'len+1'. If we can't, return 0.
485static uint64_t GetStringLength(Value *V) {
486 if (!isa<PointerType>(V->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000487
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000488 SmallPtrSet<PHINode*, 32> PHIs;
489 uint64_t Len = GetStringLengthH(V, PHIs);
490 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
491 // an empty string as a length.
492 return Len == ~0ULL ? 1 : Len;
493}
494
495/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +0000496/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000497static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
498 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
499 UI != E; ++UI) {
500 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
501 if (IC->isEquality())
502 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
503 if (C->isNullValue())
504 continue;
505 // Unknown instruction.
506 return false;
507 }
508 return true;
509}
510
511//===----------------------------------------------------------------------===//
Eric Christopher7b5e6172009-10-27 00:52:25 +0000512// Miscellaneous LibCall/Intrinsic Optimizations
513//===----------------------------------------------------------------------===//
514
515namespace {
516struct SizeOpt : public LibCallOptimization {
517 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
518 // TODO: We can do more with this, but delaying to here should be no change
519 // in behavior.
520 ConstantInt *Const = dyn_cast<ConstantInt>(CI->getOperand(2));
521
522 if (!Const) return 0;
523
524 if (Const->getZExtValue() < 2)
525 return Constant::getAllOnesValue(Const->getType());
526 else
527 return ConstantInt::get(Const->getType(), 0);
528 }
529};
530}
531
532//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000533// String and Memory LibCall Optimizations
534//===----------------------------------------------------------------------===//
535
536//===---------------------------------------===//
537// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000538namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000539struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000540 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000541 // Verify the "strcat" function prototype.
542 const FunctionType *FT = Callee->getFunctionType();
543 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000544 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000545 FT->getParamType(0) != FT->getReturnType() ||
546 FT->getParamType(1) != FT->getReturnType())
547 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000548
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000549 // Extract some information from the instruction
550 Value *Dst = CI->getOperand(1);
551 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000552
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000553 // See if we can get the length of the input string.
554 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000555 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000556 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000557
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000558 // Handle the simple, do-nothing case: strcat(x, "") -> x
559 if (Len == 0)
560 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000561
562 // These optimizations require TargetData.
563 if (!TD) return 0;
564
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000565 EmitStrLenMemCpy(Src, Dst, Len, B);
566 return Dst;
567 }
568
569 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000570 // We need to find the end of the destination string. That's where the
571 // memory is to be moved to. We just generate a call to strlen.
572 Value *DstLen = EmitStrLen(Dst, B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000573
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000574 // Now that we have the destination's length, we must index into the
575 // destination's pointer to get the actual memcpy destination (end of
576 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000577 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000578
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000579 // We have enough information to now generate the memcpy call to do the
580 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000581 EmitMemCpy(CpyDst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000582 ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000583 }
584};
585
586//===---------------------------------------===//
587// 'strncat' Optimizations
588
Chris Lattner3e8b6632009-09-02 06:11:42 +0000589struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000590 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
591 // Verify the "strncat" function prototype.
592 const FunctionType *FT = Callee->getFunctionType();
593 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000594 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000595 FT->getParamType(0) != FT->getReturnType() ||
596 FT->getParamType(1) != FT->getReturnType() ||
597 !isa<IntegerType>(FT->getParamType(2)))
598 return 0;
599
600 // Extract some information from the instruction
601 Value *Dst = CI->getOperand(1);
602 Value *Src = CI->getOperand(2);
603 uint64_t Len;
604
605 // We don't do anything if length is not constant
606 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
607 Len = LengthArg->getZExtValue();
608 else
609 return 0;
610
611 // See if we can get the length of the input string.
612 uint64_t SrcLen = GetStringLength(Src);
613 if (SrcLen == 0) return 0;
614 --SrcLen; // Unbias length.
615
616 // Handle the simple, do-nothing cases:
617 // strncat(x, "", c) -> x
618 // strncat(x, c, 0) -> x
619 if (SrcLen == 0 || Len == 0) return Dst;
620
Dan Gohmanf14d9192009-08-18 00:48:13 +0000621 // These optimizations require TargetData.
622 if (!TD) return 0;
623
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000624 // We don't optimize this case
625 if (Len < SrcLen) return 0;
626
627 // strncat(x, s, c) -> strcat(x, s)
628 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000629 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000630 return Dst;
631 }
632};
633
634//===---------------------------------------===//
635// 'strchr' Optimizations
636
Chris Lattner3e8b6632009-09-02 06:11:42 +0000637struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000638 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000639 // Verify the "strchr" function prototype.
640 const FunctionType *FT = Callee->getFunctionType();
641 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000642 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000643 FT->getParamType(0) != FT->getReturnType())
644 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000645
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000646 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000647
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000648 // If the second operand is non-constant, see if we can compute the length
649 // of the input string and turn this into memchr.
650 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
651 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000652 // These optimizations require TargetData.
653 if (!TD) return 0;
654
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000655 uint64_t Len = GetStringLength(SrcStr);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000656 if (Len == 0 ||
657 FT->getParamType(1) != Type::getInt32Ty(*Context)) // memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000658 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000659
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000660 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Owen Anderson1d0be152009-08-13 21:58:54 +0000661 ConstantInt::get(TD->getIntPtrType(*Context), Len), B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000662 }
663
664 // Otherwise, the character is a constant, see if the first argument is
665 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000666 std::string Str;
667 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000668 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000669
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000670 // strchr can find the nul character.
671 Str += '\0';
672 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000673
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000674 // Compute the offset.
675 uint64_t i = 0;
676 while (1) {
677 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000678 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000679 // Did we find our match?
680 if (Str[i] == CharValue)
681 break;
682 ++i;
683 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000684
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000685 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000686 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000687 return B.CreateGEP(SrcStr, Idx, "strchr");
688 }
689};
690
691//===---------------------------------------===//
692// 'strcmp' Optimizations
693
Chris Lattner3e8b6632009-09-02 06:11:42 +0000694struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000695 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000696 // Verify the "strcmp" function prototype.
697 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000698 if (FT->getNumParams() != 2 ||
699 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000700 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000701 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000702 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000703
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000704 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
705 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000706 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000707
Bill Wendling0582ae92009-03-13 04:39:26 +0000708 std::string Str1, Str2;
709 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
710 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000711
Bill Wendling0582ae92009-03-13 04:39:26 +0000712 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000713 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000714
Bill Wendling0582ae92009-03-13 04:39:26 +0000715 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000716 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000717
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000718 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000719 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000720 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000721 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000722
723 // strcmp(P, "x") -> memcmp(P, "x", 2)
724 uint64_t Len1 = GetStringLength(Str1P);
725 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000726 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000727 // These optimizations require TargetData.
728 if (!TD) return 0;
729
Nick Lewycky13a09e22008-12-21 00:19:21 +0000730 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000731 ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattner849832c2009-06-19 04:17:36 +0000732 std::min(Len1, Len2)), B);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000733 }
734
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000735 return 0;
736 }
737};
738
739//===---------------------------------------===//
740// 'strncmp' Optimizations
741
Chris Lattner3e8b6632009-09-02 06:11:42 +0000742struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000743 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000744 // Verify the "strncmp" function prototype.
745 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000746 if (FT->getNumParams() != 3 ||
747 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000748 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000749 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000750 !isa<IntegerType>(FT->getParamType(2)))
751 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000752
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000753 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
754 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000755 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000756
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000757 // Get the length argument if it is constant.
758 uint64_t Length;
759 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
760 Length = LengthArg->getZExtValue();
761 else
762 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000763
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000764 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000765 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000766
Bill Wendling0582ae92009-03-13 04:39:26 +0000767 std::string Str1, Str2;
768 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
769 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000770
Bill Wendling0582ae92009-03-13 04:39:26 +0000771 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000772 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000773
Bill Wendling0582ae92009-03-13 04:39:26 +0000774 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000775 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000776
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000777 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000778 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000779 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000780 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000781 return 0;
782 }
783};
784
785
786//===---------------------------------------===//
787// 'strcpy' Optimizations
788
Chris Lattner3e8b6632009-09-02 06:11:42 +0000789struct StrCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000790 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000791 // Verify the "strcpy" function prototype.
792 const FunctionType *FT = Callee->getFunctionType();
793 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
794 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000795 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000796 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000797
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000798 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
799 if (Dst == Src) // strcpy(x,x) -> x
800 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000801
Dan Gohmanf14d9192009-08-18 00:48:13 +0000802 // These optimizations require TargetData.
803 if (!TD) return 0;
804
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000805 // See if we can get the length of the input string.
806 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000807 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000808
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000809 // We have enough information to now generate the memcpy call to do the
810 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000811 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000812 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000813 return Dst;
814 }
815};
816
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000817//===---------------------------------------===//
818// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000819
Chris Lattner3e8b6632009-09-02 06:11:42 +0000820struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000821 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
822 const FunctionType *FT = Callee->getFunctionType();
823 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
824 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000825 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000826 !isa<IntegerType>(FT->getParamType(2)))
827 return 0;
828
829 Value *Dst = CI->getOperand(1);
830 Value *Src = CI->getOperand(2);
831 Value *LenOp = CI->getOperand(3);
832
833 // See if we can get the length of the input string.
834 uint64_t SrcLen = GetStringLength(Src);
835 if (SrcLen == 0) return 0;
836 --SrcLen;
837
838 if (SrcLen == 0) {
839 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000840 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
841 B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000842 return Dst;
843 }
844
845 uint64_t Len;
846 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
847 Len = LengthArg->getZExtValue();
848 else
849 return 0;
850
851 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
852
Dan Gohmanf14d9192009-08-18 00:48:13 +0000853 // These optimizations require TargetData.
854 if (!TD) return 0;
855
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000856 // Let strncpy handle the zero padding
857 if (Len > SrcLen+1) return 0;
858
859 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000860 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000861 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000862
863 return Dst;
864 }
865};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000866
867//===---------------------------------------===//
868// 'strlen' Optimizations
869
Chris Lattner3e8b6632009-09-02 06:11:42 +0000870struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000871 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000872 const FunctionType *FT = Callee->getFunctionType();
873 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000874 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000875 !isa<IntegerType>(FT->getReturnType()))
876 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000877
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000878 Value *Src = CI->getOperand(1);
879
880 // Constant folding: strlen("xyz") -> 3
881 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000882 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000883
884 // Handle strlen(p) != 0.
885 if (!IsOnlyUsedInZeroEqualityComparison(CI)) return 0;
886
887 // strlen(x) != 0 --> *x != 0
888 // strlen(x) == 0 --> *x == 0
889 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
890 }
891};
892
893//===---------------------------------------===//
Nick Lewycky4c498412009-02-13 15:31:46 +0000894// 'strto*' Optimizations
895
Chris Lattner3e8b6632009-09-02 06:11:42 +0000896struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000897 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
898 const FunctionType *FT = Callee->getFunctionType();
899 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
900 !isa<PointerType>(FT->getParamType(0)) ||
901 !isa<PointerType>(FT->getParamType(1)))
902 return 0;
903
904 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000905 if (isa<ConstantPointerNull>(EndPtr)) {
906 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000907 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000908 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000909
910 return 0;
911 }
912};
913
914
915//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000916// 'memcmp' Optimizations
917
Chris Lattner3e8b6632009-09-02 06:11:42 +0000918struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000919 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000920 const FunctionType *FT = Callee->getFunctionType();
921 if (FT->getNumParams() != 3 || !isa<PointerType>(FT->getParamType(0)) ||
922 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000923 FT->getReturnType() != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000924 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000925
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000926 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000927
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000928 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000929 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000930
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000931 // Make sure we have a constant length.
932 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000933 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000934 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000935
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000936 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000937 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000938
939 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
940 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
941 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +0000942 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000943 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000944
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000945 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS ^ *(short*)RHS) != 0
946 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS ^ *(int*)RHS) != 0
947 if ((Len == 2 || Len == 4) && IsOnlyUsedInZeroEqualityComparison(CI)) {
Owen Andersondebcb012009-07-29 22:17:13 +0000948 const Type *PTy = PointerType::getUnqual(Len == 2 ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000949 Type::getInt16Ty(*Context) : Type::getInt32Ty(*Context));
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000950 LHS = B.CreateBitCast(LHS, PTy, "tmp");
951 RHS = B.CreateBitCast(RHS, PTy, "tmp");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000952 LoadInst *LHSV = B.CreateLoad(LHS, "lhsv");
953 LoadInst *RHSV = B.CreateLoad(RHS, "rhsv");
954 LHSV->setAlignment(1); RHSV->setAlignment(1); // Unaligned loads.
955 return B.CreateZExt(B.CreateXor(LHSV, RHSV, "shortdiff"), CI->getType());
956 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000957
Benjamin Kramer992a6372009-11-05 17:44:22 +0000958 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
959 std::string LHSStr, RHSStr;
960 if (GetConstantStringInfo(LHS, LHSStr) &&
961 GetConstantStringInfo(RHS, RHSStr)) {
962 // Make sure we're not reading out-of-bounds memory.
963 if (Len > LHSStr.length() || Len > RHSStr.length())
964 return 0;
965 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
966 return ConstantInt::get(CI->getType(), Ret);
967 }
968
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000969 return 0;
970 }
971};
972
973//===---------------------------------------===//
974// 'memcpy' Optimizations
975
Chris Lattner3e8b6632009-09-02 06:11:42 +0000976struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000977 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000978 // These optimizations require TargetData.
979 if (!TD) return 0;
980
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000981 const FunctionType *FT = Callee->getFunctionType();
982 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
983 !isa<PointerType>(FT->getParamType(0)) ||
984 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000985 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000986 return 0;
987
988 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
989 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
990 return CI->getOperand(1);
991 }
992};
993
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000994//===---------------------------------------===//
995// 'memmove' Optimizations
996
Chris Lattner3e8b6632009-09-02 06:11:42 +0000997struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000998 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000999 // These optimizations require TargetData.
1000 if (!TD) return 0;
1001
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001002 const FunctionType *FT = Callee->getFunctionType();
1003 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1004 !isa<PointerType>(FT->getParamType(0)) ||
1005 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001006 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001007 return 0;
1008
1009 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
1010 Module *M = Caller->getParent();
1011 Intrinsic::ID IID = Intrinsic::memmove;
1012 const Type *Tys[1];
Owen Anderson1d0be152009-08-13 21:58:54 +00001013 Tys[0] = TD->getIntPtrType(*Context);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001014 Value *MemMove = Intrinsic::getDeclaration(M, IID, Tys, 1);
1015 Value *Dst = CastToCStr(CI->getOperand(1), B);
1016 Value *Src = CastToCStr(CI->getOperand(2), B);
1017 Value *Size = CI->getOperand(3);
Owen Anderson1d0be152009-08-13 21:58:54 +00001018 Value *Align = ConstantInt::get(Type::getInt32Ty(*Context), 1);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001019 B.CreateCall4(MemMove, Dst, Src, Size, Align);
1020 return CI->getOperand(1);
1021 }
1022};
1023
1024//===---------------------------------------===//
1025// 'memset' Optimizations
1026
Chris Lattner3e8b6632009-09-02 06:11:42 +00001027struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001028 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001029 // These optimizations require TargetData.
1030 if (!TD) return 0;
1031
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001032 const FunctionType *FT = Callee->getFunctionType();
1033 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1034 !isa<PointerType>(FT->getParamType(0)) ||
Eli Friedman62bb4132009-07-18 08:34:51 +00001035 !isa<IntegerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001036 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001037 return 0;
1038
1039 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +00001040 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1041 false);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001042 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001043 return CI->getOperand(1);
1044 }
1045};
1046
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001047//===----------------------------------------------------------------------===//
1048// Math Library Optimizations
1049//===----------------------------------------------------------------------===//
1050
1051//===---------------------------------------===//
1052// 'pow*' Optimizations
1053
Chris Lattner3e8b6632009-09-02 06:11:42 +00001054struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001055 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001056 const FunctionType *FT = Callee->getFunctionType();
1057 // Just make sure this has 2 arguments of the same FP type, which match the
1058 // result type.
1059 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1060 FT->getParamType(0) != FT->getParamType(1) ||
1061 !FT->getParamType(0)->isFloatingPoint())
1062 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001063
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001064 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
1065 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1066 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
1067 return Op1C;
1068 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +00001069 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001070 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001071
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001072 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1073 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001074
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001075 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001076 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001077
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001078 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +00001079 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1080 // This is faster than calling pow, and still handles negative zero
1081 // and negative infinite correctly.
1082 // TODO: In fast-math mode, this could be just sqrt(x).
1083 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +00001084 Value *Inf = ConstantFP::getInfinity(CI->getType());
1085 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +00001086 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1087 Callee->getAttributes());
1088 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1089 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +00001090 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
1091 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
1092 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001093 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001094
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001095 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1096 return Op1;
1097 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001098 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001099 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001100 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001101 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001102 return 0;
1103 }
1104};
1105
1106//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +00001107// 'exp2' Optimizations
1108
Chris Lattner3e8b6632009-09-02 06:11:42 +00001109struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001110 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +00001111 const FunctionType *FT = Callee->getFunctionType();
1112 // Just make sure this has 1 argument of FP type, which matches the
1113 // result type.
1114 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1115 !FT->getParamType(0)->isFloatingPoint())
1116 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001117
Chris Lattnere818f772008-05-02 18:43:35 +00001118 Value *Op = CI->getOperand(1);
1119 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1120 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1121 Value *LdExpArg = 0;
1122 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1123 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001124 LdExpArg = B.CreateSExt(OpC->getOperand(0),
1125 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001126 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1127 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001128 LdExpArg = B.CreateZExt(OpC->getOperand(0),
1129 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001130 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001131
Chris Lattnere818f772008-05-02 18:43:35 +00001132 if (LdExpArg) {
1133 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001134 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001135 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001136 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001137 Name = "ldexp";
1138 else
1139 Name = "ldexpl";
1140
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001141 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001142 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001143 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +00001144
1145 Module *M = Caller->getParent();
1146 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +00001147 Op->getType(),
1148 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001149 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1150 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1151 CI->setCallingConv(F->getCallingConv());
1152
1153 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +00001154 }
1155 return 0;
1156 }
1157};
Chris Lattnere818f772008-05-02 18:43:35 +00001158
1159//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001160// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1161
Chris Lattner3e8b6632009-09-02 06:11:42 +00001162struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001163 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001164 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001165 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1166 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001167 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001168
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001169 // If this is something like 'floor((double)floatval)', convert to floorf.
1170 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001171 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001172 return 0;
1173
1174 // floor((double)floatval) -> (double)floorf(floatval)
1175 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +00001176 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
1177 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +00001178 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001179 }
1180};
1181
1182//===----------------------------------------------------------------------===//
1183// Integer Optimizations
1184//===----------------------------------------------------------------------===//
1185
1186//===---------------------------------------===//
1187// 'ffs*' Optimizations
1188
Chris Lattner3e8b6632009-09-02 06:11:42 +00001189struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001190 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001191 const FunctionType *FT = Callee->getFunctionType();
1192 // Just make sure this has 2 arguments of the same FP type, which match the
1193 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +00001194 if (FT->getNumParams() != 1 ||
1195 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001196 !isa<IntegerType>(FT->getParamType(0)))
1197 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001198
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001199 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001200
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001201 // Constant fold.
1202 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1203 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +00001204 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +00001205 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001206 CI->getValue().countTrailingZeros()+1);
1207 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001208
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001209 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1210 const Type *ArgType = Op->getType();
1211 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1212 Intrinsic::cttz, &ArgType, 1);
1213 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +00001214 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +00001215 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001216
Owen Andersona7235ea2009-07-31 20:28:14 +00001217 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001218 return B.CreateSelect(Cond, V,
1219 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001220 }
1221};
1222
1223//===---------------------------------------===//
1224// 'isdigit' Optimizations
1225
Chris Lattner3e8b6632009-09-02 06:11:42 +00001226struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001227 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001228 const FunctionType *FT = Callee->getFunctionType();
1229 // We require integer(i32)
1230 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001231 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001232 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001233
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001234 // isdigit(c) -> (c-'0') <u 10
1235 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001236 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001237 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001238 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001239 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001240 return B.CreateZExt(Op, CI->getType());
1241 }
1242};
1243
1244//===---------------------------------------===//
1245// 'isascii' Optimizations
1246
Chris Lattner3e8b6632009-09-02 06:11:42 +00001247struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001248 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001249 const FunctionType *FT = Callee->getFunctionType();
1250 // We require integer(i32)
1251 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001252 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001253 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001254
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001255 // isascii(c) -> c <u 128
1256 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001257 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001258 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001259 return B.CreateZExt(Op, CI->getType());
1260 }
1261};
Eric Christopher37c8b862009-10-07 21:14:25 +00001262
Chris Lattner313f0e62008-06-09 08:26:51 +00001263//===---------------------------------------===//
1264// 'abs', 'labs', 'llabs' Optimizations
1265
Chris Lattner3e8b6632009-09-02 06:11:42 +00001266struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001267 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001268 const FunctionType *FT = Callee->getFunctionType();
1269 // We require integer(integer) where the types agree.
1270 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
1271 FT->getParamType(0) != FT->getReturnType())
1272 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001273
Chris Lattner313f0e62008-06-09 08:26:51 +00001274 // abs(x) -> x >s -1 ? x : -x
1275 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001276 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001277 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001278 "ispos");
1279 Value *Neg = B.CreateNeg(Op, "neg");
1280 return B.CreateSelect(Pos, Op, Neg);
1281 }
1282};
Eric Christopher37c8b862009-10-07 21:14:25 +00001283
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001284
1285//===---------------------------------------===//
1286// 'toascii' Optimizations
1287
Chris Lattner3e8b6632009-09-02 06:11:42 +00001288struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001289 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001290 const FunctionType *FT = Callee->getFunctionType();
1291 // We require i32(i32)
1292 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001293 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001294 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001295
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001296 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001297 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +00001298 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001299 }
1300};
1301
1302//===----------------------------------------------------------------------===//
1303// Formatting and IO Optimizations
1304//===----------------------------------------------------------------------===//
1305
1306//===---------------------------------------===//
1307// 'printf' Optimizations
1308
Chris Lattner3e8b6632009-09-02 06:11:42 +00001309struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001310 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001311 // Require one fixed pointer argument and an integer/void result.
1312 const FunctionType *FT = Callee->getFunctionType();
1313 if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
1314 !(isa<IntegerType>(FT->getReturnType()) ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001315 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001316 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001317
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001318 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001319 std::string FormatStr;
1320 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1321 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001322
1323 // Empty format string -> noop.
1324 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001325 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001326 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001327
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001328 // printf("x") -> putchar('x'), even for '%'.
1329 if (FormatStr.size() == 1) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001330 EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context), FormatStr[0]), B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001331 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001332 ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001333 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001334
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001335 // printf("foo\n") --> puts("foo")
1336 if (FormatStr[FormatStr.size()-1] == '\n' &&
1337 FormatStr.find('%') == std::string::npos) { // no format characters.
1338 // Create a string literal with no \n on it. We expect the constant merge
1339 // pass to be run after this pass, to merge duplicate strings.
1340 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001341 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001342 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1343 GlobalVariable::InternalLinkage, C, "str");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001344 EmitPutS(C, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001345 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001346 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001347 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001348
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001349 // Optimize specific format strings.
1350 // printf("%c", chr) --> putchar(*(i8*)dst)
1351 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
1352 isa<IntegerType>(CI->getOperand(2)->getType())) {
1353 EmitPutChar(CI->getOperand(2), B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001354 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001355 ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001356 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001357
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001358 // printf("%s\n", str) --> puts(str)
1359 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
1360 isa<PointerType>(CI->getOperand(2)->getType()) &&
1361 CI->use_empty()) {
1362 EmitPutS(CI->getOperand(2), B);
1363 return CI;
1364 }
1365 return 0;
1366 }
1367};
1368
1369//===---------------------------------------===//
1370// 'sprintf' Optimizations
1371
Chris Lattner3e8b6632009-09-02 06:11:42 +00001372struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001373 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001374 // Require two fixed pointer arguments and an integer result.
1375 const FunctionType *FT = Callee->getFunctionType();
1376 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1377 !isa<PointerType>(FT->getParamType(1)) ||
1378 !isa<IntegerType>(FT->getReturnType()))
1379 return 0;
1380
1381 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001382 std::string FormatStr;
1383 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1384 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001385
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001386 // If we just have a format string (nothing else crazy) transform it.
1387 if (CI->getNumOperands() == 3) {
1388 // Make sure there's no % in the constant array. We could try to handle
1389 // %% -> % in the future if we cared.
1390 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1391 if (FormatStr[i] == '%')
1392 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001393
1394 // These optimizations require TargetData.
1395 if (!TD) return 0;
1396
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001397 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1398 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Owen Anderson1d0be152009-08-13 21:58:54 +00001399 ConstantInt::get(TD->getIntPtrType(*Context), FormatStr.size()+1),1,B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001400 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001401 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001402
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001403 // The remaining optimizations require the format string to be "%s" or "%c"
1404 // and have an extra operand.
1405 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1406 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001407
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001408 // Decode the second character of the format string.
1409 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001410 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001411 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001412 Value *V = B.CreateTrunc(CI->getOperand(3),
1413 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001414 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1415 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001416 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1417 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001418 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001419
Owen Andersoneed707b2009-07-24 23:12:02 +00001420 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001421 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001422
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001423 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001424 // These optimizations require TargetData.
1425 if (!TD) return 0;
1426
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001427 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1428 if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
1429
1430 Value *Len = EmitStrLen(CI->getOperand(3), B);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001431 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001432 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001433 "leninc");
1434 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001435
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001436 // The sprintf result is the unincremented number of bytes in the string.
1437 return B.CreateIntCast(Len, CI->getType(), false);
1438 }
1439 return 0;
1440 }
1441};
1442
1443//===---------------------------------------===//
1444// 'fwrite' Optimizations
1445
Chris Lattner3e8b6632009-09-02 06:11:42 +00001446struct FWriteOpt : 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 a pointer, an integer, an integer, a pointer, returning integer.
1449 const FunctionType *FT = Callee->getFunctionType();
1450 if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
1451 !isa<IntegerType>(FT->getParamType(1)) ||
1452 !isa<IntegerType>(FT->getParamType(2)) ||
1453 !isa<PointerType>(FT->getParamType(3)) ||
1454 !isa<IntegerType>(FT->getReturnType()))
1455 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001456
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001457 // Get the element size and count.
1458 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1459 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1460 if (!SizeC || !CountC) return 0;
1461 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001462
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001463 // If this is writing zero records, remove the call (it's a noop).
1464 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001465 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001466
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001467 // If this is writing one byte, turn it into fputc.
1468 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1469 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1470 EmitFPutC(Char, CI->getOperand(4), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001471 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001472 }
1473
1474 return 0;
1475 }
1476};
1477
1478//===---------------------------------------===//
1479// 'fputs' Optimizations
1480
Chris Lattner3e8b6632009-09-02 06:11:42 +00001481struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001482 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001483 // These optimizations require TargetData.
1484 if (!TD) return 0;
1485
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001486 // Require two pointers. Also, we can't optimize if return value is used.
1487 const FunctionType *FT = Callee->getFunctionType();
1488 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1489 !isa<PointerType>(FT->getParamType(1)) ||
1490 !CI->use_empty())
1491 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001492
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001493 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1494 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001495 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001496 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001497 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001498 CI->getOperand(2), B);
1499 return CI; // Known to have no uses (see above).
1500 }
1501};
1502
1503//===---------------------------------------===//
1504// 'fprintf' Optimizations
1505
Chris Lattner3e8b6632009-09-02 06:11:42 +00001506struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001507 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001508 // Require two fixed paramters as pointers and integer result.
1509 const FunctionType *FT = Callee->getFunctionType();
1510 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1511 !isa<PointerType>(FT->getParamType(1)) ||
1512 !isa<IntegerType>(FT->getReturnType()))
1513 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001514
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001515 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001516 std::string FormatStr;
1517 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1518 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001519
1520 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1521 if (CI->getNumOperands() == 3) {
1522 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1523 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001524 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001525
1526 // These optimizations require TargetData.
1527 if (!TD) return 0;
1528
Owen Anderson1d0be152009-08-13 21:58:54 +00001529 EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001530 FormatStr.size()),
1531 CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001532 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001533 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001534
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001535 // The remaining optimizations require the format string to be "%s" or "%c"
1536 // and have an extra operand.
1537 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1538 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001539
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001540 // Decode the second character of the format string.
1541 if (FormatStr[1] == 'c') {
1542 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1543 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1544 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001545 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001546 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001547
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001548 if (FormatStr[1] == 's') {
1549 // fprintf(F, "%s", str) -> fputs(str, F)
1550 if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
1551 return 0;
1552 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1553 return CI;
1554 }
1555 return 0;
1556 }
1557};
1558
Bill Wendlingac178222008-05-05 21:37:59 +00001559} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001560
1561//===----------------------------------------------------------------------===//
1562// SimplifyLibCalls Pass Implementation
1563//===----------------------------------------------------------------------===//
1564
1565namespace {
1566 /// This pass optimizes well known library functions from libc and libm.
1567 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001568 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001569 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001570 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001571 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1572 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
1573 StrToOpt StrTo; MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove;
1574 MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001575 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001576 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001577 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001578 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1579 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001580 // Formatting and IO Optimizations
1581 SPrintFOpt SPrintF; PrintFOpt PrintF;
1582 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher7b5e6172009-10-27 00:52:25 +00001583 SizeOpt ObjectSize;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001584
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001585 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001586 public:
1587 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001588 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001589
1590 void InitOptimizations();
1591 bool runOnFunction(Function &F);
1592
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001593 void setDoesNotAccessMemory(Function &F);
1594 void setOnlyReadsMemory(Function &F);
1595 void setDoesNotThrow(Function &F);
1596 void setDoesNotCapture(Function &F, unsigned n);
1597 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001598 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001599
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001600 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001601 }
1602 };
1603 char SimplifyLibCalls::ID = 0;
1604} // end anonymous namespace.
1605
1606static RegisterPass<SimplifyLibCalls>
1607X("simplify-libcalls", "Simplify well-known library calls");
1608
1609// Public interface to the Simplify LibCalls pass.
1610FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001611 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001612}
1613
1614/// Optimizations - Populate the Optimizations map with all the optimizations
1615/// we know.
1616void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001617 // String and Memory LibCall Optimizations
1618 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001619 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001620 Optimizations["strchr"] = &StrChr;
1621 Optimizations["strcmp"] = &StrCmp;
1622 Optimizations["strncmp"] = &StrNCmp;
1623 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001624 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001625 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001626 Optimizations["strtol"] = &StrTo;
1627 Optimizations["strtod"] = &StrTo;
1628 Optimizations["strtof"] = &StrTo;
1629 Optimizations["strtoul"] = &StrTo;
1630 Optimizations["strtoll"] = &StrTo;
1631 Optimizations["strtold"] = &StrTo;
1632 Optimizations["strtoull"] = &StrTo;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001633 Optimizations["memcmp"] = &MemCmp;
1634 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001635 Optimizations["memmove"] = &MemMove;
1636 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001637
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001638 // Math Library Optimizations
1639 Optimizations["powf"] = &Pow;
1640 Optimizations["pow"] = &Pow;
1641 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001642 Optimizations["llvm.pow.f32"] = &Pow;
1643 Optimizations["llvm.pow.f64"] = &Pow;
1644 Optimizations["llvm.pow.f80"] = &Pow;
1645 Optimizations["llvm.pow.f128"] = &Pow;
1646 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001647 Optimizations["exp2l"] = &Exp2;
1648 Optimizations["exp2"] = &Exp2;
1649 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001650 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1651 Optimizations["llvm.exp2.f128"] = &Exp2;
1652 Optimizations["llvm.exp2.f80"] = &Exp2;
1653 Optimizations["llvm.exp2.f64"] = &Exp2;
1654 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001655
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001656#ifdef HAVE_FLOORF
1657 Optimizations["floor"] = &UnaryDoubleFP;
1658#endif
1659#ifdef HAVE_CEILF
1660 Optimizations["ceil"] = &UnaryDoubleFP;
1661#endif
1662#ifdef HAVE_ROUNDF
1663 Optimizations["round"] = &UnaryDoubleFP;
1664#endif
1665#ifdef HAVE_RINTF
1666 Optimizations["rint"] = &UnaryDoubleFP;
1667#endif
1668#ifdef HAVE_NEARBYINTF
1669 Optimizations["nearbyint"] = &UnaryDoubleFP;
1670#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001671
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001672 // Integer Optimizations
1673 Optimizations["ffs"] = &FFS;
1674 Optimizations["ffsl"] = &FFS;
1675 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001676 Optimizations["abs"] = &Abs;
1677 Optimizations["labs"] = &Abs;
1678 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001679 Optimizations["isdigit"] = &IsDigit;
1680 Optimizations["isascii"] = &IsAscii;
1681 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001682
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001683 // Formatting and IO Optimizations
1684 Optimizations["sprintf"] = &SPrintF;
1685 Optimizations["printf"] = &PrintF;
1686 Optimizations["fwrite"] = &FWrite;
1687 Optimizations["fputs"] = &FPuts;
1688 Optimizations["fprintf"] = &FPrintF;
Eric Christopher7b5e6172009-10-27 00:52:25 +00001689
1690 // Miscellaneous
1691 Optimizations["llvm.objectsize"] = &ObjectSize;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001692}
1693
1694
1695/// runOnFunction - Top level algorithm.
1696///
1697bool SimplifyLibCalls::runOnFunction(Function &F) {
1698 if (Optimizations.empty())
1699 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001700
Dan Gohmanf14d9192009-08-18 00:48:13 +00001701 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001702
Owen Andersone922c022009-07-22 00:24:57 +00001703 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001704
1705 bool Changed = false;
1706 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1707 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1708 // Ignore non-calls.
1709 CallInst *CI = dyn_cast<CallInst>(I++);
1710 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001711
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001712 // Ignore indirect calls and calls to non-external functions.
1713 Function *Callee = CI->getCalledFunction();
1714 if (Callee == 0 || !Callee->isDeclaration() ||
1715 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1716 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001717
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001718 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001719 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1720 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001721
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001722 // Set the builder to the instruction after the call.
1723 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001724
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001725 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001726 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001727 if (Result == 0) continue;
1728
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001729 DEBUG(errs() << "SimplifyLibCalls simplified: " << *CI;
1730 errs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001731
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001732 // Something changed!
1733 Changed = true;
1734 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001735
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001736 // Inspect the instruction after the call (which was potentially just
1737 // added) next.
1738 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001739
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001740 if (CI != Result && !CI->use_empty()) {
1741 CI->replaceAllUsesWith(Result);
1742 if (!Result->hasName())
1743 Result->takeName(CI);
1744 }
1745 CI->eraseFromParent();
1746 }
1747 }
1748 return Changed;
1749}
1750
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001751// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001752
1753void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1754 if (!F.doesNotAccessMemory()) {
1755 F.setDoesNotAccessMemory();
1756 ++NumAnnotated;
1757 Modified = true;
1758 }
1759}
1760void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1761 if (!F.onlyReadsMemory()) {
1762 F.setOnlyReadsMemory();
1763 ++NumAnnotated;
1764 Modified = true;
1765 }
1766}
1767void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1768 if (!F.doesNotThrow()) {
1769 F.setDoesNotThrow();
1770 ++NumAnnotated;
1771 Modified = true;
1772 }
1773}
1774void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1775 if (!F.doesNotCapture(n)) {
1776 F.setDoesNotCapture(n);
1777 ++NumAnnotated;
1778 Modified = true;
1779 }
1780}
1781void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1782 if (!F.doesNotAlias(n)) {
1783 F.setDoesNotAlias(n);
1784 ++NumAnnotated;
1785 Modified = true;
1786 }
1787}
1788
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001789/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001790///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001791bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001792 Modified = false;
1793 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1794 Function &F = *I;
1795 if (!F.isDeclaration())
1796 continue;
1797
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001798 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001799 continue;
1800
1801 const FunctionType *FTy = F.getFunctionType();
1802
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001803 StringRef Name = F.getName();
1804 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001805 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001806 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001807 if (FTy->getNumParams() != 1 ||
1808 !isa<PointerType>(FTy->getParamType(0)))
1809 continue;
1810 setOnlyReadsMemory(F);
1811 setDoesNotThrow(F);
1812 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001813 } else if (Name == "strcpy" ||
1814 Name == "stpcpy" ||
1815 Name == "strcat" ||
1816 Name == "strtol" ||
1817 Name == "strtod" ||
1818 Name == "strtof" ||
1819 Name == "strtoul" ||
1820 Name == "strtoll" ||
1821 Name == "strtold" ||
1822 Name == "strncat" ||
1823 Name == "strncpy" ||
1824 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001825 if (FTy->getNumParams() < 2 ||
1826 !isa<PointerType>(FTy->getParamType(1)))
1827 continue;
1828 setDoesNotThrow(F);
1829 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001830 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001831 if (FTy->getNumParams() != 3 ||
1832 !isa<PointerType>(FTy->getParamType(0)) ||
1833 !isa<PointerType>(FTy->getParamType(1)))
1834 continue;
1835 setDoesNotThrow(F);
1836 setDoesNotCapture(F, 1);
1837 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001838 } else if (Name == "strcmp" ||
1839 Name == "strspn" ||
1840 Name == "strncmp" ||
1841 Name ==" strcspn" ||
1842 Name == "strcoll" ||
1843 Name == "strcasecmp" ||
1844 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001845 if (FTy->getNumParams() < 2 ||
1846 !isa<PointerType>(FTy->getParamType(0)) ||
1847 !isa<PointerType>(FTy->getParamType(1)))
1848 continue;
1849 setOnlyReadsMemory(F);
1850 setDoesNotThrow(F);
1851 setDoesNotCapture(F, 1);
1852 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001853 } else if (Name == "strstr" ||
1854 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001855 if (FTy->getNumParams() != 2 ||
1856 !isa<PointerType>(FTy->getParamType(1)))
1857 continue;
1858 setOnlyReadsMemory(F);
1859 setDoesNotThrow(F);
1860 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001861 } else if (Name == "strtok" ||
1862 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001863 if (FTy->getNumParams() < 2 ||
1864 !isa<PointerType>(FTy->getParamType(1)))
1865 continue;
1866 setDoesNotThrow(F);
1867 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001868 } else if (Name == "scanf" ||
1869 Name == "setbuf" ||
1870 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001871 if (FTy->getNumParams() < 1 ||
1872 !isa<PointerType>(FTy->getParamType(0)))
1873 continue;
1874 setDoesNotThrow(F);
1875 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001876 } else if (Name == "strdup" ||
1877 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001878 if (FTy->getNumParams() < 1 ||
1879 !isa<PointerType>(FTy->getReturnType()) ||
1880 !isa<PointerType>(FTy->getParamType(0)))
1881 continue;
1882 setDoesNotThrow(F);
1883 setDoesNotAlias(F, 0);
1884 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001885 } else if (Name == "stat" ||
1886 Name == "sscanf" ||
1887 Name == "sprintf" ||
1888 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001889 if (FTy->getNumParams() < 2 ||
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001890 !isa<PointerType>(FTy->getParamType(0)) ||
1891 !isa<PointerType>(FTy->getParamType(1)))
1892 continue;
1893 setDoesNotThrow(F);
1894 setDoesNotCapture(F, 1);
1895 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001896 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001897 if (FTy->getNumParams() != 3 ||
1898 !isa<PointerType>(FTy->getParamType(0)) ||
1899 !isa<PointerType>(FTy->getParamType(2)))
1900 continue;
1901 setDoesNotThrow(F);
1902 setDoesNotCapture(F, 1);
1903 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001904 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001905 if (FTy->getNumParams() != 3 ||
1906 !isa<PointerType>(FTy->getParamType(1)) ||
1907 !isa<PointerType>(FTy->getParamType(2)))
1908 continue;
1909 setDoesNotThrow(F);
1910 setDoesNotCapture(F, 2);
1911 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001912 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001913 if (FTy->getNumParams() != 1 ||
1914 !isa<PointerType>(FTy->getParamType(0)))
1915 continue;
1916 // May throw; "system" is a valid pthread cancellation point.
1917 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001918 }
1919 break;
1920 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001921 if (Name == "malloc") {
1922 if (FTy->getNumParams() != 1 ||
1923 !isa<PointerType>(FTy->getReturnType()))
1924 continue;
1925 setDoesNotThrow(F);
1926 setDoesNotAlias(F, 0);
1927 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001928 if (FTy->getNumParams() != 3 ||
1929 !isa<PointerType>(FTy->getParamType(0)) ||
1930 !isa<PointerType>(FTy->getParamType(1)))
1931 continue;
1932 setOnlyReadsMemory(F);
1933 setDoesNotThrow(F);
1934 setDoesNotCapture(F, 1);
1935 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001936 } else if (Name == "memchr" ||
1937 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001938 if (FTy->getNumParams() != 3)
1939 continue;
1940 setOnlyReadsMemory(F);
1941 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001942 } else if (Name == "modf" ||
1943 Name == "modff" ||
1944 Name == "modfl" ||
1945 Name == "memcpy" ||
1946 Name == "memccpy" ||
1947 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001948 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001949 !isa<PointerType>(FTy->getParamType(1)))
1950 continue;
1951 setDoesNotThrow(F);
1952 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001953 } else if (Name == "memalign") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001954 if (!isa<PointerType>(FTy->getReturnType()))
1955 continue;
1956 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001957 } else if (Name == "mkdir" ||
1958 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001959 if (FTy->getNumParams() == 0 ||
1960 !isa<PointerType>(FTy->getParamType(0)))
1961 continue;
1962 setDoesNotThrow(F);
1963 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001964 }
1965 break;
1966 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001967 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001968 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001969 !isa<PointerType>(FTy->getParamType(0)) ||
1970 !isa<PointerType>(FTy->getReturnType()))
1971 continue;
1972 setDoesNotThrow(F);
1973 setDoesNotAlias(F, 0);
1974 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001975 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001976 if (FTy->getNumParams() != 3 ||
1977 !isa<PointerType>(FTy->getParamType(1)))
1978 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001979 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001980 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001981 } else if (Name == "rmdir" ||
1982 Name == "rewind" ||
1983 Name == "remove" ||
1984 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001985 if (FTy->getNumParams() < 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001986 !isa<PointerType>(FTy->getParamType(0)))
1987 continue;
1988 setDoesNotThrow(F);
1989 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001990 } else if (Name == "rename" ||
1991 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001992 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001993 !isa<PointerType>(FTy->getParamType(0)) ||
1994 !isa<PointerType>(FTy->getParamType(1)))
1995 continue;
1996 setDoesNotThrow(F);
1997 setDoesNotCapture(F, 1);
1998 setDoesNotCapture(F, 2);
1999 }
2000 break;
2001 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002002 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002003 if (FTy->getNumParams() != 3 ||
2004 !isa<PointerType>(FTy->getParamType(1)))
2005 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002006 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002007 setDoesNotCapture(F, 2);
2008 }
2009 break;
2010 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002011 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002012 if (FTy->getNumParams() != 3 ||
2013 !isa<PointerType>(FTy->getParamType(0)) ||
2014 !isa<PointerType>(FTy->getParamType(1)))
2015 continue;
2016 setDoesNotThrow(F);
2017 setDoesNotCapture(F, 1);
2018 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002019 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002020 if (FTy->getNumParams() != 3 ||
2021 !isa<PointerType>(FTy->getParamType(0)) ||
2022 !isa<PointerType>(FTy->getParamType(1)))
2023 continue;
2024 setDoesNotThrow(F);
2025 setOnlyReadsMemory(F);
2026 setDoesNotCapture(F, 1);
2027 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002028 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002029 if (FTy->getNumParams() != 2 ||
2030 !isa<PointerType>(FTy->getParamType(0)))
2031 continue;
2032 setDoesNotThrow(F);
2033 setDoesNotCapture(F, 1);
2034 }
2035 break;
2036 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002037 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002038 if (FTy->getNumParams() != 2 ||
2039 !isa<PointerType>(FTy->getReturnType()))
2040 continue;
2041 setDoesNotThrow(F);
2042 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002043 } else if (Name == "chmod" ||
2044 Name == "chown" ||
2045 Name == "ctermid" ||
2046 Name == "clearerr" ||
2047 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002048 if (FTy->getNumParams() == 0 ||
2049 !isa<PointerType>(FTy->getParamType(0)))
2050 continue;
2051 setDoesNotThrow(F);
2052 setDoesNotCapture(F, 1);
2053 }
2054 break;
2055 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002056 if (Name == "atoi" ||
2057 Name == "atol" ||
2058 Name == "atof" ||
2059 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002060 if (FTy->getNumParams() != 1 ||
2061 !isa<PointerType>(FTy->getParamType(0)))
2062 continue;
2063 setDoesNotThrow(F);
2064 setOnlyReadsMemory(F);
2065 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002066 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002067 if (FTy->getNumParams() != 2 ||
2068 !isa<PointerType>(FTy->getParamType(0)))
2069 continue;
2070 setDoesNotThrow(F);
2071 setDoesNotCapture(F, 1);
2072 }
2073 break;
2074 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002075 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002076 if (FTy->getNumParams() != 2 ||
2077 !isa<PointerType>(FTy->getReturnType()) ||
2078 !isa<PointerType>(FTy->getParamType(0)) ||
2079 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002080 continue;
2081 setDoesNotThrow(F);
2082 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002083 setDoesNotCapture(F, 1);
2084 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002085 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002086 if (FTy->getNumParams() != 2 ||
2087 !isa<PointerType>(FTy->getReturnType()) ||
2088 !isa<PointerType>(FTy->getParamType(1)))
2089 continue;
2090 setDoesNotThrow(F);
2091 setDoesNotAlias(F, 0);
2092 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002093 } else if (Name == "feof" ||
2094 Name == "free" ||
2095 Name == "fseek" ||
2096 Name == "ftell" ||
2097 Name == "fgetc" ||
2098 Name == "fseeko" ||
2099 Name == "ftello" ||
2100 Name == "fileno" ||
2101 Name == "fflush" ||
2102 Name == "fclose" ||
2103 Name == "fsetpos" ||
2104 Name == "flockfile" ||
2105 Name == "funlockfile" ||
2106 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002107 if (FTy->getNumParams() == 0 ||
2108 !isa<PointerType>(FTy->getParamType(0)))
2109 continue;
2110 setDoesNotThrow(F);
2111 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002112 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002113 if (FTy->getNumParams() != 1 ||
2114 !isa<PointerType>(FTy->getParamType(0)))
2115 continue;
2116 setDoesNotThrow(F);
2117 setDoesNotCapture(F, 1);
2118 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002119 } else if (Name == "fputc" ||
2120 Name == "fstat" ||
2121 Name == "frexp" ||
2122 Name == "frexpf" ||
2123 Name == "frexpl" ||
2124 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002125 if (FTy->getNumParams() != 2 ||
2126 !isa<PointerType>(FTy->getParamType(1)))
2127 continue;
2128 setDoesNotThrow(F);
2129 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002130 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002131 if (FTy->getNumParams() != 3 ||
2132 !isa<PointerType>(FTy->getParamType(0)) ||
2133 !isa<PointerType>(FTy->getParamType(2)))
2134 continue;
2135 setDoesNotThrow(F);
2136 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002137 } else if (Name == "fread" ||
2138 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002139 if (FTy->getNumParams() != 4 ||
2140 !isa<PointerType>(FTy->getParamType(0)) ||
2141 !isa<PointerType>(FTy->getParamType(3)))
2142 continue;
2143 setDoesNotThrow(F);
2144 setDoesNotCapture(F, 1);
2145 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002146 } else if (Name == "fputs" ||
2147 Name == "fscanf" ||
2148 Name == "fprintf" ||
2149 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002150 if (FTy->getNumParams() < 2 ||
2151 !isa<PointerType>(FTy->getParamType(0)) ||
2152 !isa<PointerType>(FTy->getParamType(1)))
2153 continue;
2154 setDoesNotThrow(F);
2155 setDoesNotCapture(F, 1);
2156 setDoesNotCapture(F, 2);
2157 }
2158 break;
2159 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002160 if (Name == "getc" ||
2161 Name == "getlogin_r" ||
2162 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002163 if (FTy->getNumParams() == 0 ||
2164 !isa<PointerType>(FTy->getParamType(0)))
2165 continue;
2166 setDoesNotThrow(F);
2167 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002168 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002169 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002170 !isa<PointerType>(FTy->getParamType(0)))
2171 continue;
2172 setDoesNotThrow(F);
2173 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002174 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002175 } else if (Name == "gets" ||
2176 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002177 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002178 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002179 if (FTy->getNumParams() != 2 ||
2180 !isa<PointerType>(FTy->getParamType(1)))
2181 continue;
2182 setDoesNotThrow(F);
2183 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002184 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002185 if (FTy->getNumParams() != 1 ||
2186 !isa<PointerType>(FTy->getParamType(0)))
2187 continue;
2188 setDoesNotThrow(F);
2189 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002190 }
2191 break;
2192 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002193 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002194 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002195 !isa<PointerType>(FTy->getParamType(1)))
2196 continue;
2197 setDoesNotThrow(F);
2198 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002199 } else if (Name == "uname" ||
2200 Name == "unlink" ||
2201 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002202 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002203 !isa<PointerType>(FTy->getParamType(0)))
2204 continue;
2205 setDoesNotThrow(F);
2206 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002207 } else if (Name == "utime" ||
2208 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002209 if (FTy->getNumParams() != 2 ||
2210 !isa<PointerType>(FTy->getParamType(0)) ||
2211 !isa<PointerType>(FTy->getParamType(1)))
2212 continue;
2213 setDoesNotThrow(F);
2214 setDoesNotCapture(F, 1);
2215 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002216 }
2217 break;
2218 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002219 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002220 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002221 !isa<PointerType>(FTy->getParamType(1)))
2222 continue;
2223 setDoesNotThrow(F);
2224 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002225 } else if (Name == "puts" ||
2226 Name == "printf" ||
2227 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002228 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002229 !isa<PointerType>(FTy->getParamType(0)))
2230 continue;
2231 setDoesNotThrow(F);
2232 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002233 } else if (Name == "pread" ||
2234 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002235 if (FTy->getNumParams() != 4 ||
2236 !isa<PointerType>(FTy->getParamType(1)))
2237 continue;
2238 // May throw; these are valid pthread cancellation points.
2239 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002240 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002241 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002242 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002243 if (FTy->getNumParams() != 2 ||
2244 !isa<PointerType>(FTy->getReturnType()) ||
2245 !isa<PointerType>(FTy->getParamType(0)) ||
2246 !isa<PointerType>(FTy->getParamType(1)))
2247 continue;
2248 setDoesNotThrow(F);
2249 setDoesNotAlias(F, 0);
2250 setDoesNotCapture(F, 1);
2251 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002252 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002253 if (FTy->getNumParams() != 1 ||
2254 !isa<PointerType>(FTy->getParamType(0)))
2255 continue;
2256 setDoesNotThrow(F);
2257 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002258 }
2259 break;
2260 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002261 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002262 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002263 !isa<PointerType>(FTy->getParamType(1)))
2264 continue;
2265 setDoesNotThrow(F);
2266 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002267 } else if (Name == "vsscanf" ||
2268 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002269 if (FTy->getNumParams() != 3 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002270 !isa<PointerType>(FTy->getParamType(1)) ||
2271 !isa<PointerType>(FTy->getParamType(2)))
2272 continue;
2273 setDoesNotThrow(F);
2274 setDoesNotCapture(F, 1);
2275 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002276 } else if (Name == "valloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002277 if (!isa<PointerType>(FTy->getReturnType()))
2278 continue;
2279 setDoesNotThrow(F);
2280 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002281 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002282 if (FTy->getNumParams() != 2 ||
2283 !isa<PointerType>(FTy->getParamType(0)))
2284 continue;
2285 setDoesNotThrow(F);
2286 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002287 } else if (Name == "vfprintf" ||
2288 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002289 if (FTy->getNumParams() != 3 ||
2290 !isa<PointerType>(FTy->getParamType(0)) ||
2291 !isa<PointerType>(FTy->getParamType(1)))
2292 continue;
2293 setDoesNotThrow(F);
2294 setDoesNotCapture(F, 1);
2295 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002296 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002297 if (FTy->getNumParams() != 4 ||
2298 !isa<PointerType>(FTy->getParamType(0)) ||
2299 !isa<PointerType>(FTy->getParamType(2)))
2300 continue;
2301 setDoesNotThrow(F);
2302 setDoesNotCapture(F, 1);
2303 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002304 }
2305 break;
2306 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002307 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002308 if (FTy->getNumParams() < 2 ||
2309 !isa<PointerType>(FTy->getParamType(0)))
2310 continue;
2311 // May throw; "open" is a valid pthread cancellation point.
2312 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002313 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002314 if (FTy->getNumParams() != 1 ||
2315 !isa<PointerType>(FTy->getReturnType()) ||
2316 !isa<PointerType>(FTy->getParamType(0)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002317 continue;
2318 setDoesNotThrow(F);
2319 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002320 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002321 }
2322 break;
2323 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002324 if (Name == "tmpfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002325 if (!isa<PointerType>(FTy->getReturnType()))
2326 continue;
2327 setDoesNotThrow(F);
2328 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002329 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002330 if (FTy->getNumParams() != 1 ||
2331 !isa<PointerType>(FTy->getParamType(0)))
2332 continue;
2333 setDoesNotThrow(F);
2334 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002335 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002336 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002337 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002338 if (Name == "htonl" ||
2339 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002340 setDoesNotThrow(F);
2341 setDoesNotAccessMemory(F);
2342 }
2343 break;
2344 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002345 if (Name == "ntohl" ||
2346 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002347 setDoesNotThrow(F);
2348 setDoesNotAccessMemory(F);
2349 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002350 break;
2351 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002352 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002353 if (FTy->getNumParams() != 2 ||
2354 !isa<PointerType>(FTy->getParamType(0)) ||
2355 !isa<PointerType>(FTy->getParamType(1)))
2356 continue;
2357 setDoesNotThrow(F);
2358 setDoesNotCapture(F, 1);
2359 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002360 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002361 if (FTy->getNumParams() != 3 ||
2362 !isa<PointerType>(FTy->getParamType(0)))
2363 continue;
2364 setDoesNotThrow(F);
2365 setDoesNotCapture(F, 1);
2366 }
2367 break;
2368 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002369 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002370 if (FTy->getNumParams() != 4 ||
2371 !isa<PointerType>(FTy->getParamType(3)))
2372 continue;
2373 // May throw; places call through function pointer.
2374 setDoesNotCapture(F, 4);
2375 }
2376 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002377 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002378 if (Name == "__strdup" ||
2379 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002380 if (FTy->getNumParams() < 1 ||
2381 !isa<PointerType>(FTy->getReturnType()) ||
2382 !isa<PointerType>(FTy->getParamType(0)))
2383 continue;
2384 setDoesNotThrow(F);
2385 setDoesNotAlias(F, 0);
2386 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002387 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002388 if (FTy->getNumParams() != 3 ||
2389 !isa<PointerType>(FTy->getParamType(1)))
2390 continue;
2391 setDoesNotThrow(F);
2392 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002393 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002394 if (FTy->getNumParams() != 1 ||
2395 !isa<PointerType>(FTy->getParamType(0)))
2396 continue;
2397 setDoesNotThrow(F);
2398 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002399 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002400 if (FTy->getNumParams() != 2 ||
2401 !isa<PointerType>(FTy->getParamType(1)))
2402 continue;
2403 setDoesNotThrow(F);
2404 setDoesNotCapture(F, 2);
2405 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002406 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002407 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002408 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002409 if (FTy->getNumParams() < 1 ||
2410 !isa<PointerType>(FTy->getParamType(0)))
2411 continue;
2412 setDoesNotThrow(F);
2413 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002414 } else if (Name == "\1stat64" ||
2415 Name == "\1lstat64" ||
2416 Name == "\1statvfs64" ||
2417 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002418 if (FTy->getNumParams() < 1 ||
Nick Lewycky225f7472009-02-15 22:47:25 +00002419 !isa<PointerType>(FTy->getParamType(0)) ||
2420 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002421 continue;
2422 setDoesNotThrow(F);
2423 setDoesNotCapture(F, 1);
2424 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002425 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002426 if (FTy->getNumParams() != 2 ||
2427 !isa<PointerType>(FTy->getReturnType()) ||
2428 !isa<PointerType>(FTy->getParamType(0)) ||
2429 !isa<PointerType>(FTy->getParamType(1)))
2430 continue;
2431 setDoesNotThrow(F);
2432 setDoesNotAlias(F, 0);
2433 setDoesNotCapture(F, 1);
2434 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002435 } else if (Name == "\1fseeko64" ||
2436 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002437 if (FTy->getNumParams() == 0 ||
2438 !isa<PointerType>(FTy->getParamType(0)))
2439 continue;
2440 setDoesNotThrow(F);
2441 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002442 } else if (Name == "\1tmpfile64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002443 if (!isa<PointerType>(FTy->getReturnType()))
2444 continue;
2445 setDoesNotThrow(F);
2446 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002447 } else if (Name == "\1fstat64" ||
2448 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002449 if (FTy->getNumParams() != 2 ||
2450 !isa<PointerType>(FTy->getParamType(1)))
2451 continue;
2452 setDoesNotThrow(F);
2453 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002454 } else if (Name == "\1open64") {
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);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002460 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002461 break;
2462 }
2463 }
2464 return Modified;
2465}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002466
2467// TODO:
2468// Additional cases that we need to add to this file:
2469//
2470// cbrt:
2471// * cbrt(expN(X)) -> expN(x/3)
2472// * cbrt(sqrt(x)) -> pow(x,1/6)
2473// * cbrt(sqrt(x)) -> pow(x,1/9)
2474//
2475// cos, cosf, cosl:
2476// * cos(-x) -> cos(x)
2477//
2478// exp, expf, expl:
2479// * exp(log(x)) -> x
2480//
2481// log, logf, logl:
2482// * log(exp(x)) -> x
2483// * log(x**y) -> y*log(x)
2484// * log(exp(y)) -> y*log(e)
2485// * log(exp2(y)) -> y*log(2)
2486// * log(exp10(y)) -> y*log(10)
2487// * log(sqrt(x)) -> 0.5*log(x)
2488// * log(pow(x,y)) -> y*log(x)
2489//
2490// lround, lroundf, lroundl:
2491// * lround(cnst) -> cnst'
2492//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002493// pow, powf, powl:
2494// * pow(exp(x),y) -> exp(x*y)
2495// * pow(sqrt(x),y) -> pow(x,y*0.5)
2496// * pow(pow(x,y),z)-> pow(x,y*z)
2497//
2498// puts:
2499// * puts("") -> putchar("\n")
2500//
2501// round, roundf, roundl:
2502// * round(cnst) -> cnst'
2503//
2504// signbit:
2505// * signbit(cnst) -> cnst'
2506// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2507//
2508// sqrt, sqrtf, sqrtl:
2509// * sqrt(expN(x)) -> expN(x*0.5)
2510// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2511// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2512//
2513// stpcpy:
2514// * stpcpy(str, "literal") ->
2515// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2516// strrchr:
2517// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2518// (if c is a constant integer and s is a constant string)
2519// * strrchr(s1,0) -> strchr(s1,0)
2520//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002521// strpbrk:
2522// * strpbrk(s,a) -> offset_in_for(s,a)
2523// (if s and a are both constant strings)
2524// * strpbrk(s,"") -> 0
2525// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2526//
2527// strspn, strcspn:
2528// * strspn(s,a) -> const_int (if both args are constant)
2529// * strspn("",a) -> 0
2530// * strspn(s,"") -> 0
2531// * strcspn(s,a) -> const_int (if both args are constant)
2532// * strcspn("",a) -> 0
2533// * strcspn(s,"") -> strlen(a)
2534//
2535// strstr:
2536// * strstr(x,x) -> x
2537// * strstr(s1,s2) -> offset_of_s2_in(s1)
2538// (if s1 and s2 are constant strings)
2539//
2540// tan, tanf, tanl:
2541// * tan(atan(x)) -> x
2542//
2543// trunc, truncf, truncl:
2544// * trunc(cnst) -> cnst'
2545//
2546//