blob: 296f0c9ac5409e0dde94418e28e009fc592dbed1 [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.
Chris Lattner74965f22009-11-09 04:57:04 +0000103 Value *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.
Chris Lattner74965f22009-11-09 04:57:04 +0000255Value *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 Lattner74965f22009-11-09 04:57:04 +0000267 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000268}
269
270/// EmitPutS - Emit a call to the puts function. This assumes that Str is
271/// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000272void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000273 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000274 AttributeWithIndex AWI[2];
275 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
276 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
277
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000278 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000279 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000280 Type::getInt8PtrTy(*Context),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000281 NULL);
282 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
283 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
284 CI->setCallingConv(F->getCallingConv());
285
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000286}
287
288/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
289/// an integer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000290void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000291 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000292 AttributeWithIndex AWI[2];
293 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
294 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
295 Constant *F;
296 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000297 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
298 Type::getInt32Ty(*Context),
299 Type::getInt32Ty(*Context), File->getType(),
300 NULL);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000301 else
Eric Christopher37c8b862009-10-07 21:14:25 +0000302 F = M->getOrInsertFunction("fputc",
303 Type::getInt32Ty(*Context),
304 Type::getInt32Ty(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000305 File->getType(), NULL);
Owen Anderson1d0be152009-08-13 21:58:54 +0000306 Char = B.CreateIntCast(Char, Type::getInt32Ty(*Context), "chari");
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000307 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
308
309 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
310 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000311}
312
313/// EmitFPutS - Emit a call to the puts function. Str is required to be a
314/// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000315void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000316 Module *M = Caller->getParent();
Nick Lewycky225f7472009-02-15 22:47:25 +0000317 AttributeWithIndex AWI[3];
318 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
319 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
320 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000321 Constant *F;
322 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000323 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
324 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000325 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000326 File->getType(), NULL);
327 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000328 F = M->getOrInsertFunction("fputs", Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000329 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000330 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000331 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
332
333 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
334 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000335}
336
337/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
338/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
339void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Eric Christopher7a61d702008-08-08 19:39:37 +0000340 IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000341 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000342 AttributeWithIndex AWI[3];
343 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
344 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
345 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
346 Constant *F;
347 if (isa<PointerType>(File->getType()))
348 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000349 TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000350 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000351 TD->getIntPtrType(*Context),
352 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000353 File->getType(), NULL);
354 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000355 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000356 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000357 TD->getIntPtrType(*Context),
358 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000359 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000360 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Owen Anderson1d0be152009-08-13 21:58:54 +0000361 ConstantInt::get(TD->getIntPtrType(*Context), 1), File);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000362
363 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
364 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000365}
366
367//===----------------------------------------------------------------------===//
368// Helper Functions
369//===----------------------------------------------------------------------===//
370
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000371/// GetStringLengthH - If we can compute the length of the string pointed to by
372/// the specified pointer, return 'len+1'. If we can't, return 0.
373static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
374 // Look through noop bitcast instructions.
375 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
376 return GetStringLengthH(BCI->getOperand(0), PHIs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000377
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000378 // If this is a PHI node, there are two cases: either we have already seen it
379 // or we haven't.
380 if (PHINode *PN = dyn_cast<PHINode>(V)) {
381 if (!PHIs.insert(PN))
382 return ~0ULL; // already in the set.
Eric Christopher37c8b862009-10-07 21:14:25 +0000383
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000384 // If it was new, see if all the input strings are the same length.
385 uint64_t LenSoFar = ~0ULL;
386 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
387 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
388 if (Len == 0) return 0; // Unknown length -> unknown.
Eric Christopher37c8b862009-10-07 21:14:25 +0000389
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000390 if (Len == ~0ULL) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +0000391
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000392 if (Len != LenSoFar && LenSoFar != ~0ULL)
393 return 0; // Disagree -> unknown.
394 LenSoFar = Len;
395 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000396
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000397 // Success, all agree.
398 return LenSoFar;
399 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000400
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000401 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
402 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
403 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
404 if (Len1 == 0) return 0;
405 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
406 if (Len2 == 0) return 0;
407 if (Len1 == ~0ULL) return Len2;
408 if (Len2 == ~0ULL) return Len1;
409 if (Len1 != Len2) return 0;
410 return Len1;
411 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000412
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000413 // If the value is not a GEP instruction nor a constant expression with a
414 // GEP instruction, then return unknown.
415 User *GEP = 0;
416 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
417 GEP = GEPI;
418 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
419 if (CE->getOpcode() != Instruction::GetElementPtr)
420 return 0;
421 GEP = CE;
422 } else {
423 return 0;
424 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000425
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000426 // Make sure the GEP has exactly three arguments.
427 if (GEP->getNumOperands() != 3)
428 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000429
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000430 // Check to make sure that the first operand of the GEP is an integer and
431 // has value 0 so that we are sure we're indexing into the initializer.
432 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
433 if (!Idx->isZero())
434 return 0;
435 } else
436 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000437
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000438 // If the second index isn't a ConstantInt, then this is a variable index
439 // into the array. If this occurs, we can't say anything meaningful about
440 // the string.
441 uint64_t StartIdx = 0;
442 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
443 StartIdx = CI->getZExtValue();
444 else
445 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000446
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000447 // The GEP instruction, constant or instruction, must reference a global
448 // variable that is a constant and is initialized. The referenced constant
449 // initializer is the array that we'll use for optimization.
450 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
Dan Gohman107f41f2009-08-19 00:11:12 +0000451 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
452 GV->mayBeOverridden())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000453 return 0;
454 Constant *GlobalInit = GV->getInitializer();
Eric Christopher37c8b862009-10-07 21:14:25 +0000455
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000456 // Handle the ConstantAggregateZero case, which is a degenerate case. The
457 // initializer is constant zero so the length of the string must be zero.
458 if (isa<ConstantAggregateZero>(GlobalInit))
459 return 1; // Len = 0 offset by 1.
Eric Christopher37c8b862009-10-07 21:14:25 +0000460
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000461 // Must be a Constant Array
462 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
Owen Anderson1d0be152009-08-13 21:58:54 +0000463 if (!Array ||
464 Array->getType()->getElementType() != Type::getInt8Ty(V->getContext()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000465 return false;
Eric Christopher37c8b862009-10-07 21:14:25 +0000466
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000467 // Get the number of elements in the array
468 uint64_t NumElts = Array->getType()->getNumElements();
Eric Christopher37c8b862009-10-07 21:14:25 +0000469
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000470 // Traverse the constant array from StartIdx (derived above) which is
471 // the place the GEP refers to in the array.
472 for (unsigned i = StartIdx; i != NumElts; ++i) {
473 Constant *Elt = Array->getOperand(i);
474 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
475 if (!CI) // This array isn't suitable, non-int initializer.
476 return 0;
477 if (CI->isZero())
478 return i-StartIdx+1; // We found end of string, success!
479 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000480
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000481 return 0; // The array isn't null terminated, conservatively return 'unknown'.
482}
483
484/// GetStringLength - If we can compute the length of the string pointed to by
485/// the specified pointer, return 'len+1'. If we can't, return 0.
486static uint64_t GetStringLength(Value *V) {
487 if (!isa<PointerType>(V->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000488
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000489 SmallPtrSet<PHINode*, 32> PHIs;
490 uint64_t Len = GetStringLengthH(V, PHIs);
491 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
492 // an empty string as a length.
493 return Len == ~0ULL ? 1 : Len;
494}
495
496/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +0000497/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000498static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
499 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
500 UI != E; ++UI) {
501 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
502 if (IC->isEquality())
503 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
504 if (C->isNullValue())
505 continue;
506 // Unknown instruction.
507 return false;
508 }
509 return true;
510}
511
512//===----------------------------------------------------------------------===//
Eric Christopher7b5e6172009-10-27 00:52:25 +0000513// Miscellaneous LibCall/Intrinsic Optimizations
514//===----------------------------------------------------------------------===//
515
516namespace {
517struct SizeOpt : public LibCallOptimization {
518 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
519 // TODO: We can do more with this, but delaying to here should be no change
520 // in behavior.
521 ConstantInt *Const = dyn_cast<ConstantInt>(CI->getOperand(2));
522
523 if (!Const) return 0;
524
525 if (Const->getZExtValue() < 2)
526 return Constant::getAllOnesValue(Const->getType());
527 else
528 return ConstantInt::get(Const->getType(), 0);
529 }
530};
531}
532
533//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000534// String and Memory LibCall Optimizations
535//===----------------------------------------------------------------------===//
536
537//===---------------------------------------===//
538// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000539namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000540struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000541 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000542 // Verify the "strcat" function prototype.
543 const FunctionType *FT = Callee->getFunctionType();
544 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000545 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000546 FT->getParamType(0) != FT->getReturnType() ||
547 FT->getParamType(1) != FT->getReturnType())
548 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000549
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000550 // Extract some information from the instruction
551 Value *Dst = CI->getOperand(1);
552 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000553
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000554 // See if we can get the length of the input string.
555 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000556 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000557 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000558
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000559 // Handle the simple, do-nothing case: strcat(x, "") -> x
560 if (Len == 0)
561 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000562
563 // These optimizations require TargetData.
564 if (!TD) return 0;
565
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000566 EmitStrLenMemCpy(Src, Dst, Len, B);
567 return Dst;
568 }
569
570 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000571 // We need to find the end of the destination string. That's where the
572 // memory is to be moved to. We just generate a call to strlen.
573 Value *DstLen = EmitStrLen(Dst, B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000574
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000575 // Now that we have the destination's length, we must index into the
576 // destination's pointer to get the actual memcpy destination (end of
577 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000578 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000579
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000580 // We have enough information to now generate the memcpy call to do the
581 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000582 EmitMemCpy(CpyDst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000583 ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000584 }
585};
586
587//===---------------------------------------===//
588// 'strncat' Optimizations
589
Chris Lattner3e8b6632009-09-02 06:11:42 +0000590struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000591 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
592 // Verify the "strncat" function prototype.
593 const FunctionType *FT = Callee->getFunctionType();
594 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000595 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000596 FT->getParamType(0) != FT->getReturnType() ||
597 FT->getParamType(1) != FT->getReturnType() ||
598 !isa<IntegerType>(FT->getParamType(2)))
599 return 0;
600
601 // Extract some information from the instruction
602 Value *Dst = CI->getOperand(1);
603 Value *Src = CI->getOperand(2);
604 uint64_t Len;
605
606 // We don't do anything if length is not constant
607 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
608 Len = LengthArg->getZExtValue();
609 else
610 return 0;
611
612 // See if we can get the length of the input string.
613 uint64_t SrcLen = GetStringLength(Src);
614 if (SrcLen == 0) return 0;
615 --SrcLen; // Unbias length.
616
617 // Handle the simple, do-nothing cases:
618 // strncat(x, "", c) -> x
619 // strncat(x, c, 0) -> x
620 if (SrcLen == 0 || Len == 0) return Dst;
621
Dan Gohmanf14d9192009-08-18 00:48:13 +0000622 // These optimizations require TargetData.
623 if (!TD) return 0;
624
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000625 // We don't optimize this case
626 if (Len < SrcLen) return 0;
627
628 // strncat(x, s, c) -> strcat(x, s)
629 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000630 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000631 return Dst;
632 }
633};
634
635//===---------------------------------------===//
636// 'strchr' Optimizations
637
Chris Lattner3e8b6632009-09-02 06:11:42 +0000638struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000639 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000640 // Verify the "strchr" function prototype.
641 const FunctionType *FT = Callee->getFunctionType();
642 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000643 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000644 FT->getParamType(0) != FT->getReturnType())
645 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000646
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000647 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000648
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000649 // If the second operand is non-constant, see if we can compute the length
650 // of the input string and turn this into memchr.
651 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
652 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000653 // These optimizations require TargetData.
654 if (!TD) return 0;
655
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000656 uint64_t Len = GetStringLength(SrcStr);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000657 if (Len == 0 ||
658 FT->getParamType(1) != Type::getInt32Ty(*Context)) // memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000659 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000660
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000661 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Owen Anderson1d0be152009-08-13 21:58:54 +0000662 ConstantInt::get(TD->getIntPtrType(*Context), Len), B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000663 }
664
665 // Otherwise, the character is a constant, see if the first argument is
666 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000667 std::string Str;
668 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000669 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000670
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000671 // strchr can find the nul character.
672 Str += '\0';
673 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000674
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000675 // Compute the offset.
676 uint64_t i = 0;
677 while (1) {
678 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000679 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000680 // Did we find our match?
681 if (Str[i] == CharValue)
682 break;
683 ++i;
684 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000685
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000686 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000687 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000688 return B.CreateGEP(SrcStr, Idx, "strchr");
689 }
690};
691
692//===---------------------------------------===//
693// 'strcmp' Optimizations
694
Chris Lattner3e8b6632009-09-02 06:11:42 +0000695struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000696 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000697 // Verify the "strcmp" function prototype.
698 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000699 if (FT->getNumParams() != 2 ||
700 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000701 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000702 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000703 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000704
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000705 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
706 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000707 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000708
Bill Wendling0582ae92009-03-13 04:39:26 +0000709 std::string Str1, Str2;
710 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
711 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000712
Bill Wendling0582ae92009-03-13 04:39:26 +0000713 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000714 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000715
Bill Wendling0582ae92009-03-13 04:39:26 +0000716 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000717 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000718
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000719 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000720 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000721 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000722 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000723
724 // strcmp(P, "x") -> memcmp(P, "x", 2)
725 uint64_t Len1 = GetStringLength(Str1P);
726 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000727 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000728 // These optimizations require TargetData.
729 if (!TD) return 0;
730
Nick Lewycky13a09e22008-12-21 00:19:21 +0000731 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000732 ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattner849832c2009-06-19 04:17:36 +0000733 std::min(Len1, Len2)), B);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000734 }
735
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000736 return 0;
737 }
738};
739
740//===---------------------------------------===//
741// 'strncmp' Optimizations
742
Chris Lattner3e8b6632009-09-02 06:11:42 +0000743struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000744 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000745 // Verify the "strncmp" function prototype.
746 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000747 if (FT->getNumParams() != 3 ||
748 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000749 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000750 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000751 !isa<IntegerType>(FT->getParamType(2)))
752 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000753
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000754 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
755 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000756 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000757
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000758 // Get the length argument if it is constant.
759 uint64_t Length;
760 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
761 Length = LengthArg->getZExtValue();
762 else
763 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000764
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000765 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000766 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000767
Bill Wendling0582ae92009-03-13 04:39:26 +0000768 std::string Str1, Str2;
769 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
770 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000771
Bill Wendling0582ae92009-03-13 04:39:26 +0000772 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000773 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000774
Bill Wendling0582ae92009-03-13 04:39:26 +0000775 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000776 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000777
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000778 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000779 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000780 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000781 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000782 return 0;
783 }
784};
785
786
787//===---------------------------------------===//
788// 'strcpy' Optimizations
789
Chris Lattner3e8b6632009-09-02 06:11:42 +0000790struct StrCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000791 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000792 // Verify the "strcpy" function prototype.
793 const FunctionType *FT = Callee->getFunctionType();
794 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
795 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000796 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000797 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000798
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000799 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
800 if (Dst == Src) // strcpy(x,x) -> x
801 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000802
Dan Gohmanf14d9192009-08-18 00:48:13 +0000803 // These optimizations require TargetData.
804 if (!TD) return 0;
805
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000806 // See if we can get the length of the input string.
807 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000808 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000809
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000810 // We have enough information to now generate the memcpy call to do the
811 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000812 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000813 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000814 return Dst;
815 }
816};
817
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000818//===---------------------------------------===//
819// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000820
Chris Lattner3e8b6632009-09-02 06:11:42 +0000821struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000822 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
823 const FunctionType *FT = Callee->getFunctionType();
824 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
825 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000826 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000827 !isa<IntegerType>(FT->getParamType(2)))
828 return 0;
829
830 Value *Dst = CI->getOperand(1);
831 Value *Src = CI->getOperand(2);
832 Value *LenOp = CI->getOperand(3);
833
834 // See if we can get the length of the input string.
835 uint64_t SrcLen = GetStringLength(Src);
836 if (SrcLen == 0) return 0;
837 --SrcLen;
838
839 if (SrcLen == 0) {
840 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000841 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
842 B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000843 return Dst;
844 }
845
846 uint64_t Len;
847 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
848 Len = LengthArg->getZExtValue();
849 else
850 return 0;
851
852 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
853
Dan Gohmanf14d9192009-08-18 00:48:13 +0000854 // These optimizations require TargetData.
855 if (!TD) return 0;
856
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000857 // Let strncpy handle the zero padding
858 if (Len > SrcLen+1) return 0;
859
860 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000861 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000862 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000863
864 return Dst;
865 }
866};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000867
868//===---------------------------------------===//
869// 'strlen' Optimizations
870
Chris Lattner3e8b6632009-09-02 06:11:42 +0000871struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000872 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000873 const FunctionType *FT = Callee->getFunctionType();
874 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000875 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000876 !isa<IntegerType>(FT->getReturnType()))
877 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000878
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000879 Value *Src = CI->getOperand(1);
880
881 // Constant folding: strlen("xyz") -> 3
882 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000883 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000884
885 // Handle strlen(p) != 0.
886 if (!IsOnlyUsedInZeroEqualityComparison(CI)) return 0;
887
888 // strlen(x) != 0 --> *x != 0
889 // strlen(x) == 0 --> *x == 0
890 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
891 }
892};
893
894//===---------------------------------------===//
Nick Lewycky4c498412009-02-13 15:31:46 +0000895// 'strto*' Optimizations
896
Chris Lattner3e8b6632009-09-02 06:11:42 +0000897struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000898 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
899 const FunctionType *FT = Callee->getFunctionType();
900 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
901 !isa<PointerType>(FT->getParamType(0)) ||
902 !isa<PointerType>(FT->getParamType(1)))
903 return 0;
904
905 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000906 if (isa<ConstantPointerNull>(EndPtr)) {
907 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000908 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000909 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000910
911 return 0;
912 }
913};
914
915
916//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000917// 'memcmp' Optimizations
918
Chris Lattner3e8b6632009-09-02 06:11:42 +0000919struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000920 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000921 const FunctionType *FT = Callee->getFunctionType();
922 if (FT->getNumParams() != 3 || !isa<PointerType>(FT->getParamType(0)) ||
923 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000924 FT->getReturnType() != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000925 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000926
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000927 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000928
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000929 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000930 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000931
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000932 // Make sure we have a constant length.
933 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000934 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000935 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000936
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000937 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000938 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000939
940 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
941 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
942 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +0000943 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000944 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000945
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000946 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS ^ *(short*)RHS) != 0
947 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS ^ *(int*)RHS) != 0
948 if ((Len == 2 || Len == 4) && IsOnlyUsedInZeroEqualityComparison(CI)) {
Owen Andersondebcb012009-07-29 22:17:13 +0000949 const Type *PTy = PointerType::getUnqual(Len == 2 ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000950 Type::getInt16Ty(*Context) : Type::getInt32Ty(*Context));
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000951 LHS = B.CreateBitCast(LHS, PTy, "tmp");
952 RHS = B.CreateBitCast(RHS, PTy, "tmp");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000953 LoadInst *LHSV = B.CreateLoad(LHS, "lhsv");
954 LoadInst *RHSV = B.CreateLoad(RHS, "rhsv");
955 LHSV->setAlignment(1); RHSV->setAlignment(1); // Unaligned loads.
956 return B.CreateZExt(B.CreateXor(LHSV, RHSV, "shortdiff"), CI->getType());
957 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000958
Benjamin Kramer992a6372009-11-05 17:44:22 +0000959 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
960 std::string LHSStr, RHSStr;
961 if (GetConstantStringInfo(LHS, LHSStr) &&
962 GetConstantStringInfo(RHS, RHSStr)) {
963 // Make sure we're not reading out-of-bounds memory.
964 if (Len > LHSStr.length() || Len > RHSStr.length())
965 return 0;
966 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
967 return ConstantInt::get(CI->getType(), Ret);
968 }
969
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000970 return 0;
971 }
972};
973
974//===---------------------------------------===//
975// 'memcpy' Optimizations
976
Chris Lattner3e8b6632009-09-02 06:11:42 +0000977struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000978 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000979 // These optimizations require TargetData.
980 if (!TD) return 0;
981
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000982 const FunctionType *FT = Callee->getFunctionType();
983 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
984 !isa<PointerType>(FT->getParamType(0)) ||
985 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000986 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000987 return 0;
988
989 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
990 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
991 return CI->getOperand(1);
992 }
993};
994
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000995//===---------------------------------------===//
996// 'memmove' Optimizations
997
Chris Lattner3e8b6632009-09-02 06:11:42 +0000998struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000999 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001000 // These optimizations require TargetData.
1001 if (!TD) return 0;
1002
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001003 const FunctionType *FT = Callee->getFunctionType();
1004 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1005 !isa<PointerType>(FT->getParamType(0)) ||
1006 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001007 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001008 return 0;
1009
1010 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
1011 Module *M = Caller->getParent();
1012 Intrinsic::ID IID = Intrinsic::memmove;
1013 const Type *Tys[1];
Owen Anderson1d0be152009-08-13 21:58:54 +00001014 Tys[0] = TD->getIntPtrType(*Context);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001015 Value *MemMove = Intrinsic::getDeclaration(M, IID, Tys, 1);
1016 Value *Dst = CastToCStr(CI->getOperand(1), B);
1017 Value *Src = CastToCStr(CI->getOperand(2), B);
1018 Value *Size = CI->getOperand(3);
Owen Anderson1d0be152009-08-13 21:58:54 +00001019 Value *Align = ConstantInt::get(Type::getInt32Ty(*Context), 1);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001020 B.CreateCall4(MemMove, Dst, Src, Size, Align);
1021 return CI->getOperand(1);
1022 }
1023};
1024
1025//===---------------------------------------===//
1026// 'memset' Optimizations
1027
Chris Lattner3e8b6632009-09-02 06:11:42 +00001028struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001029 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001030 // These optimizations require TargetData.
1031 if (!TD) return 0;
1032
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001033 const FunctionType *FT = Callee->getFunctionType();
1034 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1035 !isa<PointerType>(FT->getParamType(0)) ||
Eli Friedman62bb4132009-07-18 08:34:51 +00001036 !isa<IntegerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001037 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001038 return 0;
1039
1040 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +00001041 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1042 false);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001043 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001044 return CI->getOperand(1);
1045 }
1046};
1047
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001048//===----------------------------------------------------------------------===//
1049// Math Library Optimizations
1050//===----------------------------------------------------------------------===//
1051
1052//===---------------------------------------===//
1053// 'pow*' Optimizations
1054
Chris Lattner3e8b6632009-09-02 06:11:42 +00001055struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001056 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001057 const FunctionType *FT = Callee->getFunctionType();
1058 // Just make sure this has 2 arguments of the same FP type, which match the
1059 // result type.
1060 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1061 FT->getParamType(0) != FT->getParamType(1) ||
1062 !FT->getParamType(0)->isFloatingPoint())
1063 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001064
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001065 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
1066 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1067 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
1068 return Op1C;
1069 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +00001070 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001071 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001072
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001073 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1074 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001075
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001076 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001077 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001078
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001079 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +00001080 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1081 // This is faster than calling pow, and still handles negative zero
1082 // and negative infinite correctly.
1083 // TODO: In fast-math mode, this could be just sqrt(x).
1084 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +00001085 Value *Inf = ConstantFP::getInfinity(CI->getType());
1086 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +00001087 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1088 Callee->getAttributes());
1089 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1090 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +00001091 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
1092 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
1093 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001094 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001095
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001096 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1097 return Op1;
1098 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001099 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001100 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001101 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001102 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001103 return 0;
1104 }
1105};
1106
1107//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +00001108// 'exp2' Optimizations
1109
Chris Lattner3e8b6632009-09-02 06:11:42 +00001110struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001111 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +00001112 const FunctionType *FT = Callee->getFunctionType();
1113 // Just make sure this has 1 argument of FP type, which matches the
1114 // result type.
1115 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1116 !FT->getParamType(0)->isFloatingPoint())
1117 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001118
Chris Lattnere818f772008-05-02 18:43:35 +00001119 Value *Op = CI->getOperand(1);
1120 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1121 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1122 Value *LdExpArg = 0;
1123 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1124 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001125 LdExpArg = B.CreateSExt(OpC->getOperand(0),
1126 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001127 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1128 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001129 LdExpArg = B.CreateZExt(OpC->getOperand(0),
1130 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001131 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001132
Chris Lattnere818f772008-05-02 18:43:35 +00001133 if (LdExpArg) {
1134 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001135 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001136 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001137 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001138 Name = "ldexp";
1139 else
1140 Name = "ldexpl";
1141
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001142 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001143 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001144 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +00001145
1146 Module *M = Caller->getParent();
1147 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +00001148 Op->getType(),
1149 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001150 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1151 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1152 CI->setCallingConv(F->getCallingConv());
1153
1154 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +00001155 }
1156 return 0;
1157 }
1158};
Chris Lattnere818f772008-05-02 18:43:35 +00001159
1160//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001161// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1162
Chris Lattner3e8b6632009-09-02 06:11:42 +00001163struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001164 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001165 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001166 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1167 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001168 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001169
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001170 // If this is something like 'floor((double)floatval)', convert to floorf.
1171 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001172 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001173 return 0;
1174
1175 // floor((double)floatval) -> (double)floorf(floatval)
1176 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +00001177 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
1178 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +00001179 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001180 }
1181};
1182
1183//===----------------------------------------------------------------------===//
1184// Integer Optimizations
1185//===----------------------------------------------------------------------===//
1186
1187//===---------------------------------------===//
1188// 'ffs*' Optimizations
1189
Chris Lattner3e8b6632009-09-02 06:11:42 +00001190struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001191 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001192 const FunctionType *FT = Callee->getFunctionType();
1193 // Just make sure this has 2 arguments of the same FP type, which match the
1194 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +00001195 if (FT->getNumParams() != 1 ||
1196 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001197 !isa<IntegerType>(FT->getParamType(0)))
1198 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001199
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001200 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001201
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001202 // Constant fold.
1203 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1204 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +00001205 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +00001206 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001207 CI->getValue().countTrailingZeros()+1);
1208 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001209
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001210 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1211 const Type *ArgType = Op->getType();
1212 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1213 Intrinsic::cttz, &ArgType, 1);
1214 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +00001215 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +00001216 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001217
Owen Andersona7235ea2009-07-31 20:28:14 +00001218 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001219 return B.CreateSelect(Cond, V,
1220 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001221 }
1222};
1223
1224//===---------------------------------------===//
1225// 'isdigit' Optimizations
1226
Chris Lattner3e8b6632009-09-02 06:11:42 +00001227struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001228 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001229 const FunctionType *FT = Callee->getFunctionType();
1230 // We require integer(i32)
1231 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001232 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001233 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001234
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001235 // isdigit(c) -> (c-'0') <u 10
1236 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001237 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001238 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001239 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001240 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001241 return B.CreateZExt(Op, CI->getType());
1242 }
1243};
1244
1245//===---------------------------------------===//
1246// 'isascii' Optimizations
1247
Chris Lattner3e8b6632009-09-02 06:11:42 +00001248struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001249 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001250 const FunctionType *FT = Callee->getFunctionType();
1251 // We require integer(i32)
1252 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001253 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001254 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001255
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001256 // isascii(c) -> c <u 128
1257 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001258 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001259 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001260 return B.CreateZExt(Op, CI->getType());
1261 }
1262};
Eric Christopher37c8b862009-10-07 21:14:25 +00001263
Chris Lattner313f0e62008-06-09 08:26:51 +00001264//===---------------------------------------===//
1265// 'abs', 'labs', 'llabs' Optimizations
1266
Chris Lattner3e8b6632009-09-02 06:11:42 +00001267struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001268 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001269 const FunctionType *FT = Callee->getFunctionType();
1270 // We require integer(integer) where the types agree.
1271 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
1272 FT->getParamType(0) != FT->getReturnType())
1273 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001274
Chris Lattner313f0e62008-06-09 08:26:51 +00001275 // abs(x) -> x >s -1 ? x : -x
1276 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001277 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001278 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001279 "ispos");
1280 Value *Neg = B.CreateNeg(Op, "neg");
1281 return B.CreateSelect(Pos, Op, Neg);
1282 }
1283};
Eric Christopher37c8b862009-10-07 21:14:25 +00001284
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001285
1286//===---------------------------------------===//
1287// 'toascii' Optimizations
1288
Chris Lattner3e8b6632009-09-02 06:11:42 +00001289struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001290 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001291 const FunctionType *FT = Callee->getFunctionType();
1292 // We require i32(i32)
1293 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001294 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001295 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001296
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001297 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001298 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +00001299 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001300 }
1301};
1302
1303//===----------------------------------------------------------------------===//
1304// Formatting and IO Optimizations
1305//===----------------------------------------------------------------------===//
1306
1307//===---------------------------------------===//
1308// 'printf' Optimizations
1309
Chris Lattner3e8b6632009-09-02 06:11:42 +00001310struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001311 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001312 // Require one fixed pointer argument and an integer/void result.
1313 const FunctionType *FT = Callee->getFunctionType();
1314 if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
1315 !(isa<IntegerType>(FT->getReturnType()) ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001316 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001317 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001318
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001319 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001320 std::string FormatStr;
1321 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1322 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001323
1324 // Empty format string -> noop.
1325 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001326 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001327 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001328
Chris Lattner74965f22009-11-09 04:57:04 +00001329 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1330 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001331 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001332 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
1333 FormatStr[0]), B);
1334 if (CI->use_empty()) return CI;
1335 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001336 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001337
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001338 // printf("foo\n") --> puts("foo")
1339 if (FormatStr[FormatStr.size()-1] == '\n' &&
1340 FormatStr.find('%') == std::string::npos) { // no format characters.
1341 // Create a string literal with no \n on it. We expect the constant merge
1342 // pass to be run after this pass, to merge duplicate strings.
1343 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001344 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001345 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1346 GlobalVariable::InternalLinkage, C, "str");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001347 EmitPutS(C, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001348 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001349 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001350 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001351
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001352 // Optimize specific format strings.
1353 // printf("%c", chr) --> putchar(*(i8*)dst)
1354 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
1355 isa<IntegerType>(CI->getOperand(2)->getType())) {
Chris Lattner74965f22009-11-09 04:57:04 +00001356 Value *Res = EmitPutChar(CI->getOperand(2), B);
1357
1358 if (CI->use_empty()) return CI;
1359 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001360 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001361
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001362 // printf("%s\n", str) --> puts(str)
1363 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
1364 isa<PointerType>(CI->getOperand(2)->getType()) &&
1365 CI->use_empty()) {
1366 EmitPutS(CI->getOperand(2), B);
1367 return CI;
1368 }
1369 return 0;
1370 }
1371};
1372
1373//===---------------------------------------===//
1374// 'sprintf' Optimizations
1375
Chris Lattner3e8b6632009-09-02 06:11:42 +00001376struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001377 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001378 // Require two fixed pointer arguments and an integer result.
1379 const FunctionType *FT = Callee->getFunctionType();
1380 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1381 !isa<PointerType>(FT->getParamType(1)) ||
1382 !isa<IntegerType>(FT->getReturnType()))
1383 return 0;
1384
1385 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001386 std::string FormatStr;
1387 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1388 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001389
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001390 // If we just have a format string (nothing else crazy) transform it.
1391 if (CI->getNumOperands() == 3) {
1392 // Make sure there's no % in the constant array. We could try to handle
1393 // %% -> % in the future if we cared.
1394 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1395 if (FormatStr[i] == '%')
1396 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001397
1398 // These optimizations require TargetData.
1399 if (!TD) return 0;
1400
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001401 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1402 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Owen Anderson1d0be152009-08-13 21:58:54 +00001403 ConstantInt::get(TD->getIntPtrType(*Context), FormatStr.size()+1),1,B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001404 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001405 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001406
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001407 // The remaining optimizations require the format string to be "%s" or "%c"
1408 // and have an extra operand.
1409 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1410 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001411
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001412 // Decode the second character of the format string.
1413 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001414 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001415 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001416 Value *V = B.CreateTrunc(CI->getOperand(3),
1417 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001418 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1419 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001420 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1421 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001422 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001423
Owen Andersoneed707b2009-07-24 23:12:02 +00001424 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001425 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001426
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001427 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001428 // These optimizations require TargetData.
1429 if (!TD) return 0;
1430
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001431 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1432 if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
1433
1434 Value *Len = EmitStrLen(CI->getOperand(3), B);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001435 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001436 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001437 "leninc");
1438 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001439
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001440 // The sprintf result is the unincremented number of bytes in the string.
1441 return B.CreateIntCast(Len, CI->getType(), false);
1442 }
1443 return 0;
1444 }
1445};
1446
1447//===---------------------------------------===//
1448// 'fwrite' Optimizations
1449
Chris Lattner3e8b6632009-09-02 06:11:42 +00001450struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001451 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001452 // Require a pointer, an integer, an integer, a pointer, returning integer.
1453 const FunctionType *FT = Callee->getFunctionType();
1454 if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
1455 !isa<IntegerType>(FT->getParamType(1)) ||
1456 !isa<IntegerType>(FT->getParamType(2)) ||
1457 !isa<PointerType>(FT->getParamType(3)) ||
1458 !isa<IntegerType>(FT->getReturnType()))
1459 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001460
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001461 // Get the element size and count.
1462 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1463 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1464 if (!SizeC || !CountC) return 0;
1465 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001466
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001467 // If this is writing zero records, remove the call (it's a noop).
1468 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001469 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001470
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001471 // If this is writing one byte, turn it into fputc.
1472 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1473 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1474 EmitFPutC(Char, CI->getOperand(4), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001475 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001476 }
1477
1478 return 0;
1479 }
1480};
1481
1482//===---------------------------------------===//
1483// 'fputs' Optimizations
1484
Chris Lattner3e8b6632009-09-02 06:11:42 +00001485struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001486 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001487 // These optimizations require TargetData.
1488 if (!TD) return 0;
1489
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001490 // Require two pointers. Also, we can't optimize if return value is used.
1491 const FunctionType *FT = Callee->getFunctionType();
1492 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1493 !isa<PointerType>(FT->getParamType(1)) ||
1494 !CI->use_empty())
1495 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001496
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001497 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1498 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001499 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001500 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001501 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001502 CI->getOperand(2), B);
1503 return CI; // Known to have no uses (see above).
1504 }
1505};
1506
1507//===---------------------------------------===//
1508// 'fprintf' Optimizations
1509
Chris Lattner3e8b6632009-09-02 06:11:42 +00001510struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001511 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001512 // Require two fixed paramters as pointers and integer result.
1513 const FunctionType *FT = Callee->getFunctionType();
1514 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1515 !isa<PointerType>(FT->getParamType(1)) ||
1516 !isa<IntegerType>(FT->getReturnType()))
1517 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001518
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001519 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001520 std::string FormatStr;
1521 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1522 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001523
1524 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1525 if (CI->getNumOperands() == 3) {
1526 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1527 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001528 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001529
1530 // These optimizations require TargetData.
1531 if (!TD) return 0;
1532
Owen Anderson1d0be152009-08-13 21:58:54 +00001533 EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001534 FormatStr.size()),
1535 CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001536 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001537 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001538
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001539 // The remaining optimizations require the format string to be "%s" or "%c"
1540 // and have an extra operand.
1541 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1542 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001543
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001544 // Decode the second character of the format string.
1545 if (FormatStr[1] == 'c') {
1546 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1547 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1548 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001549 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001550 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001551
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001552 if (FormatStr[1] == 's') {
1553 // fprintf(F, "%s", str) -> fputs(str, F)
1554 if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
1555 return 0;
1556 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1557 return CI;
1558 }
1559 return 0;
1560 }
1561};
1562
Bill Wendlingac178222008-05-05 21:37:59 +00001563} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001564
1565//===----------------------------------------------------------------------===//
1566// SimplifyLibCalls Pass Implementation
1567//===----------------------------------------------------------------------===//
1568
1569namespace {
1570 /// This pass optimizes well known library functions from libc and libm.
1571 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001572 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001573 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001574 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001575 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1576 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
1577 StrToOpt StrTo; MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove;
1578 MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001579 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001580 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001581 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001582 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1583 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001584 // Formatting and IO Optimizations
1585 SPrintFOpt SPrintF; PrintFOpt PrintF;
1586 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher7b5e6172009-10-27 00:52:25 +00001587 SizeOpt ObjectSize;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001588
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001589 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001590 public:
1591 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001592 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001593
1594 void InitOptimizations();
1595 bool runOnFunction(Function &F);
1596
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001597 void setDoesNotAccessMemory(Function &F);
1598 void setOnlyReadsMemory(Function &F);
1599 void setDoesNotThrow(Function &F);
1600 void setDoesNotCapture(Function &F, unsigned n);
1601 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001602 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001603
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001604 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001605 }
1606 };
1607 char SimplifyLibCalls::ID = 0;
1608} // end anonymous namespace.
1609
1610static RegisterPass<SimplifyLibCalls>
1611X("simplify-libcalls", "Simplify well-known library calls");
1612
1613// Public interface to the Simplify LibCalls pass.
1614FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001615 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001616}
1617
1618/// Optimizations - Populate the Optimizations map with all the optimizations
1619/// we know.
1620void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001621 // String and Memory LibCall Optimizations
1622 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001623 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001624 Optimizations["strchr"] = &StrChr;
1625 Optimizations["strcmp"] = &StrCmp;
1626 Optimizations["strncmp"] = &StrNCmp;
1627 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001628 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001629 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001630 Optimizations["strtol"] = &StrTo;
1631 Optimizations["strtod"] = &StrTo;
1632 Optimizations["strtof"] = &StrTo;
1633 Optimizations["strtoul"] = &StrTo;
1634 Optimizations["strtoll"] = &StrTo;
1635 Optimizations["strtold"] = &StrTo;
1636 Optimizations["strtoull"] = &StrTo;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001637 Optimizations["memcmp"] = &MemCmp;
1638 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001639 Optimizations["memmove"] = &MemMove;
1640 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001641
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001642 // Math Library Optimizations
1643 Optimizations["powf"] = &Pow;
1644 Optimizations["pow"] = &Pow;
1645 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001646 Optimizations["llvm.pow.f32"] = &Pow;
1647 Optimizations["llvm.pow.f64"] = &Pow;
1648 Optimizations["llvm.pow.f80"] = &Pow;
1649 Optimizations["llvm.pow.f128"] = &Pow;
1650 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001651 Optimizations["exp2l"] = &Exp2;
1652 Optimizations["exp2"] = &Exp2;
1653 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001654 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1655 Optimizations["llvm.exp2.f128"] = &Exp2;
1656 Optimizations["llvm.exp2.f80"] = &Exp2;
1657 Optimizations["llvm.exp2.f64"] = &Exp2;
1658 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001659
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001660#ifdef HAVE_FLOORF
1661 Optimizations["floor"] = &UnaryDoubleFP;
1662#endif
1663#ifdef HAVE_CEILF
1664 Optimizations["ceil"] = &UnaryDoubleFP;
1665#endif
1666#ifdef HAVE_ROUNDF
1667 Optimizations["round"] = &UnaryDoubleFP;
1668#endif
1669#ifdef HAVE_RINTF
1670 Optimizations["rint"] = &UnaryDoubleFP;
1671#endif
1672#ifdef HAVE_NEARBYINTF
1673 Optimizations["nearbyint"] = &UnaryDoubleFP;
1674#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001675
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001676 // Integer Optimizations
1677 Optimizations["ffs"] = &FFS;
1678 Optimizations["ffsl"] = &FFS;
1679 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001680 Optimizations["abs"] = &Abs;
1681 Optimizations["labs"] = &Abs;
1682 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001683 Optimizations["isdigit"] = &IsDigit;
1684 Optimizations["isascii"] = &IsAscii;
1685 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001686
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001687 // Formatting and IO Optimizations
1688 Optimizations["sprintf"] = &SPrintF;
1689 Optimizations["printf"] = &PrintF;
1690 Optimizations["fwrite"] = &FWrite;
1691 Optimizations["fputs"] = &FPuts;
1692 Optimizations["fprintf"] = &FPrintF;
Eric Christopher7b5e6172009-10-27 00:52:25 +00001693
1694 // Miscellaneous
1695 Optimizations["llvm.objectsize"] = &ObjectSize;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001696}
1697
1698
1699/// runOnFunction - Top level algorithm.
1700///
1701bool SimplifyLibCalls::runOnFunction(Function &F) {
1702 if (Optimizations.empty())
1703 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001704
Dan Gohmanf14d9192009-08-18 00:48:13 +00001705 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001706
Owen Andersone922c022009-07-22 00:24:57 +00001707 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001708
1709 bool Changed = false;
1710 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1711 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1712 // Ignore non-calls.
1713 CallInst *CI = dyn_cast<CallInst>(I++);
1714 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001715
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001716 // Ignore indirect calls and calls to non-external functions.
1717 Function *Callee = CI->getCalledFunction();
1718 if (Callee == 0 || !Callee->isDeclaration() ||
1719 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1720 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001721
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001722 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001723 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1724 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001725
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001726 // Set the builder to the instruction after the call.
1727 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001728
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001729 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001730 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001731 if (Result == 0) continue;
1732
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001733 DEBUG(errs() << "SimplifyLibCalls simplified: " << *CI;
1734 errs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001735
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001736 // Something changed!
1737 Changed = true;
1738 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001739
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001740 // Inspect the instruction after the call (which was potentially just
1741 // added) next.
1742 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001743
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001744 if (CI != Result && !CI->use_empty()) {
1745 CI->replaceAllUsesWith(Result);
1746 if (!Result->hasName())
1747 Result->takeName(CI);
1748 }
1749 CI->eraseFromParent();
1750 }
1751 }
1752 return Changed;
1753}
1754
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001755// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001756
1757void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1758 if (!F.doesNotAccessMemory()) {
1759 F.setDoesNotAccessMemory();
1760 ++NumAnnotated;
1761 Modified = true;
1762 }
1763}
1764void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1765 if (!F.onlyReadsMemory()) {
1766 F.setOnlyReadsMemory();
1767 ++NumAnnotated;
1768 Modified = true;
1769 }
1770}
1771void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1772 if (!F.doesNotThrow()) {
1773 F.setDoesNotThrow();
1774 ++NumAnnotated;
1775 Modified = true;
1776 }
1777}
1778void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1779 if (!F.doesNotCapture(n)) {
1780 F.setDoesNotCapture(n);
1781 ++NumAnnotated;
1782 Modified = true;
1783 }
1784}
1785void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1786 if (!F.doesNotAlias(n)) {
1787 F.setDoesNotAlias(n);
1788 ++NumAnnotated;
1789 Modified = true;
1790 }
1791}
1792
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001793/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001794///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001795bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001796 Modified = false;
1797 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1798 Function &F = *I;
1799 if (!F.isDeclaration())
1800 continue;
1801
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001802 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001803 continue;
1804
1805 const FunctionType *FTy = F.getFunctionType();
1806
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001807 StringRef Name = F.getName();
1808 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001809 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001810 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001811 if (FTy->getNumParams() != 1 ||
1812 !isa<PointerType>(FTy->getParamType(0)))
1813 continue;
1814 setOnlyReadsMemory(F);
1815 setDoesNotThrow(F);
1816 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001817 } else if (Name == "strcpy" ||
1818 Name == "stpcpy" ||
1819 Name == "strcat" ||
1820 Name == "strtol" ||
1821 Name == "strtod" ||
1822 Name == "strtof" ||
1823 Name == "strtoul" ||
1824 Name == "strtoll" ||
1825 Name == "strtold" ||
1826 Name == "strncat" ||
1827 Name == "strncpy" ||
1828 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001829 if (FTy->getNumParams() < 2 ||
1830 !isa<PointerType>(FTy->getParamType(1)))
1831 continue;
1832 setDoesNotThrow(F);
1833 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001834 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001835 if (FTy->getNumParams() != 3 ||
1836 !isa<PointerType>(FTy->getParamType(0)) ||
1837 !isa<PointerType>(FTy->getParamType(1)))
1838 continue;
1839 setDoesNotThrow(F);
1840 setDoesNotCapture(F, 1);
1841 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001842 } else if (Name == "strcmp" ||
1843 Name == "strspn" ||
1844 Name == "strncmp" ||
1845 Name ==" strcspn" ||
1846 Name == "strcoll" ||
1847 Name == "strcasecmp" ||
1848 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001849 if (FTy->getNumParams() < 2 ||
1850 !isa<PointerType>(FTy->getParamType(0)) ||
1851 !isa<PointerType>(FTy->getParamType(1)))
1852 continue;
1853 setOnlyReadsMemory(F);
1854 setDoesNotThrow(F);
1855 setDoesNotCapture(F, 1);
1856 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001857 } else if (Name == "strstr" ||
1858 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001859 if (FTy->getNumParams() != 2 ||
1860 !isa<PointerType>(FTy->getParamType(1)))
1861 continue;
1862 setOnlyReadsMemory(F);
1863 setDoesNotThrow(F);
1864 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001865 } else if (Name == "strtok" ||
1866 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001867 if (FTy->getNumParams() < 2 ||
1868 !isa<PointerType>(FTy->getParamType(1)))
1869 continue;
1870 setDoesNotThrow(F);
1871 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001872 } else if (Name == "scanf" ||
1873 Name == "setbuf" ||
1874 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001875 if (FTy->getNumParams() < 1 ||
1876 !isa<PointerType>(FTy->getParamType(0)))
1877 continue;
1878 setDoesNotThrow(F);
1879 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001880 } else if (Name == "strdup" ||
1881 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001882 if (FTy->getNumParams() < 1 ||
1883 !isa<PointerType>(FTy->getReturnType()) ||
1884 !isa<PointerType>(FTy->getParamType(0)))
1885 continue;
1886 setDoesNotThrow(F);
1887 setDoesNotAlias(F, 0);
1888 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001889 } else if (Name == "stat" ||
1890 Name == "sscanf" ||
1891 Name == "sprintf" ||
1892 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001893 if (FTy->getNumParams() < 2 ||
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001894 !isa<PointerType>(FTy->getParamType(0)) ||
1895 !isa<PointerType>(FTy->getParamType(1)))
1896 continue;
1897 setDoesNotThrow(F);
1898 setDoesNotCapture(F, 1);
1899 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001900 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001901 if (FTy->getNumParams() != 3 ||
1902 !isa<PointerType>(FTy->getParamType(0)) ||
1903 !isa<PointerType>(FTy->getParamType(2)))
1904 continue;
1905 setDoesNotThrow(F);
1906 setDoesNotCapture(F, 1);
1907 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001908 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001909 if (FTy->getNumParams() != 3 ||
1910 !isa<PointerType>(FTy->getParamType(1)) ||
1911 !isa<PointerType>(FTy->getParamType(2)))
1912 continue;
1913 setDoesNotThrow(F);
1914 setDoesNotCapture(F, 2);
1915 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001916 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001917 if (FTy->getNumParams() != 1 ||
1918 !isa<PointerType>(FTy->getParamType(0)))
1919 continue;
1920 // May throw; "system" is a valid pthread cancellation point.
1921 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001922 }
1923 break;
1924 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001925 if (Name == "malloc") {
1926 if (FTy->getNumParams() != 1 ||
1927 !isa<PointerType>(FTy->getReturnType()))
1928 continue;
1929 setDoesNotThrow(F);
1930 setDoesNotAlias(F, 0);
1931 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001932 if (FTy->getNumParams() != 3 ||
1933 !isa<PointerType>(FTy->getParamType(0)) ||
1934 !isa<PointerType>(FTy->getParamType(1)))
1935 continue;
1936 setOnlyReadsMemory(F);
1937 setDoesNotThrow(F);
1938 setDoesNotCapture(F, 1);
1939 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001940 } else if (Name == "memchr" ||
1941 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001942 if (FTy->getNumParams() != 3)
1943 continue;
1944 setOnlyReadsMemory(F);
1945 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001946 } else if (Name == "modf" ||
1947 Name == "modff" ||
1948 Name == "modfl" ||
1949 Name == "memcpy" ||
1950 Name == "memccpy" ||
1951 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001952 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001953 !isa<PointerType>(FTy->getParamType(1)))
1954 continue;
1955 setDoesNotThrow(F);
1956 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001957 } else if (Name == "memalign") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001958 if (!isa<PointerType>(FTy->getReturnType()))
1959 continue;
1960 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001961 } else if (Name == "mkdir" ||
1962 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001963 if (FTy->getNumParams() == 0 ||
1964 !isa<PointerType>(FTy->getParamType(0)))
1965 continue;
1966 setDoesNotThrow(F);
1967 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001968 }
1969 break;
1970 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001971 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001972 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001973 !isa<PointerType>(FTy->getParamType(0)) ||
1974 !isa<PointerType>(FTy->getReturnType()))
1975 continue;
1976 setDoesNotThrow(F);
1977 setDoesNotAlias(F, 0);
1978 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001979 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001980 if (FTy->getNumParams() != 3 ||
1981 !isa<PointerType>(FTy->getParamType(1)))
1982 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001983 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001984 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001985 } else if (Name == "rmdir" ||
1986 Name == "rewind" ||
1987 Name == "remove" ||
1988 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001989 if (FTy->getNumParams() < 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001990 !isa<PointerType>(FTy->getParamType(0)))
1991 continue;
1992 setDoesNotThrow(F);
1993 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001994 } else if (Name == "rename" ||
1995 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001996 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001997 !isa<PointerType>(FTy->getParamType(0)) ||
1998 !isa<PointerType>(FTy->getParamType(1)))
1999 continue;
2000 setDoesNotThrow(F);
2001 setDoesNotCapture(F, 1);
2002 setDoesNotCapture(F, 2);
2003 }
2004 break;
2005 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002006 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002007 if (FTy->getNumParams() != 3 ||
2008 !isa<PointerType>(FTy->getParamType(1)))
2009 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002010 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002011 setDoesNotCapture(F, 2);
2012 }
2013 break;
2014 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002015 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002016 if (FTy->getNumParams() != 3 ||
2017 !isa<PointerType>(FTy->getParamType(0)) ||
2018 !isa<PointerType>(FTy->getParamType(1)))
2019 continue;
2020 setDoesNotThrow(F);
2021 setDoesNotCapture(F, 1);
2022 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002023 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002024 if (FTy->getNumParams() != 3 ||
2025 !isa<PointerType>(FTy->getParamType(0)) ||
2026 !isa<PointerType>(FTy->getParamType(1)))
2027 continue;
2028 setDoesNotThrow(F);
2029 setOnlyReadsMemory(F);
2030 setDoesNotCapture(F, 1);
2031 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002032 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002033 if (FTy->getNumParams() != 2 ||
2034 !isa<PointerType>(FTy->getParamType(0)))
2035 continue;
2036 setDoesNotThrow(F);
2037 setDoesNotCapture(F, 1);
2038 }
2039 break;
2040 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002041 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002042 if (FTy->getNumParams() != 2 ||
2043 !isa<PointerType>(FTy->getReturnType()))
2044 continue;
2045 setDoesNotThrow(F);
2046 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002047 } else if (Name == "chmod" ||
2048 Name == "chown" ||
2049 Name == "ctermid" ||
2050 Name == "clearerr" ||
2051 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002052 if (FTy->getNumParams() == 0 ||
2053 !isa<PointerType>(FTy->getParamType(0)))
2054 continue;
2055 setDoesNotThrow(F);
2056 setDoesNotCapture(F, 1);
2057 }
2058 break;
2059 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002060 if (Name == "atoi" ||
2061 Name == "atol" ||
2062 Name == "atof" ||
2063 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002064 if (FTy->getNumParams() != 1 ||
2065 !isa<PointerType>(FTy->getParamType(0)))
2066 continue;
2067 setDoesNotThrow(F);
2068 setOnlyReadsMemory(F);
2069 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002070 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002071 if (FTy->getNumParams() != 2 ||
2072 !isa<PointerType>(FTy->getParamType(0)))
2073 continue;
2074 setDoesNotThrow(F);
2075 setDoesNotCapture(F, 1);
2076 }
2077 break;
2078 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002079 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002080 if (FTy->getNumParams() != 2 ||
2081 !isa<PointerType>(FTy->getReturnType()) ||
2082 !isa<PointerType>(FTy->getParamType(0)) ||
2083 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002084 continue;
2085 setDoesNotThrow(F);
2086 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002087 setDoesNotCapture(F, 1);
2088 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002089 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002090 if (FTy->getNumParams() != 2 ||
2091 !isa<PointerType>(FTy->getReturnType()) ||
2092 !isa<PointerType>(FTy->getParamType(1)))
2093 continue;
2094 setDoesNotThrow(F);
2095 setDoesNotAlias(F, 0);
2096 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002097 } else if (Name == "feof" ||
2098 Name == "free" ||
2099 Name == "fseek" ||
2100 Name == "ftell" ||
2101 Name == "fgetc" ||
2102 Name == "fseeko" ||
2103 Name == "ftello" ||
2104 Name == "fileno" ||
2105 Name == "fflush" ||
2106 Name == "fclose" ||
2107 Name == "fsetpos" ||
2108 Name == "flockfile" ||
2109 Name == "funlockfile" ||
2110 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002111 if (FTy->getNumParams() == 0 ||
2112 !isa<PointerType>(FTy->getParamType(0)))
2113 continue;
2114 setDoesNotThrow(F);
2115 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002116 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002117 if (FTy->getNumParams() != 1 ||
2118 !isa<PointerType>(FTy->getParamType(0)))
2119 continue;
2120 setDoesNotThrow(F);
2121 setDoesNotCapture(F, 1);
2122 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002123 } else if (Name == "fputc" ||
2124 Name == "fstat" ||
2125 Name == "frexp" ||
2126 Name == "frexpf" ||
2127 Name == "frexpl" ||
2128 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002129 if (FTy->getNumParams() != 2 ||
2130 !isa<PointerType>(FTy->getParamType(1)))
2131 continue;
2132 setDoesNotThrow(F);
2133 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002134 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002135 if (FTy->getNumParams() != 3 ||
2136 !isa<PointerType>(FTy->getParamType(0)) ||
2137 !isa<PointerType>(FTy->getParamType(2)))
2138 continue;
2139 setDoesNotThrow(F);
2140 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002141 } else if (Name == "fread" ||
2142 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002143 if (FTy->getNumParams() != 4 ||
2144 !isa<PointerType>(FTy->getParamType(0)) ||
2145 !isa<PointerType>(FTy->getParamType(3)))
2146 continue;
2147 setDoesNotThrow(F);
2148 setDoesNotCapture(F, 1);
2149 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002150 } else if (Name == "fputs" ||
2151 Name == "fscanf" ||
2152 Name == "fprintf" ||
2153 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002154 if (FTy->getNumParams() < 2 ||
2155 !isa<PointerType>(FTy->getParamType(0)) ||
2156 !isa<PointerType>(FTy->getParamType(1)))
2157 continue;
2158 setDoesNotThrow(F);
2159 setDoesNotCapture(F, 1);
2160 setDoesNotCapture(F, 2);
2161 }
2162 break;
2163 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002164 if (Name == "getc" ||
2165 Name == "getlogin_r" ||
2166 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002167 if (FTy->getNumParams() == 0 ||
2168 !isa<PointerType>(FTy->getParamType(0)))
2169 continue;
2170 setDoesNotThrow(F);
2171 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002172 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002173 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002174 !isa<PointerType>(FTy->getParamType(0)))
2175 continue;
2176 setDoesNotThrow(F);
2177 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002178 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002179 } else if (Name == "gets" ||
2180 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002181 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002182 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002183 if (FTy->getNumParams() != 2 ||
2184 !isa<PointerType>(FTy->getParamType(1)))
2185 continue;
2186 setDoesNotThrow(F);
2187 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002188 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002189 if (FTy->getNumParams() != 1 ||
2190 !isa<PointerType>(FTy->getParamType(0)))
2191 continue;
2192 setDoesNotThrow(F);
2193 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002194 }
2195 break;
2196 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002197 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002198 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002199 !isa<PointerType>(FTy->getParamType(1)))
2200 continue;
2201 setDoesNotThrow(F);
2202 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002203 } else if (Name == "uname" ||
2204 Name == "unlink" ||
2205 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002206 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002207 !isa<PointerType>(FTy->getParamType(0)))
2208 continue;
2209 setDoesNotThrow(F);
2210 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002211 } else if (Name == "utime" ||
2212 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002213 if (FTy->getNumParams() != 2 ||
2214 !isa<PointerType>(FTy->getParamType(0)) ||
2215 !isa<PointerType>(FTy->getParamType(1)))
2216 continue;
2217 setDoesNotThrow(F);
2218 setDoesNotCapture(F, 1);
2219 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002220 }
2221 break;
2222 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002223 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002224 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002225 !isa<PointerType>(FTy->getParamType(1)))
2226 continue;
2227 setDoesNotThrow(F);
2228 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002229 } else if (Name == "puts" ||
2230 Name == "printf" ||
2231 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002232 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002233 !isa<PointerType>(FTy->getParamType(0)))
2234 continue;
2235 setDoesNotThrow(F);
2236 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002237 } else if (Name == "pread" ||
2238 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002239 if (FTy->getNumParams() != 4 ||
2240 !isa<PointerType>(FTy->getParamType(1)))
2241 continue;
2242 // May throw; these are valid pthread cancellation points.
2243 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002244 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002245 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002246 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002247 if (FTy->getNumParams() != 2 ||
2248 !isa<PointerType>(FTy->getReturnType()) ||
2249 !isa<PointerType>(FTy->getParamType(0)) ||
2250 !isa<PointerType>(FTy->getParamType(1)))
2251 continue;
2252 setDoesNotThrow(F);
2253 setDoesNotAlias(F, 0);
2254 setDoesNotCapture(F, 1);
2255 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002256 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002257 if (FTy->getNumParams() != 1 ||
2258 !isa<PointerType>(FTy->getParamType(0)))
2259 continue;
2260 setDoesNotThrow(F);
2261 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002262 }
2263 break;
2264 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002265 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002266 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002267 !isa<PointerType>(FTy->getParamType(1)))
2268 continue;
2269 setDoesNotThrow(F);
2270 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002271 } else if (Name == "vsscanf" ||
2272 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002273 if (FTy->getNumParams() != 3 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002274 !isa<PointerType>(FTy->getParamType(1)) ||
2275 !isa<PointerType>(FTy->getParamType(2)))
2276 continue;
2277 setDoesNotThrow(F);
2278 setDoesNotCapture(F, 1);
2279 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002280 } else if (Name == "valloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002281 if (!isa<PointerType>(FTy->getReturnType()))
2282 continue;
2283 setDoesNotThrow(F);
2284 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002285 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002286 if (FTy->getNumParams() != 2 ||
2287 !isa<PointerType>(FTy->getParamType(0)))
2288 continue;
2289 setDoesNotThrow(F);
2290 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002291 } else if (Name == "vfprintf" ||
2292 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002293 if (FTy->getNumParams() != 3 ||
2294 !isa<PointerType>(FTy->getParamType(0)) ||
2295 !isa<PointerType>(FTy->getParamType(1)))
2296 continue;
2297 setDoesNotThrow(F);
2298 setDoesNotCapture(F, 1);
2299 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002300 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002301 if (FTy->getNumParams() != 4 ||
2302 !isa<PointerType>(FTy->getParamType(0)) ||
2303 !isa<PointerType>(FTy->getParamType(2)))
2304 continue;
2305 setDoesNotThrow(F);
2306 setDoesNotCapture(F, 1);
2307 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002308 }
2309 break;
2310 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002311 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002312 if (FTy->getNumParams() < 2 ||
2313 !isa<PointerType>(FTy->getParamType(0)))
2314 continue;
2315 // May throw; "open" is a valid pthread cancellation point.
2316 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002317 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002318 if (FTy->getNumParams() != 1 ||
2319 !isa<PointerType>(FTy->getReturnType()) ||
2320 !isa<PointerType>(FTy->getParamType(0)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002321 continue;
2322 setDoesNotThrow(F);
2323 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002324 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002325 }
2326 break;
2327 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002328 if (Name == "tmpfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002329 if (!isa<PointerType>(FTy->getReturnType()))
2330 continue;
2331 setDoesNotThrow(F);
2332 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002333 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002334 if (FTy->getNumParams() != 1 ||
2335 !isa<PointerType>(FTy->getParamType(0)))
2336 continue;
2337 setDoesNotThrow(F);
2338 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002339 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002340 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002341 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002342 if (Name == "htonl" ||
2343 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002344 setDoesNotThrow(F);
2345 setDoesNotAccessMemory(F);
2346 }
2347 break;
2348 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002349 if (Name == "ntohl" ||
2350 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002351 setDoesNotThrow(F);
2352 setDoesNotAccessMemory(F);
2353 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002354 break;
2355 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002356 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002357 if (FTy->getNumParams() != 2 ||
2358 !isa<PointerType>(FTy->getParamType(0)) ||
2359 !isa<PointerType>(FTy->getParamType(1)))
2360 continue;
2361 setDoesNotThrow(F);
2362 setDoesNotCapture(F, 1);
2363 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002364 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002365 if (FTy->getNumParams() != 3 ||
2366 !isa<PointerType>(FTy->getParamType(0)))
2367 continue;
2368 setDoesNotThrow(F);
2369 setDoesNotCapture(F, 1);
2370 }
2371 break;
2372 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002373 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002374 if (FTy->getNumParams() != 4 ||
2375 !isa<PointerType>(FTy->getParamType(3)))
2376 continue;
2377 // May throw; places call through function pointer.
2378 setDoesNotCapture(F, 4);
2379 }
2380 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002381 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002382 if (Name == "__strdup" ||
2383 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002384 if (FTy->getNumParams() < 1 ||
2385 !isa<PointerType>(FTy->getReturnType()) ||
2386 !isa<PointerType>(FTy->getParamType(0)))
2387 continue;
2388 setDoesNotThrow(F);
2389 setDoesNotAlias(F, 0);
2390 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002391 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002392 if (FTy->getNumParams() != 3 ||
2393 !isa<PointerType>(FTy->getParamType(1)))
2394 continue;
2395 setDoesNotThrow(F);
2396 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002397 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002398 if (FTy->getNumParams() != 1 ||
2399 !isa<PointerType>(FTy->getParamType(0)))
2400 continue;
2401 setDoesNotThrow(F);
2402 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002403 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002404 if (FTy->getNumParams() != 2 ||
2405 !isa<PointerType>(FTy->getParamType(1)))
2406 continue;
2407 setDoesNotThrow(F);
2408 setDoesNotCapture(F, 2);
2409 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002410 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002411 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002412 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002413 if (FTy->getNumParams() < 1 ||
2414 !isa<PointerType>(FTy->getParamType(0)))
2415 continue;
2416 setDoesNotThrow(F);
2417 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002418 } else if (Name == "\1stat64" ||
2419 Name == "\1lstat64" ||
2420 Name == "\1statvfs64" ||
2421 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002422 if (FTy->getNumParams() < 1 ||
Nick Lewycky225f7472009-02-15 22:47:25 +00002423 !isa<PointerType>(FTy->getParamType(0)) ||
2424 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002425 continue;
2426 setDoesNotThrow(F);
2427 setDoesNotCapture(F, 1);
2428 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002429 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002430 if (FTy->getNumParams() != 2 ||
2431 !isa<PointerType>(FTy->getReturnType()) ||
2432 !isa<PointerType>(FTy->getParamType(0)) ||
2433 !isa<PointerType>(FTy->getParamType(1)))
2434 continue;
2435 setDoesNotThrow(F);
2436 setDoesNotAlias(F, 0);
2437 setDoesNotCapture(F, 1);
2438 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002439 } else if (Name == "\1fseeko64" ||
2440 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002441 if (FTy->getNumParams() == 0 ||
2442 !isa<PointerType>(FTy->getParamType(0)))
2443 continue;
2444 setDoesNotThrow(F);
2445 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002446 } else if (Name == "\1tmpfile64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002447 if (!isa<PointerType>(FTy->getReturnType()))
2448 continue;
2449 setDoesNotThrow(F);
2450 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002451 } else if (Name == "\1fstat64" ||
2452 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002453 if (FTy->getNumParams() != 2 ||
2454 !isa<PointerType>(FTy->getParamType(1)))
2455 continue;
2456 setDoesNotThrow(F);
2457 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002458 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002459 if (FTy->getNumParams() < 2 ||
2460 !isa<PointerType>(FTy->getParamType(0)))
2461 continue;
2462 // May throw; "open" is a valid pthread cancellation point.
2463 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002464 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002465 break;
2466 }
2467 }
2468 return Modified;
2469}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002470
2471// TODO:
2472// Additional cases that we need to add to this file:
2473//
2474// cbrt:
2475// * cbrt(expN(X)) -> expN(x/3)
2476// * cbrt(sqrt(x)) -> pow(x,1/6)
2477// * cbrt(sqrt(x)) -> pow(x,1/9)
2478//
2479// cos, cosf, cosl:
2480// * cos(-x) -> cos(x)
2481//
2482// exp, expf, expl:
2483// * exp(log(x)) -> x
2484//
2485// log, logf, logl:
2486// * log(exp(x)) -> x
2487// * log(x**y) -> y*log(x)
2488// * log(exp(y)) -> y*log(e)
2489// * log(exp2(y)) -> y*log(2)
2490// * log(exp10(y)) -> y*log(10)
2491// * log(sqrt(x)) -> 0.5*log(x)
2492// * log(pow(x,y)) -> y*log(x)
2493//
2494// lround, lroundf, lroundl:
2495// * lround(cnst) -> cnst'
2496//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002497// pow, powf, powl:
2498// * pow(exp(x),y) -> exp(x*y)
2499// * pow(sqrt(x),y) -> pow(x,y*0.5)
2500// * pow(pow(x,y),z)-> pow(x,y*z)
2501//
2502// puts:
2503// * puts("") -> putchar("\n")
2504//
2505// round, roundf, roundl:
2506// * round(cnst) -> cnst'
2507//
2508// signbit:
2509// * signbit(cnst) -> cnst'
2510// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2511//
2512// sqrt, sqrtf, sqrtl:
2513// * sqrt(expN(x)) -> expN(x*0.5)
2514// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2515// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2516//
2517// stpcpy:
2518// * stpcpy(str, "literal") ->
2519// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2520// strrchr:
2521// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2522// (if c is a constant integer and s is a constant string)
2523// * strrchr(s1,0) -> strchr(s1,0)
2524//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002525// strpbrk:
2526// * strpbrk(s,a) -> offset_in_for(s,a)
2527// (if s and a are both constant strings)
2528// * strpbrk(s,"") -> 0
2529// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2530//
2531// strspn, strcspn:
2532// * strspn(s,a) -> const_int (if both args are constant)
2533// * strspn("",a) -> 0
2534// * strspn(s,"") -> 0
2535// * strcspn(s,a) -> const_int (if both args are constant)
2536// * strcspn("",a) -> 0
2537// * strcspn(s,"") -> strlen(a)
2538//
2539// strstr:
2540// * strstr(x,x) -> x
2541// * strstr(s1,s2) -> offset_of_s2_in(s1)
2542// (if s1 and s2 are constant strings)
2543//
2544// tan, tanf, tanl:
2545// * tan(atan(x)) -> x
2546//
2547// trunc, truncf, truncl:
2548// * trunc(cnst) -> cnst'
2549//
2550//