blob: 611505ef363ae61202917fde845ee83068db2e0a [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),
Duncan Sandsf63c4102009-11-16 12:32:28 +0000262 /*isSigned*/true,
Eric Christopher37c8b862009-10-07 21:14:25 +0000263 "chari"),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000264 "putchar");
265
266 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
267 CI->setCallingConv(F->getCallingConv());
Chris Lattner74965f22009-11-09 04:57:04 +0000268 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000269}
270
271/// EmitPutS - Emit a call to the puts function. This assumes that Str is
272/// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000273void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000274 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000275 AttributeWithIndex AWI[2];
276 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
277 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
278
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000279 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000280 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000281 Type::getInt8PtrTy(*Context),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000282 NULL);
283 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
284 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
285 CI->setCallingConv(F->getCallingConv());
286
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000287}
288
289/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
290/// an integer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000291void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000292 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000293 AttributeWithIndex AWI[2];
294 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
295 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
296 Constant *F;
297 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000298 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
299 Type::getInt32Ty(*Context),
300 Type::getInt32Ty(*Context), File->getType(),
301 NULL);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000302 else
Eric Christopher37c8b862009-10-07 21:14:25 +0000303 F = M->getOrInsertFunction("fputc",
304 Type::getInt32Ty(*Context),
305 Type::getInt32Ty(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000306 File->getType(), NULL);
Duncan Sandsf63c4102009-11-16 12:32:28 +0000307 Char = B.CreateIntCast(Char, Type::getInt32Ty(*Context), /*isSigned*/true,
308 "chari");
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000309 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
310
311 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
312 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000313}
314
315/// EmitFPutS - Emit a call to the puts function. Str is required to be a
316/// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000317void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000318 Module *M = Caller->getParent();
Nick Lewycky225f7472009-02-15 22:47:25 +0000319 AttributeWithIndex AWI[3];
320 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
321 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
322 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000323 Constant *F;
324 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000325 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
326 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000327 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000328 File->getType(), NULL);
329 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000330 F = M->getOrInsertFunction("fputs", Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000331 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000332 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000333 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
334
335 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
336 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000337}
338
339/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
340/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
341void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Eric Christopher7a61d702008-08-08 19:39:37 +0000342 IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000343 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000344 AttributeWithIndex AWI[3];
345 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
346 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
347 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
348 Constant *F;
349 if (isa<PointerType>(File->getType()))
350 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000351 TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000352 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000353 TD->getIntPtrType(*Context),
354 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000355 File->getType(), NULL);
356 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000357 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000358 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000359 TD->getIntPtrType(*Context),
360 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000361 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000362 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Owen Anderson1d0be152009-08-13 21:58:54 +0000363 ConstantInt::get(TD->getIntPtrType(*Context), 1), File);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000364
365 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
366 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000367}
368
369//===----------------------------------------------------------------------===//
370// Helper Functions
371//===----------------------------------------------------------------------===//
372
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000373/// GetStringLengthH - If we can compute the length of the string pointed to by
374/// the specified pointer, return 'len+1'. If we can't, return 0.
375static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
376 // Look through noop bitcast instructions.
377 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
378 return GetStringLengthH(BCI->getOperand(0), PHIs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000379
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000380 // If this is a PHI node, there are two cases: either we have already seen it
381 // or we haven't.
382 if (PHINode *PN = dyn_cast<PHINode>(V)) {
383 if (!PHIs.insert(PN))
384 return ~0ULL; // already in the set.
Eric Christopher37c8b862009-10-07 21:14:25 +0000385
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000386 // If it was new, see if all the input strings are the same length.
387 uint64_t LenSoFar = ~0ULL;
388 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
389 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
390 if (Len == 0) return 0; // Unknown length -> unknown.
Eric Christopher37c8b862009-10-07 21:14:25 +0000391
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000392 if (Len == ~0ULL) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +0000393
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000394 if (Len != LenSoFar && LenSoFar != ~0ULL)
395 return 0; // Disagree -> unknown.
396 LenSoFar = Len;
397 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000398
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000399 // Success, all agree.
400 return LenSoFar;
401 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000402
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000403 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
404 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
405 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
406 if (Len1 == 0) return 0;
407 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
408 if (Len2 == 0) return 0;
409 if (Len1 == ~0ULL) return Len2;
410 if (Len2 == ~0ULL) return Len1;
411 if (Len1 != Len2) return 0;
412 return Len1;
413 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000414
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000415 // If the value is not a GEP instruction nor a constant expression with a
416 // GEP instruction, then return unknown.
417 User *GEP = 0;
418 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
419 GEP = GEPI;
420 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
421 if (CE->getOpcode() != Instruction::GetElementPtr)
422 return 0;
423 GEP = CE;
424 } else {
425 return 0;
426 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000427
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000428 // Make sure the GEP has exactly three arguments.
429 if (GEP->getNumOperands() != 3)
430 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000431
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000432 // Check to make sure that the first operand of the GEP is an integer and
433 // has value 0 so that we are sure we're indexing into the initializer.
434 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
435 if (!Idx->isZero())
436 return 0;
437 } else
438 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000439
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000440 // If the second index isn't a ConstantInt, then this is a variable index
441 // into the array. If this occurs, we can't say anything meaningful about
442 // the string.
443 uint64_t StartIdx = 0;
444 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
445 StartIdx = CI->getZExtValue();
446 else
447 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000448
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000449 // The GEP instruction, constant or instruction, must reference a global
450 // variable that is a constant and is initialized. The referenced constant
451 // initializer is the array that we'll use for optimization.
452 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
Dan Gohman107f41f2009-08-19 00:11:12 +0000453 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
454 GV->mayBeOverridden())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000455 return 0;
456 Constant *GlobalInit = GV->getInitializer();
Eric Christopher37c8b862009-10-07 21:14:25 +0000457
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000458 // Handle the ConstantAggregateZero case, which is a degenerate case. The
459 // initializer is constant zero so the length of the string must be zero.
460 if (isa<ConstantAggregateZero>(GlobalInit))
461 return 1; // Len = 0 offset by 1.
Eric Christopher37c8b862009-10-07 21:14:25 +0000462
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000463 // Must be a Constant Array
464 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
Owen Anderson1d0be152009-08-13 21:58:54 +0000465 if (!Array ||
466 Array->getType()->getElementType() != Type::getInt8Ty(V->getContext()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000467 return false;
Eric Christopher37c8b862009-10-07 21:14:25 +0000468
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000469 // Get the number of elements in the array
470 uint64_t NumElts = Array->getType()->getNumElements();
Eric Christopher37c8b862009-10-07 21:14:25 +0000471
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000472 // Traverse the constant array from StartIdx (derived above) which is
473 // the place the GEP refers to in the array.
474 for (unsigned i = StartIdx; i != NumElts; ++i) {
475 Constant *Elt = Array->getOperand(i);
476 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
477 if (!CI) // This array isn't suitable, non-int initializer.
478 return 0;
479 if (CI->isZero())
480 return i-StartIdx+1; // We found end of string, success!
481 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000482
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000483 return 0; // The array isn't null terminated, conservatively return 'unknown'.
484}
485
486/// GetStringLength - If we can compute the length of the string pointed to by
487/// the specified pointer, return 'len+1'. If we can't, return 0.
488static uint64_t GetStringLength(Value *V) {
489 if (!isa<PointerType>(V->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000490
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000491 SmallPtrSet<PHINode*, 32> PHIs;
492 uint64_t Len = GetStringLengthH(V, PHIs);
493 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
494 // an empty string as a length.
495 return Len == ~0ULL ? 1 : Len;
496}
497
498/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +0000499/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000500static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
501 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
502 UI != E; ++UI) {
503 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
504 if (IC->isEquality())
505 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
506 if (C->isNullValue())
507 continue;
508 // Unknown instruction.
509 return false;
510 }
511 return true;
512}
513
514//===----------------------------------------------------------------------===//
Eric Christopher7b5e6172009-10-27 00:52:25 +0000515// Miscellaneous LibCall/Intrinsic Optimizations
516//===----------------------------------------------------------------------===//
517
518namespace {
519struct SizeOpt : public LibCallOptimization {
520 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
521 // TODO: We can do more with this, but delaying to here should be no change
522 // in behavior.
523 ConstantInt *Const = dyn_cast<ConstantInt>(CI->getOperand(2));
524
525 if (!Const) return 0;
526
527 if (Const->getZExtValue() < 2)
528 return Constant::getAllOnesValue(Const->getType());
529 else
530 return ConstantInt::get(Const->getType(), 0);
531 }
532};
533}
534
535//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000536// String and Memory LibCall Optimizations
537//===----------------------------------------------------------------------===//
538
539//===---------------------------------------===//
540// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000541namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000542struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000543 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000544 // Verify the "strcat" function prototype.
545 const FunctionType *FT = Callee->getFunctionType();
546 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000547 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000548 FT->getParamType(0) != FT->getReturnType() ||
549 FT->getParamType(1) != FT->getReturnType())
550 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000551
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000552 // Extract some information from the instruction
553 Value *Dst = CI->getOperand(1);
554 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000555
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000556 // See if we can get the length of the input string.
557 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000558 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000559 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000560
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000561 // Handle the simple, do-nothing case: strcat(x, "") -> x
562 if (Len == 0)
563 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000564
565 // These optimizations require TargetData.
566 if (!TD) return 0;
567
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000568 EmitStrLenMemCpy(Src, Dst, Len, B);
569 return Dst;
570 }
571
572 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000573 // We need to find the end of the destination string. That's where the
574 // memory is to be moved to. We just generate a call to strlen.
575 Value *DstLen = EmitStrLen(Dst, B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000576
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000577 // Now that we have the destination's length, we must index into the
578 // destination's pointer to get the actual memcpy destination (end of
579 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000580 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000581
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000582 // We have enough information to now generate the memcpy call to do the
583 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000584 EmitMemCpy(CpyDst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000585 ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000586 }
587};
588
589//===---------------------------------------===//
590// 'strncat' Optimizations
591
Chris Lattner3e8b6632009-09-02 06:11:42 +0000592struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000593 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
594 // Verify the "strncat" function prototype.
595 const FunctionType *FT = Callee->getFunctionType();
596 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000597 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000598 FT->getParamType(0) != FT->getReturnType() ||
599 FT->getParamType(1) != FT->getReturnType() ||
600 !isa<IntegerType>(FT->getParamType(2)))
601 return 0;
602
603 // Extract some information from the instruction
604 Value *Dst = CI->getOperand(1);
605 Value *Src = CI->getOperand(2);
606 uint64_t Len;
607
608 // We don't do anything if length is not constant
609 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
610 Len = LengthArg->getZExtValue();
611 else
612 return 0;
613
614 // See if we can get the length of the input string.
615 uint64_t SrcLen = GetStringLength(Src);
616 if (SrcLen == 0) return 0;
617 --SrcLen; // Unbias length.
618
619 // Handle the simple, do-nothing cases:
620 // strncat(x, "", c) -> x
621 // strncat(x, c, 0) -> x
622 if (SrcLen == 0 || Len == 0) return Dst;
623
Dan Gohmanf14d9192009-08-18 00:48:13 +0000624 // These optimizations require TargetData.
625 if (!TD) return 0;
626
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000627 // We don't optimize this case
628 if (Len < SrcLen) return 0;
629
630 // strncat(x, s, c) -> strcat(x, s)
631 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000632 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000633 return Dst;
634 }
635};
636
637//===---------------------------------------===//
638// 'strchr' Optimizations
639
Chris Lattner3e8b6632009-09-02 06:11:42 +0000640struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000641 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000642 // Verify the "strchr" function prototype.
643 const FunctionType *FT = Callee->getFunctionType();
644 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000645 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000646 FT->getParamType(0) != FT->getReturnType())
647 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000648
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000649 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000650
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000651 // If the second operand is non-constant, see if we can compute the length
652 // of the input string and turn this into memchr.
653 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
654 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000655 // These optimizations require TargetData.
656 if (!TD) return 0;
657
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000658 uint64_t Len = GetStringLength(SrcStr);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000659 if (Len == 0 ||
660 FT->getParamType(1) != Type::getInt32Ty(*Context)) // memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000661 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000662
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000663 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Owen Anderson1d0be152009-08-13 21:58:54 +0000664 ConstantInt::get(TD->getIntPtrType(*Context), Len), B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000665 }
666
667 // Otherwise, the character is a constant, see if the first argument is
668 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000669 std::string Str;
670 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000671 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000672
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000673 // strchr can find the nul character.
674 Str += '\0';
675 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000676
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000677 // Compute the offset.
678 uint64_t i = 0;
679 while (1) {
680 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000681 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000682 // Did we find our match?
683 if (Str[i] == CharValue)
684 break;
685 ++i;
686 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000687
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000688 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000689 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000690 return B.CreateGEP(SrcStr, Idx, "strchr");
691 }
692};
693
694//===---------------------------------------===//
695// 'strcmp' Optimizations
696
Chris Lattner3e8b6632009-09-02 06:11:42 +0000697struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000698 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000699 // Verify the "strcmp" function prototype.
700 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000701 if (FT->getNumParams() != 2 ||
702 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000703 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000704 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000705 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000706
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000707 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
708 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000709 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000710
Bill Wendling0582ae92009-03-13 04:39:26 +0000711 std::string Str1, Str2;
712 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
713 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000714
Bill Wendling0582ae92009-03-13 04:39:26 +0000715 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000716 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000717
Bill Wendling0582ae92009-03-13 04:39:26 +0000718 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000719 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000720
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000721 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000722 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000723 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000724 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000725
726 // strcmp(P, "x") -> memcmp(P, "x", 2)
727 uint64_t Len1 = GetStringLength(Str1P);
728 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000729 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000730 // These optimizations require TargetData.
731 if (!TD) return 0;
732
Nick Lewycky13a09e22008-12-21 00:19:21 +0000733 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000734 ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattner849832c2009-06-19 04:17:36 +0000735 std::min(Len1, Len2)), B);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000736 }
737
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000738 return 0;
739 }
740};
741
742//===---------------------------------------===//
743// 'strncmp' Optimizations
744
Chris Lattner3e8b6632009-09-02 06:11:42 +0000745struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000746 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000747 // Verify the "strncmp" function prototype.
748 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000749 if (FT->getNumParams() != 3 ||
750 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000751 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000752 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000753 !isa<IntegerType>(FT->getParamType(2)))
754 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000755
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000756 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
757 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000758 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000759
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000760 // Get the length argument if it is constant.
761 uint64_t Length;
762 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
763 Length = LengthArg->getZExtValue();
764 else
765 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000766
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000767 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000768 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000769
Bill Wendling0582ae92009-03-13 04:39:26 +0000770 std::string Str1, Str2;
771 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
772 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000773
Bill Wendling0582ae92009-03-13 04:39:26 +0000774 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000775 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000776
Bill Wendling0582ae92009-03-13 04:39:26 +0000777 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000778 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000779
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000780 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000781 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000782 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000783 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000784 return 0;
785 }
786};
787
788
789//===---------------------------------------===//
790// 'strcpy' Optimizations
791
Chris Lattner3e8b6632009-09-02 06:11:42 +0000792struct StrCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000793 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000794 // Verify the "strcpy" function prototype.
795 const FunctionType *FT = Callee->getFunctionType();
796 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
797 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000798 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000799 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000800
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000801 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
802 if (Dst == Src) // strcpy(x,x) -> x
803 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000804
Dan Gohmanf14d9192009-08-18 00:48:13 +0000805 // These optimizations require TargetData.
806 if (!TD) return 0;
807
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000808 // See if we can get the length of the input string.
809 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000810 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000811
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000812 // We have enough information to now generate the memcpy call to do the
813 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000814 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000815 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000816 return Dst;
817 }
818};
819
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000820//===---------------------------------------===//
821// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000822
Chris Lattner3e8b6632009-09-02 06:11:42 +0000823struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000824 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
825 const FunctionType *FT = Callee->getFunctionType();
826 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
827 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000828 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000829 !isa<IntegerType>(FT->getParamType(2)))
830 return 0;
831
832 Value *Dst = CI->getOperand(1);
833 Value *Src = CI->getOperand(2);
834 Value *LenOp = CI->getOperand(3);
835
836 // See if we can get the length of the input string.
837 uint64_t SrcLen = GetStringLength(Src);
838 if (SrcLen == 0) return 0;
839 --SrcLen;
840
841 if (SrcLen == 0) {
842 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000843 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
844 B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000845 return Dst;
846 }
847
848 uint64_t Len;
849 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
850 Len = LengthArg->getZExtValue();
851 else
852 return 0;
853
854 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
855
Dan Gohmanf14d9192009-08-18 00:48:13 +0000856 // These optimizations require TargetData.
857 if (!TD) return 0;
858
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000859 // Let strncpy handle the zero padding
860 if (Len > SrcLen+1) return 0;
861
862 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000863 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000864 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000865
866 return Dst;
867 }
868};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000869
870//===---------------------------------------===//
871// 'strlen' Optimizations
872
Chris Lattner3e8b6632009-09-02 06:11:42 +0000873struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000874 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000875 const FunctionType *FT = Callee->getFunctionType();
876 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000877 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000878 !isa<IntegerType>(FT->getReturnType()))
879 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000880
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000881 Value *Src = CI->getOperand(1);
882
883 // Constant folding: strlen("xyz") -> 3
884 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000885 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000886
887 // Handle strlen(p) != 0.
888 if (!IsOnlyUsedInZeroEqualityComparison(CI)) return 0;
889
890 // strlen(x) != 0 --> *x != 0
891 // strlen(x) == 0 --> *x == 0
892 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
893 }
894};
895
896//===---------------------------------------===//
Nick Lewycky4c498412009-02-13 15:31:46 +0000897// 'strto*' Optimizations
898
Chris Lattner3e8b6632009-09-02 06:11:42 +0000899struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000900 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
901 const FunctionType *FT = Callee->getFunctionType();
902 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
903 !isa<PointerType>(FT->getParamType(0)) ||
904 !isa<PointerType>(FT->getParamType(1)))
905 return 0;
906
907 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000908 if (isa<ConstantPointerNull>(EndPtr)) {
909 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000910 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000911 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000912
913 return 0;
914 }
915};
916
917
918//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000919// 'memcmp' Optimizations
920
Chris Lattner3e8b6632009-09-02 06:11:42 +0000921struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000922 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000923 const FunctionType *FT = Callee->getFunctionType();
924 if (FT->getNumParams() != 3 || !isa<PointerType>(FT->getParamType(0)) ||
925 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000926 FT->getReturnType() != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000927 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000928
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000929 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000930
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000931 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000932 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000933
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000934 // Make sure we have a constant length.
935 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000936 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000937 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000938
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000939 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000940 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000941
942 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
943 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
944 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +0000945 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000946 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000947
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000948 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS ^ *(short*)RHS) != 0
949 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS ^ *(int*)RHS) != 0
950 if ((Len == 2 || Len == 4) && IsOnlyUsedInZeroEqualityComparison(CI)) {
Owen Andersondebcb012009-07-29 22:17:13 +0000951 const Type *PTy = PointerType::getUnqual(Len == 2 ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000952 Type::getInt16Ty(*Context) : Type::getInt32Ty(*Context));
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000953 LHS = B.CreateBitCast(LHS, PTy, "tmp");
954 RHS = B.CreateBitCast(RHS, PTy, "tmp");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000955 LoadInst *LHSV = B.CreateLoad(LHS, "lhsv");
956 LoadInst *RHSV = B.CreateLoad(RHS, "rhsv");
957 LHSV->setAlignment(1); RHSV->setAlignment(1); // Unaligned loads.
958 return B.CreateZExt(B.CreateXor(LHSV, RHSV, "shortdiff"), CI->getType());
959 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000960
Benjamin Kramer992a6372009-11-05 17:44:22 +0000961 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
962 std::string LHSStr, RHSStr;
963 if (GetConstantStringInfo(LHS, LHSStr) &&
964 GetConstantStringInfo(RHS, RHSStr)) {
965 // Make sure we're not reading out-of-bounds memory.
966 if (Len > LHSStr.length() || Len > RHSStr.length())
967 return 0;
968 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
969 return ConstantInt::get(CI->getType(), Ret);
970 }
971
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000972 return 0;
973 }
974};
975
976//===---------------------------------------===//
977// 'memcpy' Optimizations
978
Chris Lattner3e8b6632009-09-02 06:11:42 +0000979struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000980 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000981 // These optimizations require TargetData.
982 if (!TD) return 0;
983
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000984 const FunctionType *FT = Callee->getFunctionType();
985 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
986 !isa<PointerType>(FT->getParamType(0)) ||
987 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000988 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000989 return 0;
990
991 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
992 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
993 return CI->getOperand(1);
994 }
995};
996
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000997//===---------------------------------------===//
998// 'memmove' Optimizations
999
Chris Lattner3e8b6632009-09-02 06:11:42 +00001000struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001001 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001002 // These optimizations require TargetData.
1003 if (!TD) return 0;
1004
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001005 const FunctionType *FT = Callee->getFunctionType();
1006 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1007 !isa<PointerType>(FT->getParamType(0)) ||
1008 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001009 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001010 return 0;
1011
1012 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
1013 Module *M = Caller->getParent();
1014 Intrinsic::ID IID = Intrinsic::memmove;
1015 const Type *Tys[1];
Owen Anderson1d0be152009-08-13 21:58:54 +00001016 Tys[0] = TD->getIntPtrType(*Context);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001017 Value *MemMove = Intrinsic::getDeclaration(M, IID, Tys, 1);
1018 Value *Dst = CastToCStr(CI->getOperand(1), B);
1019 Value *Src = CastToCStr(CI->getOperand(2), B);
1020 Value *Size = CI->getOperand(3);
Owen Anderson1d0be152009-08-13 21:58:54 +00001021 Value *Align = ConstantInt::get(Type::getInt32Ty(*Context), 1);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001022 B.CreateCall4(MemMove, Dst, Src, Size, Align);
1023 return CI->getOperand(1);
1024 }
1025};
1026
1027//===---------------------------------------===//
1028// 'memset' Optimizations
1029
Chris Lattner3e8b6632009-09-02 06:11:42 +00001030struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001031 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001032 // These optimizations require TargetData.
1033 if (!TD) return 0;
1034
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001035 const FunctionType *FT = Callee->getFunctionType();
1036 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1037 !isa<PointerType>(FT->getParamType(0)) ||
Eli Friedman62bb4132009-07-18 08:34:51 +00001038 !isa<IntegerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001039 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001040 return 0;
1041
1042 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +00001043 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1044 false);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001045 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001046 return CI->getOperand(1);
1047 }
1048};
1049
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001050//===----------------------------------------------------------------------===//
1051// Math Library Optimizations
1052//===----------------------------------------------------------------------===//
1053
1054//===---------------------------------------===//
1055// 'pow*' Optimizations
1056
Chris Lattner3e8b6632009-09-02 06:11:42 +00001057struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001058 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001059 const FunctionType *FT = Callee->getFunctionType();
1060 // Just make sure this has 2 arguments of the same FP type, which match the
1061 // result type.
1062 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1063 FT->getParamType(0) != FT->getParamType(1) ||
1064 !FT->getParamType(0)->isFloatingPoint())
1065 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001066
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001067 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
1068 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1069 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
1070 return Op1C;
1071 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +00001072 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001073 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001074
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001075 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1076 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001077
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001078 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001079 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001080
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001081 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +00001082 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1083 // This is faster than calling pow, and still handles negative zero
1084 // and negative infinite correctly.
1085 // TODO: In fast-math mode, this could be just sqrt(x).
1086 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +00001087 Value *Inf = ConstantFP::getInfinity(CI->getType());
1088 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +00001089 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1090 Callee->getAttributes());
1091 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1092 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +00001093 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
1094 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
1095 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001096 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001097
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001098 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1099 return Op1;
1100 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001101 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001102 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001103 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001104 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001105 return 0;
1106 }
1107};
1108
1109//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +00001110// 'exp2' Optimizations
1111
Chris Lattner3e8b6632009-09-02 06:11:42 +00001112struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001113 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +00001114 const FunctionType *FT = Callee->getFunctionType();
1115 // Just make sure this has 1 argument of FP type, which matches the
1116 // result type.
1117 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1118 !FT->getParamType(0)->isFloatingPoint())
1119 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001120
Chris Lattnere818f772008-05-02 18:43:35 +00001121 Value *Op = CI->getOperand(1);
1122 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1123 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1124 Value *LdExpArg = 0;
1125 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1126 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001127 LdExpArg = B.CreateSExt(OpC->getOperand(0),
1128 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001129 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1130 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001131 LdExpArg = B.CreateZExt(OpC->getOperand(0),
1132 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001133 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001134
Chris Lattnere818f772008-05-02 18:43:35 +00001135 if (LdExpArg) {
1136 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001137 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001138 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001139 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001140 Name = "ldexp";
1141 else
1142 Name = "ldexpl";
1143
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001144 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001145 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001146 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +00001147
1148 Module *M = Caller->getParent();
1149 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +00001150 Op->getType(),
1151 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001152 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1153 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1154 CI->setCallingConv(F->getCallingConv());
1155
1156 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +00001157 }
1158 return 0;
1159 }
1160};
Chris Lattnere818f772008-05-02 18:43:35 +00001161
1162//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001163// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1164
Chris Lattner3e8b6632009-09-02 06:11:42 +00001165struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001166 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001167 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001168 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1169 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001170 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001171
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001172 // If this is something like 'floor((double)floatval)', convert to floorf.
1173 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001174 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001175 return 0;
1176
1177 // floor((double)floatval) -> (double)floorf(floatval)
1178 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +00001179 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
1180 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +00001181 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001182 }
1183};
1184
1185//===----------------------------------------------------------------------===//
1186// Integer Optimizations
1187//===----------------------------------------------------------------------===//
1188
1189//===---------------------------------------===//
1190// 'ffs*' Optimizations
1191
Chris Lattner3e8b6632009-09-02 06:11:42 +00001192struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001193 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001194 const FunctionType *FT = Callee->getFunctionType();
1195 // Just make sure this has 2 arguments of the same FP type, which match the
1196 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +00001197 if (FT->getNumParams() != 1 ||
1198 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001199 !isa<IntegerType>(FT->getParamType(0)))
1200 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001201
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001202 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001203
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001204 // Constant fold.
1205 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1206 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +00001207 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +00001208 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001209 CI->getValue().countTrailingZeros()+1);
1210 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001211
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001212 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1213 const Type *ArgType = Op->getType();
1214 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1215 Intrinsic::cttz, &ArgType, 1);
1216 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +00001217 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +00001218 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001219
Owen Andersona7235ea2009-07-31 20:28:14 +00001220 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001221 return B.CreateSelect(Cond, V,
1222 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001223 }
1224};
1225
1226//===---------------------------------------===//
1227// 'isdigit' Optimizations
1228
Chris Lattner3e8b6632009-09-02 06:11:42 +00001229struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001230 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001231 const FunctionType *FT = Callee->getFunctionType();
1232 // We require integer(i32)
1233 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001234 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001235 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001236
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001237 // isdigit(c) -> (c-'0') <u 10
1238 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001239 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001240 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001241 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001242 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001243 return B.CreateZExt(Op, CI->getType());
1244 }
1245};
1246
1247//===---------------------------------------===//
1248// 'isascii' Optimizations
1249
Chris Lattner3e8b6632009-09-02 06:11:42 +00001250struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001251 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001252 const FunctionType *FT = Callee->getFunctionType();
1253 // We require integer(i32)
1254 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001255 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001256 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001257
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001258 // isascii(c) -> c <u 128
1259 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001260 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001261 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001262 return B.CreateZExt(Op, CI->getType());
1263 }
1264};
Eric Christopher37c8b862009-10-07 21:14:25 +00001265
Chris Lattner313f0e62008-06-09 08:26:51 +00001266//===---------------------------------------===//
1267// 'abs', 'labs', 'llabs' Optimizations
1268
Chris Lattner3e8b6632009-09-02 06:11:42 +00001269struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001270 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001271 const FunctionType *FT = Callee->getFunctionType();
1272 // We require integer(integer) where the types agree.
1273 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
1274 FT->getParamType(0) != FT->getReturnType())
1275 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001276
Chris Lattner313f0e62008-06-09 08:26:51 +00001277 // abs(x) -> x >s -1 ? x : -x
1278 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001279 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001280 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001281 "ispos");
1282 Value *Neg = B.CreateNeg(Op, "neg");
1283 return B.CreateSelect(Pos, Op, Neg);
1284 }
1285};
Eric Christopher37c8b862009-10-07 21:14:25 +00001286
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001287
1288//===---------------------------------------===//
1289// 'toascii' Optimizations
1290
Chris Lattner3e8b6632009-09-02 06:11:42 +00001291struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001292 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001293 const FunctionType *FT = Callee->getFunctionType();
1294 // We require i32(i32)
1295 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001296 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001297 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001298
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001299 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001300 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +00001301 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001302 }
1303};
1304
1305//===----------------------------------------------------------------------===//
1306// Formatting and IO Optimizations
1307//===----------------------------------------------------------------------===//
1308
1309//===---------------------------------------===//
1310// 'printf' Optimizations
1311
Chris Lattner3e8b6632009-09-02 06:11:42 +00001312struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001313 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001314 // Require one fixed pointer argument and an integer/void result.
1315 const FunctionType *FT = Callee->getFunctionType();
1316 if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
1317 !(isa<IntegerType>(FT->getReturnType()) ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001318 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001319 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001320
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001321 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001322 std::string FormatStr;
1323 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1324 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001325
1326 // Empty format string -> noop.
1327 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001328 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001329 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001330
Chris Lattner74965f22009-11-09 04:57:04 +00001331 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1332 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001333 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001334 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
1335 FormatStr[0]), B);
1336 if (CI->use_empty()) return CI;
1337 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001338 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001339
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001340 // printf("foo\n") --> puts("foo")
1341 if (FormatStr[FormatStr.size()-1] == '\n' &&
1342 FormatStr.find('%') == std::string::npos) { // no format characters.
1343 // Create a string literal with no \n on it. We expect the constant merge
1344 // pass to be run after this pass, to merge duplicate strings.
1345 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001346 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001347 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1348 GlobalVariable::InternalLinkage, C, "str");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001349 EmitPutS(C, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001350 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001351 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001352 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001353
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001354 // Optimize specific format strings.
1355 // printf("%c", chr) --> putchar(*(i8*)dst)
1356 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
1357 isa<IntegerType>(CI->getOperand(2)->getType())) {
Chris Lattner74965f22009-11-09 04:57:04 +00001358 Value *Res = EmitPutChar(CI->getOperand(2), B);
1359
1360 if (CI->use_empty()) return CI;
1361 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001362 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001363
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001364 // printf("%s\n", str) --> puts(str)
1365 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
1366 isa<PointerType>(CI->getOperand(2)->getType()) &&
1367 CI->use_empty()) {
1368 EmitPutS(CI->getOperand(2), B);
1369 return CI;
1370 }
1371 return 0;
1372 }
1373};
1374
1375//===---------------------------------------===//
1376// 'sprintf' Optimizations
1377
Chris Lattner3e8b6632009-09-02 06:11:42 +00001378struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001379 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001380 // Require two fixed pointer arguments and an integer result.
1381 const FunctionType *FT = Callee->getFunctionType();
1382 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1383 !isa<PointerType>(FT->getParamType(1)) ||
1384 !isa<IntegerType>(FT->getReturnType()))
1385 return 0;
1386
1387 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001388 std::string FormatStr;
1389 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1390 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001391
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001392 // If we just have a format string (nothing else crazy) transform it.
1393 if (CI->getNumOperands() == 3) {
1394 // Make sure there's no % in the constant array. We could try to handle
1395 // %% -> % in the future if we cared.
1396 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1397 if (FormatStr[i] == '%')
1398 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001399
1400 // These optimizations require TargetData.
1401 if (!TD) return 0;
1402
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001403 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1404 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Owen Anderson1d0be152009-08-13 21:58:54 +00001405 ConstantInt::get(TD->getIntPtrType(*Context), FormatStr.size()+1),1,B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001406 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001407 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001408
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001409 // The remaining optimizations require the format string to be "%s" or "%c"
1410 // and have an extra operand.
1411 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1412 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001413
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001414 // Decode the second character of the format string.
1415 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001416 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001417 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001418 Value *V = B.CreateTrunc(CI->getOperand(3),
1419 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001420 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1421 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001422 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1423 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001424 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001425
Owen Andersoneed707b2009-07-24 23:12:02 +00001426 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001427 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001428
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001429 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001430 // These optimizations require TargetData.
1431 if (!TD) return 0;
1432
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001433 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1434 if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
1435
1436 Value *Len = EmitStrLen(CI->getOperand(3), B);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001437 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001438 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001439 "leninc");
1440 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001441
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001442 // The sprintf result is the unincremented number of bytes in the string.
1443 return B.CreateIntCast(Len, CI->getType(), false);
1444 }
1445 return 0;
1446 }
1447};
1448
1449//===---------------------------------------===//
1450// 'fwrite' Optimizations
1451
Chris Lattner3e8b6632009-09-02 06:11:42 +00001452struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001453 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001454 // Require a pointer, an integer, an integer, a pointer, returning integer.
1455 const FunctionType *FT = Callee->getFunctionType();
1456 if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
1457 !isa<IntegerType>(FT->getParamType(1)) ||
1458 !isa<IntegerType>(FT->getParamType(2)) ||
1459 !isa<PointerType>(FT->getParamType(3)) ||
1460 !isa<IntegerType>(FT->getReturnType()))
1461 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001462
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001463 // Get the element size and count.
1464 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1465 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1466 if (!SizeC || !CountC) return 0;
1467 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001468
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001469 // If this is writing zero records, remove the call (it's a noop).
1470 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001471 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001472
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001473 // If this is writing one byte, turn it into fputc.
1474 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1475 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1476 EmitFPutC(Char, CI->getOperand(4), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001477 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001478 }
1479
1480 return 0;
1481 }
1482};
1483
1484//===---------------------------------------===//
1485// 'fputs' Optimizations
1486
Chris Lattner3e8b6632009-09-02 06:11:42 +00001487struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001488 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001489 // These optimizations require TargetData.
1490 if (!TD) return 0;
1491
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001492 // Require two pointers. Also, we can't optimize if return value is used.
1493 const FunctionType *FT = Callee->getFunctionType();
1494 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1495 !isa<PointerType>(FT->getParamType(1)) ||
1496 !CI->use_empty())
1497 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001498
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001499 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1500 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001501 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001502 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001503 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001504 CI->getOperand(2), B);
1505 return CI; // Known to have no uses (see above).
1506 }
1507};
1508
1509//===---------------------------------------===//
1510// 'fprintf' Optimizations
1511
Chris Lattner3e8b6632009-09-02 06:11:42 +00001512struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001513 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001514 // Require two fixed paramters as pointers and integer result.
1515 const FunctionType *FT = Callee->getFunctionType();
1516 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1517 !isa<PointerType>(FT->getParamType(1)) ||
1518 !isa<IntegerType>(FT->getReturnType()))
1519 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001520
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001521 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001522 std::string FormatStr;
1523 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1524 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001525
1526 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1527 if (CI->getNumOperands() == 3) {
1528 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1529 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001530 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001531
1532 // These optimizations require TargetData.
1533 if (!TD) return 0;
1534
Owen Anderson1d0be152009-08-13 21:58:54 +00001535 EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001536 FormatStr.size()),
1537 CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001538 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001539 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001540
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001541 // The remaining optimizations require the format string to be "%s" or "%c"
1542 // and have an extra operand.
1543 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1544 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001545
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001546 // Decode the second character of the format string.
1547 if (FormatStr[1] == 'c') {
1548 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1549 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1550 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001551 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001552 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001553
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001554 if (FormatStr[1] == 's') {
1555 // fprintf(F, "%s", str) -> fputs(str, F)
1556 if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
1557 return 0;
1558 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1559 return CI;
1560 }
1561 return 0;
1562 }
1563};
1564
Bill Wendlingac178222008-05-05 21:37:59 +00001565} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001566
1567//===----------------------------------------------------------------------===//
1568// SimplifyLibCalls Pass Implementation
1569//===----------------------------------------------------------------------===//
1570
1571namespace {
1572 /// This pass optimizes well known library functions from libc and libm.
1573 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001574 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001575 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001576 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001577 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1578 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
1579 StrToOpt StrTo; MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove;
1580 MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001581 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001582 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001583 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001584 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1585 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001586 // Formatting and IO Optimizations
1587 SPrintFOpt SPrintF; PrintFOpt PrintF;
1588 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher7b5e6172009-10-27 00:52:25 +00001589 SizeOpt ObjectSize;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001590
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001591 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001592 public:
1593 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001594 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001595
1596 void InitOptimizations();
1597 bool runOnFunction(Function &F);
1598
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001599 void setDoesNotAccessMemory(Function &F);
1600 void setOnlyReadsMemory(Function &F);
1601 void setDoesNotThrow(Function &F);
1602 void setDoesNotCapture(Function &F, unsigned n);
1603 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001604 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001605
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001606 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001607 }
1608 };
1609 char SimplifyLibCalls::ID = 0;
1610} // end anonymous namespace.
1611
1612static RegisterPass<SimplifyLibCalls>
1613X("simplify-libcalls", "Simplify well-known library calls");
1614
1615// Public interface to the Simplify LibCalls pass.
1616FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001617 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001618}
1619
1620/// Optimizations - Populate the Optimizations map with all the optimizations
1621/// we know.
1622void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001623 // String and Memory LibCall Optimizations
1624 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001625 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001626 Optimizations["strchr"] = &StrChr;
1627 Optimizations["strcmp"] = &StrCmp;
1628 Optimizations["strncmp"] = &StrNCmp;
1629 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001630 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001631 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001632 Optimizations["strtol"] = &StrTo;
1633 Optimizations["strtod"] = &StrTo;
1634 Optimizations["strtof"] = &StrTo;
1635 Optimizations["strtoul"] = &StrTo;
1636 Optimizations["strtoll"] = &StrTo;
1637 Optimizations["strtold"] = &StrTo;
1638 Optimizations["strtoull"] = &StrTo;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001639 Optimizations["memcmp"] = &MemCmp;
1640 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001641 Optimizations["memmove"] = &MemMove;
1642 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001643
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001644 // Math Library Optimizations
1645 Optimizations["powf"] = &Pow;
1646 Optimizations["pow"] = &Pow;
1647 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001648 Optimizations["llvm.pow.f32"] = &Pow;
1649 Optimizations["llvm.pow.f64"] = &Pow;
1650 Optimizations["llvm.pow.f80"] = &Pow;
1651 Optimizations["llvm.pow.f128"] = &Pow;
1652 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001653 Optimizations["exp2l"] = &Exp2;
1654 Optimizations["exp2"] = &Exp2;
1655 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001656 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1657 Optimizations["llvm.exp2.f128"] = &Exp2;
1658 Optimizations["llvm.exp2.f80"] = &Exp2;
1659 Optimizations["llvm.exp2.f64"] = &Exp2;
1660 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001661
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001662#ifdef HAVE_FLOORF
1663 Optimizations["floor"] = &UnaryDoubleFP;
1664#endif
1665#ifdef HAVE_CEILF
1666 Optimizations["ceil"] = &UnaryDoubleFP;
1667#endif
1668#ifdef HAVE_ROUNDF
1669 Optimizations["round"] = &UnaryDoubleFP;
1670#endif
1671#ifdef HAVE_RINTF
1672 Optimizations["rint"] = &UnaryDoubleFP;
1673#endif
1674#ifdef HAVE_NEARBYINTF
1675 Optimizations["nearbyint"] = &UnaryDoubleFP;
1676#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001677
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001678 // Integer Optimizations
1679 Optimizations["ffs"] = &FFS;
1680 Optimizations["ffsl"] = &FFS;
1681 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001682 Optimizations["abs"] = &Abs;
1683 Optimizations["labs"] = &Abs;
1684 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001685 Optimizations["isdigit"] = &IsDigit;
1686 Optimizations["isascii"] = &IsAscii;
1687 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001688
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001689 // Formatting and IO Optimizations
1690 Optimizations["sprintf"] = &SPrintF;
1691 Optimizations["printf"] = &PrintF;
1692 Optimizations["fwrite"] = &FWrite;
1693 Optimizations["fputs"] = &FPuts;
1694 Optimizations["fprintf"] = &FPrintF;
Eric Christopher7b5e6172009-10-27 00:52:25 +00001695
1696 // Miscellaneous
1697 Optimizations["llvm.objectsize"] = &ObjectSize;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001698}
1699
1700
1701/// runOnFunction - Top level algorithm.
1702///
1703bool SimplifyLibCalls::runOnFunction(Function &F) {
1704 if (Optimizations.empty())
1705 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001706
Dan Gohmanf14d9192009-08-18 00:48:13 +00001707 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001708
Owen Andersone922c022009-07-22 00:24:57 +00001709 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001710
1711 bool Changed = false;
1712 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1713 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1714 // Ignore non-calls.
1715 CallInst *CI = dyn_cast<CallInst>(I++);
1716 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001717
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001718 // Ignore indirect calls and calls to non-external functions.
1719 Function *Callee = CI->getCalledFunction();
1720 if (Callee == 0 || !Callee->isDeclaration() ||
1721 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1722 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001723
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001724 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001725 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1726 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001727
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001728 // Set the builder to the instruction after the call.
1729 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001730
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001731 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001732 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001733 if (Result == 0) continue;
1734
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001735 DEBUG(errs() << "SimplifyLibCalls simplified: " << *CI;
1736 errs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001737
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001738 // Something changed!
1739 Changed = true;
1740 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001741
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001742 // Inspect the instruction after the call (which was potentially just
1743 // added) next.
1744 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001745
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001746 if (CI != Result && !CI->use_empty()) {
1747 CI->replaceAllUsesWith(Result);
1748 if (!Result->hasName())
1749 Result->takeName(CI);
1750 }
1751 CI->eraseFromParent();
1752 }
1753 }
1754 return Changed;
1755}
1756
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001757// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001758
1759void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1760 if (!F.doesNotAccessMemory()) {
1761 F.setDoesNotAccessMemory();
1762 ++NumAnnotated;
1763 Modified = true;
1764 }
1765}
1766void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1767 if (!F.onlyReadsMemory()) {
1768 F.setOnlyReadsMemory();
1769 ++NumAnnotated;
1770 Modified = true;
1771 }
1772}
1773void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1774 if (!F.doesNotThrow()) {
1775 F.setDoesNotThrow();
1776 ++NumAnnotated;
1777 Modified = true;
1778 }
1779}
1780void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1781 if (!F.doesNotCapture(n)) {
1782 F.setDoesNotCapture(n);
1783 ++NumAnnotated;
1784 Modified = true;
1785 }
1786}
1787void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1788 if (!F.doesNotAlias(n)) {
1789 F.setDoesNotAlias(n);
1790 ++NumAnnotated;
1791 Modified = true;
1792 }
1793}
1794
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001795/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001796///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001797bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001798 Modified = false;
1799 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1800 Function &F = *I;
1801 if (!F.isDeclaration())
1802 continue;
1803
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001804 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001805 continue;
1806
1807 const FunctionType *FTy = F.getFunctionType();
1808
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001809 StringRef Name = F.getName();
1810 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001811 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001812 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001813 if (FTy->getNumParams() != 1 ||
1814 !isa<PointerType>(FTy->getParamType(0)))
1815 continue;
1816 setOnlyReadsMemory(F);
1817 setDoesNotThrow(F);
1818 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001819 } else if (Name == "strcpy" ||
1820 Name == "stpcpy" ||
1821 Name == "strcat" ||
1822 Name == "strtol" ||
1823 Name == "strtod" ||
1824 Name == "strtof" ||
1825 Name == "strtoul" ||
1826 Name == "strtoll" ||
1827 Name == "strtold" ||
1828 Name == "strncat" ||
1829 Name == "strncpy" ||
1830 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001831 if (FTy->getNumParams() < 2 ||
1832 !isa<PointerType>(FTy->getParamType(1)))
1833 continue;
1834 setDoesNotThrow(F);
1835 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001836 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001837 if (FTy->getNumParams() != 3 ||
1838 !isa<PointerType>(FTy->getParamType(0)) ||
1839 !isa<PointerType>(FTy->getParamType(1)))
1840 continue;
1841 setDoesNotThrow(F);
1842 setDoesNotCapture(F, 1);
1843 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001844 } else if (Name == "strcmp" ||
1845 Name == "strspn" ||
1846 Name == "strncmp" ||
1847 Name ==" strcspn" ||
1848 Name == "strcoll" ||
1849 Name == "strcasecmp" ||
1850 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001851 if (FTy->getNumParams() < 2 ||
1852 !isa<PointerType>(FTy->getParamType(0)) ||
1853 !isa<PointerType>(FTy->getParamType(1)))
1854 continue;
1855 setOnlyReadsMemory(F);
1856 setDoesNotThrow(F);
1857 setDoesNotCapture(F, 1);
1858 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001859 } else if (Name == "strstr" ||
1860 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001861 if (FTy->getNumParams() != 2 ||
1862 !isa<PointerType>(FTy->getParamType(1)))
1863 continue;
1864 setOnlyReadsMemory(F);
1865 setDoesNotThrow(F);
1866 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001867 } else if (Name == "strtok" ||
1868 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001869 if (FTy->getNumParams() < 2 ||
1870 !isa<PointerType>(FTy->getParamType(1)))
1871 continue;
1872 setDoesNotThrow(F);
1873 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001874 } else if (Name == "scanf" ||
1875 Name == "setbuf" ||
1876 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001877 if (FTy->getNumParams() < 1 ||
1878 !isa<PointerType>(FTy->getParamType(0)))
1879 continue;
1880 setDoesNotThrow(F);
1881 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001882 } else if (Name == "strdup" ||
1883 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001884 if (FTy->getNumParams() < 1 ||
1885 !isa<PointerType>(FTy->getReturnType()) ||
1886 !isa<PointerType>(FTy->getParamType(0)))
1887 continue;
1888 setDoesNotThrow(F);
1889 setDoesNotAlias(F, 0);
1890 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001891 } else if (Name == "stat" ||
1892 Name == "sscanf" ||
1893 Name == "sprintf" ||
1894 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001895 if (FTy->getNumParams() < 2 ||
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001896 !isa<PointerType>(FTy->getParamType(0)) ||
1897 !isa<PointerType>(FTy->getParamType(1)))
1898 continue;
1899 setDoesNotThrow(F);
1900 setDoesNotCapture(F, 1);
1901 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001902 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001903 if (FTy->getNumParams() != 3 ||
1904 !isa<PointerType>(FTy->getParamType(0)) ||
1905 !isa<PointerType>(FTy->getParamType(2)))
1906 continue;
1907 setDoesNotThrow(F);
1908 setDoesNotCapture(F, 1);
1909 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001910 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001911 if (FTy->getNumParams() != 3 ||
1912 !isa<PointerType>(FTy->getParamType(1)) ||
1913 !isa<PointerType>(FTy->getParamType(2)))
1914 continue;
1915 setDoesNotThrow(F);
1916 setDoesNotCapture(F, 2);
1917 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001918 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001919 if (FTy->getNumParams() != 1 ||
1920 !isa<PointerType>(FTy->getParamType(0)))
1921 continue;
1922 // May throw; "system" is a valid pthread cancellation point.
1923 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001924 }
1925 break;
1926 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001927 if (Name == "malloc") {
1928 if (FTy->getNumParams() != 1 ||
1929 !isa<PointerType>(FTy->getReturnType()))
1930 continue;
1931 setDoesNotThrow(F);
1932 setDoesNotAlias(F, 0);
1933 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001934 if (FTy->getNumParams() != 3 ||
1935 !isa<PointerType>(FTy->getParamType(0)) ||
1936 !isa<PointerType>(FTy->getParamType(1)))
1937 continue;
1938 setOnlyReadsMemory(F);
1939 setDoesNotThrow(F);
1940 setDoesNotCapture(F, 1);
1941 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001942 } else if (Name == "memchr" ||
1943 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001944 if (FTy->getNumParams() != 3)
1945 continue;
1946 setOnlyReadsMemory(F);
1947 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001948 } else if (Name == "modf" ||
1949 Name == "modff" ||
1950 Name == "modfl" ||
1951 Name == "memcpy" ||
1952 Name == "memccpy" ||
1953 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001954 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001955 !isa<PointerType>(FTy->getParamType(1)))
1956 continue;
1957 setDoesNotThrow(F);
1958 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001959 } else if (Name == "memalign") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001960 if (!isa<PointerType>(FTy->getReturnType()))
1961 continue;
1962 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001963 } else if (Name == "mkdir" ||
1964 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001965 if (FTy->getNumParams() == 0 ||
1966 !isa<PointerType>(FTy->getParamType(0)))
1967 continue;
1968 setDoesNotThrow(F);
1969 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001970 }
1971 break;
1972 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001973 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001974 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001975 !isa<PointerType>(FTy->getParamType(0)) ||
1976 !isa<PointerType>(FTy->getReturnType()))
1977 continue;
1978 setDoesNotThrow(F);
1979 setDoesNotAlias(F, 0);
1980 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001981 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001982 if (FTy->getNumParams() != 3 ||
1983 !isa<PointerType>(FTy->getParamType(1)))
1984 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001985 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001986 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001987 } else if (Name == "rmdir" ||
1988 Name == "rewind" ||
1989 Name == "remove" ||
1990 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001991 if (FTy->getNumParams() < 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001992 !isa<PointerType>(FTy->getParamType(0)))
1993 continue;
1994 setDoesNotThrow(F);
1995 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001996 } else if (Name == "rename" ||
1997 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001998 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001999 !isa<PointerType>(FTy->getParamType(0)) ||
2000 !isa<PointerType>(FTy->getParamType(1)))
2001 continue;
2002 setDoesNotThrow(F);
2003 setDoesNotCapture(F, 1);
2004 setDoesNotCapture(F, 2);
2005 }
2006 break;
2007 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002008 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002009 if (FTy->getNumParams() != 3 ||
2010 !isa<PointerType>(FTy->getParamType(1)))
2011 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002012 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002013 setDoesNotCapture(F, 2);
2014 }
2015 break;
2016 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002017 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002018 if (FTy->getNumParams() != 3 ||
2019 !isa<PointerType>(FTy->getParamType(0)) ||
2020 !isa<PointerType>(FTy->getParamType(1)))
2021 continue;
2022 setDoesNotThrow(F);
2023 setDoesNotCapture(F, 1);
2024 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002025 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002026 if (FTy->getNumParams() != 3 ||
2027 !isa<PointerType>(FTy->getParamType(0)) ||
2028 !isa<PointerType>(FTy->getParamType(1)))
2029 continue;
2030 setDoesNotThrow(F);
2031 setOnlyReadsMemory(F);
2032 setDoesNotCapture(F, 1);
2033 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002034 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002035 if (FTy->getNumParams() != 2 ||
2036 !isa<PointerType>(FTy->getParamType(0)))
2037 continue;
2038 setDoesNotThrow(F);
2039 setDoesNotCapture(F, 1);
2040 }
2041 break;
2042 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002043 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002044 if (FTy->getNumParams() != 2 ||
2045 !isa<PointerType>(FTy->getReturnType()))
2046 continue;
2047 setDoesNotThrow(F);
2048 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002049 } else if (Name == "chmod" ||
2050 Name == "chown" ||
2051 Name == "ctermid" ||
2052 Name == "clearerr" ||
2053 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002054 if (FTy->getNumParams() == 0 ||
2055 !isa<PointerType>(FTy->getParamType(0)))
2056 continue;
2057 setDoesNotThrow(F);
2058 setDoesNotCapture(F, 1);
2059 }
2060 break;
2061 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002062 if (Name == "atoi" ||
2063 Name == "atol" ||
2064 Name == "atof" ||
2065 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002066 if (FTy->getNumParams() != 1 ||
2067 !isa<PointerType>(FTy->getParamType(0)))
2068 continue;
2069 setDoesNotThrow(F);
2070 setOnlyReadsMemory(F);
2071 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002072 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002073 if (FTy->getNumParams() != 2 ||
2074 !isa<PointerType>(FTy->getParamType(0)))
2075 continue;
2076 setDoesNotThrow(F);
2077 setDoesNotCapture(F, 1);
2078 }
2079 break;
2080 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002081 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002082 if (FTy->getNumParams() != 2 ||
2083 !isa<PointerType>(FTy->getReturnType()) ||
2084 !isa<PointerType>(FTy->getParamType(0)) ||
2085 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002086 continue;
2087 setDoesNotThrow(F);
2088 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002089 setDoesNotCapture(F, 1);
2090 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002091 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002092 if (FTy->getNumParams() != 2 ||
2093 !isa<PointerType>(FTy->getReturnType()) ||
2094 !isa<PointerType>(FTy->getParamType(1)))
2095 continue;
2096 setDoesNotThrow(F);
2097 setDoesNotAlias(F, 0);
2098 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002099 } else if (Name == "feof" ||
2100 Name == "free" ||
2101 Name == "fseek" ||
2102 Name == "ftell" ||
2103 Name == "fgetc" ||
2104 Name == "fseeko" ||
2105 Name == "ftello" ||
2106 Name == "fileno" ||
2107 Name == "fflush" ||
2108 Name == "fclose" ||
2109 Name == "fsetpos" ||
2110 Name == "flockfile" ||
2111 Name == "funlockfile" ||
2112 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002113 if (FTy->getNumParams() == 0 ||
2114 !isa<PointerType>(FTy->getParamType(0)))
2115 continue;
2116 setDoesNotThrow(F);
2117 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002118 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002119 if (FTy->getNumParams() != 1 ||
2120 !isa<PointerType>(FTy->getParamType(0)))
2121 continue;
2122 setDoesNotThrow(F);
2123 setDoesNotCapture(F, 1);
2124 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002125 } else if (Name == "fputc" ||
2126 Name == "fstat" ||
2127 Name == "frexp" ||
2128 Name == "frexpf" ||
2129 Name == "frexpl" ||
2130 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002131 if (FTy->getNumParams() != 2 ||
2132 !isa<PointerType>(FTy->getParamType(1)))
2133 continue;
2134 setDoesNotThrow(F);
2135 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002136 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002137 if (FTy->getNumParams() != 3 ||
2138 !isa<PointerType>(FTy->getParamType(0)) ||
2139 !isa<PointerType>(FTy->getParamType(2)))
2140 continue;
2141 setDoesNotThrow(F);
2142 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002143 } else if (Name == "fread" ||
2144 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002145 if (FTy->getNumParams() != 4 ||
2146 !isa<PointerType>(FTy->getParamType(0)) ||
2147 !isa<PointerType>(FTy->getParamType(3)))
2148 continue;
2149 setDoesNotThrow(F);
2150 setDoesNotCapture(F, 1);
2151 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002152 } else if (Name == "fputs" ||
2153 Name == "fscanf" ||
2154 Name == "fprintf" ||
2155 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002156 if (FTy->getNumParams() < 2 ||
2157 !isa<PointerType>(FTy->getParamType(0)) ||
2158 !isa<PointerType>(FTy->getParamType(1)))
2159 continue;
2160 setDoesNotThrow(F);
2161 setDoesNotCapture(F, 1);
2162 setDoesNotCapture(F, 2);
2163 }
2164 break;
2165 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002166 if (Name == "getc" ||
2167 Name == "getlogin_r" ||
2168 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002169 if (FTy->getNumParams() == 0 ||
2170 !isa<PointerType>(FTy->getParamType(0)))
2171 continue;
2172 setDoesNotThrow(F);
2173 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002174 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002175 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002176 !isa<PointerType>(FTy->getParamType(0)))
2177 continue;
2178 setDoesNotThrow(F);
2179 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002180 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002181 } else if (Name == "gets" ||
2182 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002183 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002184 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002185 if (FTy->getNumParams() != 2 ||
2186 !isa<PointerType>(FTy->getParamType(1)))
2187 continue;
2188 setDoesNotThrow(F);
2189 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002190 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002191 if (FTy->getNumParams() != 1 ||
2192 !isa<PointerType>(FTy->getParamType(0)))
2193 continue;
2194 setDoesNotThrow(F);
2195 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002196 }
2197 break;
2198 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002199 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002200 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002201 !isa<PointerType>(FTy->getParamType(1)))
2202 continue;
2203 setDoesNotThrow(F);
2204 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002205 } else if (Name == "uname" ||
2206 Name == "unlink" ||
2207 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002208 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002209 !isa<PointerType>(FTy->getParamType(0)))
2210 continue;
2211 setDoesNotThrow(F);
2212 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002213 } else if (Name == "utime" ||
2214 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002215 if (FTy->getNumParams() != 2 ||
2216 !isa<PointerType>(FTy->getParamType(0)) ||
2217 !isa<PointerType>(FTy->getParamType(1)))
2218 continue;
2219 setDoesNotThrow(F);
2220 setDoesNotCapture(F, 1);
2221 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002222 }
2223 break;
2224 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002225 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002226 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002227 !isa<PointerType>(FTy->getParamType(1)))
2228 continue;
2229 setDoesNotThrow(F);
2230 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002231 } else if (Name == "puts" ||
2232 Name == "printf" ||
2233 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002234 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002235 !isa<PointerType>(FTy->getParamType(0)))
2236 continue;
2237 setDoesNotThrow(F);
2238 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002239 } else if (Name == "pread" ||
2240 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002241 if (FTy->getNumParams() != 4 ||
2242 !isa<PointerType>(FTy->getParamType(1)))
2243 continue;
2244 // May throw; these are valid pthread cancellation points.
2245 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002246 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002247 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002248 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002249 if (FTy->getNumParams() != 2 ||
2250 !isa<PointerType>(FTy->getReturnType()) ||
2251 !isa<PointerType>(FTy->getParamType(0)) ||
2252 !isa<PointerType>(FTy->getParamType(1)))
2253 continue;
2254 setDoesNotThrow(F);
2255 setDoesNotAlias(F, 0);
2256 setDoesNotCapture(F, 1);
2257 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002258 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002259 if (FTy->getNumParams() != 1 ||
2260 !isa<PointerType>(FTy->getParamType(0)))
2261 continue;
2262 setDoesNotThrow(F);
2263 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002264 }
2265 break;
2266 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002267 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002268 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002269 !isa<PointerType>(FTy->getParamType(1)))
2270 continue;
2271 setDoesNotThrow(F);
2272 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002273 } else if (Name == "vsscanf" ||
2274 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002275 if (FTy->getNumParams() != 3 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002276 !isa<PointerType>(FTy->getParamType(1)) ||
2277 !isa<PointerType>(FTy->getParamType(2)))
2278 continue;
2279 setDoesNotThrow(F);
2280 setDoesNotCapture(F, 1);
2281 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002282 } else if (Name == "valloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002283 if (!isa<PointerType>(FTy->getReturnType()))
2284 continue;
2285 setDoesNotThrow(F);
2286 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002287 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002288 if (FTy->getNumParams() != 2 ||
2289 !isa<PointerType>(FTy->getParamType(0)))
2290 continue;
2291 setDoesNotThrow(F);
2292 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002293 } else if (Name == "vfprintf" ||
2294 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002295 if (FTy->getNumParams() != 3 ||
2296 !isa<PointerType>(FTy->getParamType(0)) ||
2297 !isa<PointerType>(FTy->getParamType(1)))
2298 continue;
2299 setDoesNotThrow(F);
2300 setDoesNotCapture(F, 1);
2301 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002302 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002303 if (FTy->getNumParams() != 4 ||
2304 !isa<PointerType>(FTy->getParamType(0)) ||
2305 !isa<PointerType>(FTy->getParamType(2)))
2306 continue;
2307 setDoesNotThrow(F);
2308 setDoesNotCapture(F, 1);
2309 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002310 }
2311 break;
2312 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002313 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002314 if (FTy->getNumParams() < 2 ||
2315 !isa<PointerType>(FTy->getParamType(0)))
2316 continue;
2317 // May throw; "open" is a valid pthread cancellation point.
2318 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002319 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002320 if (FTy->getNumParams() != 1 ||
2321 !isa<PointerType>(FTy->getReturnType()) ||
2322 !isa<PointerType>(FTy->getParamType(0)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002323 continue;
2324 setDoesNotThrow(F);
2325 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002326 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002327 }
2328 break;
2329 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002330 if (Name == "tmpfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002331 if (!isa<PointerType>(FTy->getReturnType()))
2332 continue;
2333 setDoesNotThrow(F);
2334 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002335 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002336 if (FTy->getNumParams() != 1 ||
2337 !isa<PointerType>(FTy->getParamType(0)))
2338 continue;
2339 setDoesNotThrow(F);
2340 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002341 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002342 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002343 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002344 if (Name == "htonl" ||
2345 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002346 setDoesNotThrow(F);
2347 setDoesNotAccessMemory(F);
2348 }
2349 break;
2350 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002351 if (Name == "ntohl" ||
2352 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002353 setDoesNotThrow(F);
2354 setDoesNotAccessMemory(F);
2355 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002356 break;
2357 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002358 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002359 if (FTy->getNumParams() != 2 ||
2360 !isa<PointerType>(FTy->getParamType(0)) ||
2361 !isa<PointerType>(FTy->getParamType(1)))
2362 continue;
2363 setDoesNotThrow(F);
2364 setDoesNotCapture(F, 1);
2365 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002366 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002367 if (FTy->getNumParams() != 3 ||
2368 !isa<PointerType>(FTy->getParamType(0)))
2369 continue;
2370 setDoesNotThrow(F);
2371 setDoesNotCapture(F, 1);
2372 }
2373 break;
2374 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002375 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002376 if (FTy->getNumParams() != 4 ||
2377 !isa<PointerType>(FTy->getParamType(3)))
2378 continue;
2379 // May throw; places call through function pointer.
2380 setDoesNotCapture(F, 4);
2381 }
2382 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002383 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002384 if (Name == "__strdup" ||
2385 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002386 if (FTy->getNumParams() < 1 ||
2387 !isa<PointerType>(FTy->getReturnType()) ||
2388 !isa<PointerType>(FTy->getParamType(0)))
2389 continue;
2390 setDoesNotThrow(F);
2391 setDoesNotAlias(F, 0);
2392 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002393 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002394 if (FTy->getNumParams() != 3 ||
2395 !isa<PointerType>(FTy->getParamType(1)))
2396 continue;
2397 setDoesNotThrow(F);
2398 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002399 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002400 if (FTy->getNumParams() != 1 ||
2401 !isa<PointerType>(FTy->getParamType(0)))
2402 continue;
2403 setDoesNotThrow(F);
2404 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002405 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002406 if (FTy->getNumParams() != 2 ||
2407 !isa<PointerType>(FTy->getParamType(1)))
2408 continue;
2409 setDoesNotThrow(F);
2410 setDoesNotCapture(F, 2);
2411 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002412 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002413 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002414 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002415 if (FTy->getNumParams() < 1 ||
2416 !isa<PointerType>(FTy->getParamType(0)))
2417 continue;
2418 setDoesNotThrow(F);
2419 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002420 } else if (Name == "\1stat64" ||
2421 Name == "\1lstat64" ||
2422 Name == "\1statvfs64" ||
2423 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002424 if (FTy->getNumParams() < 1 ||
Nick Lewycky225f7472009-02-15 22:47:25 +00002425 !isa<PointerType>(FTy->getParamType(0)) ||
2426 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002427 continue;
2428 setDoesNotThrow(F);
2429 setDoesNotCapture(F, 1);
2430 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002431 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002432 if (FTy->getNumParams() != 2 ||
2433 !isa<PointerType>(FTy->getReturnType()) ||
2434 !isa<PointerType>(FTy->getParamType(0)) ||
2435 !isa<PointerType>(FTy->getParamType(1)))
2436 continue;
2437 setDoesNotThrow(F);
2438 setDoesNotAlias(F, 0);
2439 setDoesNotCapture(F, 1);
2440 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002441 } else if (Name == "\1fseeko64" ||
2442 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002443 if (FTy->getNumParams() == 0 ||
2444 !isa<PointerType>(FTy->getParamType(0)))
2445 continue;
2446 setDoesNotThrow(F);
2447 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002448 } else if (Name == "\1tmpfile64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002449 if (!isa<PointerType>(FTy->getReturnType()))
2450 continue;
2451 setDoesNotThrow(F);
2452 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002453 } else if (Name == "\1fstat64" ||
2454 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002455 if (FTy->getNumParams() != 2 ||
2456 !isa<PointerType>(FTy->getParamType(1)))
2457 continue;
2458 setDoesNotThrow(F);
2459 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002460 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002461 if (FTy->getNumParams() < 2 ||
2462 !isa<PointerType>(FTy->getParamType(0)))
2463 continue;
2464 // May throw; "open" is a valid pthread cancellation point.
2465 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002466 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002467 break;
2468 }
2469 }
2470 return Modified;
2471}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002472
2473// TODO:
2474// Additional cases that we need to add to this file:
2475//
2476// cbrt:
2477// * cbrt(expN(X)) -> expN(x/3)
2478// * cbrt(sqrt(x)) -> pow(x,1/6)
2479// * cbrt(sqrt(x)) -> pow(x,1/9)
2480//
2481// cos, cosf, cosl:
2482// * cos(-x) -> cos(x)
2483//
2484// exp, expf, expl:
2485// * exp(log(x)) -> x
2486//
2487// log, logf, logl:
2488// * log(exp(x)) -> x
2489// * log(x**y) -> y*log(x)
2490// * log(exp(y)) -> y*log(e)
2491// * log(exp2(y)) -> y*log(2)
2492// * log(exp10(y)) -> y*log(10)
2493// * log(sqrt(x)) -> 0.5*log(x)
2494// * log(pow(x,y)) -> y*log(x)
2495//
2496// lround, lroundf, lroundl:
2497// * lround(cnst) -> cnst'
2498//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002499// pow, powf, powl:
2500// * pow(exp(x),y) -> exp(x*y)
2501// * pow(sqrt(x),y) -> pow(x,y*0.5)
2502// * pow(pow(x,y),z)-> pow(x,y*z)
2503//
2504// puts:
2505// * puts("") -> putchar("\n")
2506//
2507// round, roundf, roundl:
2508// * round(cnst) -> cnst'
2509//
2510// signbit:
2511// * signbit(cnst) -> cnst'
2512// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2513//
2514// sqrt, sqrtf, sqrtl:
2515// * sqrt(expN(x)) -> expN(x*0.5)
2516// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2517// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2518//
2519// stpcpy:
2520// * stpcpy(str, "literal") ->
2521// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2522// strrchr:
2523// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2524// (if c is a constant integer and s is a constant string)
2525// * strrchr(s1,0) -> strchr(s1,0)
2526//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002527// strpbrk:
2528// * strpbrk(s,a) -> offset_in_for(s,a)
2529// (if s and a are both constant strings)
2530// * strpbrk(s,"") -> 0
2531// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2532//
2533// strspn, strcspn:
2534// * strspn(s,a) -> const_int (if both args are constant)
2535// * strspn("",a) -> 0
2536// * strspn(s,"") -> 0
2537// * strcspn(s,a) -> const_int (if both args are constant)
2538// * strcspn("",a) -> 0
2539// * strcspn(s,"") -> strlen(a)
2540//
2541// strstr:
2542// * strstr(x,x) -> x
2543// * strstr(s1,s2) -> offset_of_s2_in(s1)
2544// (if s1 and s2 are constant strings)
2545//
2546// tan, tanf, tanl:
2547// * tan(atan(x)) -> x
2548//
2549// trunc, truncf, truncl:
2550// * trunc(cnst) -> cnst'
2551//
2552//