blob: e186601505c21b228dde38131d33e0cb1877d8ea [file] [log] [blame]
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001//===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a simple pass that applies a variety of small
11// optimizations for calls to specific well-known function calls (e.g. runtime
Chris Lattnere9f9a7e2009-09-03 05:19:59 +000012// library functions). Any optimization that takes the very simple form
13// "replace call to library function with simpler code that provides the same
14// result" belongs in this file.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000015//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "simplify-libcalls"
19#include "llvm/Transforms/Scalar.h"
20#include "llvm/Intrinsics.h"
Owen Andersonfa5cbd62009-07-03 19:42:02 +000021#include "llvm/LLVMContext.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000022#include "llvm/Module.h"
23#include "llvm/Pass.h"
24#include "llvm/Support/IRBuilder.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000025#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/ADT/SmallPtrSet.h"
28#include "llvm/ADT/StringMap.h"
29#include "llvm/ADT/Statistic.h"
Daniel Dunbar473955f2009-07-29 22:00:43 +000030#include "llvm/ADT/STLExtras.h"
Chris Lattner56b4f2b2008-05-01 06:39:12 +000031#include "llvm/Support/Debug.h"
Daniel Dunbarf0443c12009-07-26 08:34:35 +000032#include "llvm/Support/raw_ostream.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000033#include "llvm/Config/config.h"
34using namespace llvm;
35
36STATISTIC(NumSimplified, "Number of library calls simplified");
Nick Lewycky0f8df9a2009-01-04 20:27:34 +000037STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000038
39//===----------------------------------------------------------------------===//
40// Optimizer Base Class
41//===----------------------------------------------------------------------===//
42
43/// This class is the abstract base class for the set of optimizations that
44/// corresponds to one library call.
45namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +000046class LibCallOptimization {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000047protected:
48 Function *Caller;
49 const TargetData *TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000050 LLVMContext* Context;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000051public:
52 LibCallOptimization() { }
53 virtual ~LibCallOptimization() {}
54
55 /// CallOptimizer - This pure virtual method is implemented by base classes to
56 /// do various optimizations. If this returns null then no transformation was
57 /// performed. If it returns CI, then it transformed the call and CI is to be
58 /// deleted. If it returns something else, replace CI with the new value and
59 /// delete CI.
Eric Christopher37c8b862009-10-07 21:14:25 +000060 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
Eric Christopher7a61d702008-08-08 19:39:37 +000061 =0;
Eric Christopher37c8b862009-10-07 21:14:25 +000062
Dan Gohmanf14d9192009-08-18 00:48:13 +000063 Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000064 Caller = CI->getParent()->getParent();
Dan Gohmanf14d9192009-08-18 00:48:13 +000065 this->TD = TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000066 if (CI->getCalledFunction())
Owen Andersone922c022009-07-22 00:24:57 +000067 Context = &CI->getCalledFunction()->getContext();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000068 return CallOptimizer(CI->getCalledFunction(), CI, B);
69 }
70
71 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Eric Christopher7a61d702008-08-08 19:39:37 +000072 Value *CastToCStr(Value *V, IRBuilder<> &B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000073
74 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
75 /// specified pointer. Ptr is required to be some pointer type, and the
76 /// return value has 'intptr_t' type.
Eric Christopher7a61d702008-08-08 19:39:37 +000077 Value *EmitStrLen(Value *Ptr, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +000078
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000079 /// EmitMemCpy - Emit a call to the memcpy function to the builder. This
80 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
Eric Christopher37c8b862009-10-07 21:14:25 +000081 Value *EmitMemCpy(Value *Dst, Value *Src, Value *Len,
Eric Christopher7a61d702008-08-08 19:39:37 +000082 unsigned Align, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +000083
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000084 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
85 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
Eric Christopher7a61d702008-08-08 19:39:37 +000086 Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B);
Nick Lewycky13a09e22008-12-21 00:19:21 +000087
88 /// EmitMemCmp - Emit a call to the memcmp function.
89 Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B);
90
Chris Lattnerf5b6bc72009-04-12 05:06:39 +000091 /// EmitMemSet - Emit a call to the memset function
92 Value *EmitMemSet(Value *Dst, Value *Val, Value *Len, IRBuilder<> &B);
93
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000094 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
95 /// 'floor'). This function is known to take a single of type matching 'Op'
96 /// and returns one value with the same type. If 'Op' is a long double, 'l'
97 /// is added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
Dan Gohman79cb8402009-09-25 23:10:17 +000098 Value *EmitUnaryFloatFnCall(Value *Op, const char *Name, IRBuilder<> &B,
99 const AttrListPtr &Attrs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000100
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000101 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
102 /// is an integer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000103 void EmitPutChar(Value *Char, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000104
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000105 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
106 /// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000107 void EmitPutS(Value *Str, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000108
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000109 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
110 /// an i32, and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000111 void EmitFPutC(Value *Char, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000112
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000113 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
114 /// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000115 void EmitFPutS(Value *Str, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000116
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000117 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
118 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000119 void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000120
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000121};
122} // End anonymous namespace.
123
124/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Eric Christopher7a61d702008-08-08 19:39:37 +0000125Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) {
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000126 return
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000127 B.CreateBitCast(V, Type::getInt8PtrTy(*Context), "cstr");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000128}
129
130/// EmitStrLen - Emit a call to the strlen function to the builder, for the
131/// specified pointer. This always returns an integer value of size intptr_t.
Eric Christopher7a61d702008-08-08 19:39:37 +0000132Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000133 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000134 AttributeWithIndex AWI[2];
135 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
136 AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
137 Attribute::NoUnwind);
138
139 Constant *StrLen =M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000140 TD->getIntPtrType(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000141 Type::getInt8PtrTy(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000142 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000143 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
144 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
145 CI->setCallingConv(F->getCallingConv());
146
147 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000148}
149
150/// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
151/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
152Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
Eric Christopher7a61d702008-08-08 19:39:37 +0000153 unsigned Align, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000154 Module *M = Caller->getParent();
Chris Lattner824b9582008-11-21 16:42:48 +0000155 Intrinsic::ID IID = Intrinsic::memcpy;
156 const Type *Tys[1];
157 Tys[0] = Len->getType();
158 Value *MemCpy = Intrinsic::getDeclaration(M, IID, Tys, 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000159 return B.CreateCall4(MemCpy, CastToCStr(Dst, B), CastToCStr(Src, B), Len,
Owen Anderson1d0be152009-08-13 21:58:54 +0000160 ConstantInt::get(Type::getInt32Ty(*Context), Align));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000161}
162
163/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
164/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
165Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
Eric Christopher7a61d702008-08-08 19:39:37 +0000166 Value *Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000167 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000168 AttributeWithIndex AWI;
169 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
170
171 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
Eric Christopher37c8b862009-10-07 21:14:25 +0000172 Type::getInt8PtrTy(*Context),
173 Type::getInt8PtrTy(*Context),
174 Type::getInt32Ty(*Context),
175 TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000176 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000177 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
178
179 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
180 CI->setCallingConv(F->getCallingConv());
181
182 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000183}
184
Nick Lewycky13a09e22008-12-21 00:19:21 +0000185/// EmitMemCmp - Emit a call to the memcmp function.
186Value *LibCallOptimization::EmitMemCmp(Value *Ptr1, Value *Ptr2,
187 Value *Len, IRBuilder<> &B) {
188 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000189 AttributeWithIndex AWI[3];
190 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
191 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
192 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
193 Attribute::NoUnwind);
194
195 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000196 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000197 Type::getInt8PtrTy(*Context),
198 Type::getInt8PtrTy(*Context),
Owen Anderson1d0be152009-08-13 21:58:54 +0000199 TD->getIntPtrType(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000200 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
201 Len, "memcmp");
202
203 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
204 CI->setCallingConv(F->getCallingConv());
205
206 return CI;
Nick Lewycky13a09e22008-12-21 00:19:21 +0000207}
208
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000209/// EmitMemSet - Emit a call to the memset function
210Value *LibCallOptimization::EmitMemSet(Value *Dst, Value *Val,
211 Value *Len, IRBuilder<> &B) {
212 Module *M = Caller->getParent();
213 Intrinsic::ID IID = Intrinsic::memset;
214 const Type *Tys[1];
215 Tys[0] = Len->getType();
216 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000217 Value *Align = ConstantInt::get(Type::getInt32Ty(*Context), 1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000218 return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
219}
220
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000221/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
222/// 'floor'). This function is known to take a single of type matching 'Op' and
223/// returns one value with the same type. If 'Op' is a long double, 'l' is
224/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
225Value *LibCallOptimization::EmitUnaryFloatFnCall(Value *Op, const char *Name,
Dan Gohman79cb8402009-09-25 23:10:17 +0000226 IRBuilder<> &B,
227 const AttrListPtr &Attrs) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000228 char NameBuffer[20];
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000229 if (!Op->getType()->isDoubleTy()) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000230 // If we need to add a suffix, copy into NameBuffer.
231 unsigned NameLen = strlen(Name);
232 assert(NameLen < sizeof(NameBuffer)-2);
233 memcpy(NameBuffer, Name, NameLen);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000234 if (Op->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000235 NameBuffer[NameLen] = 'f'; // floorf
236 else
237 NameBuffer[NameLen] = 'l'; // floorl
238 NameBuffer[NameLen+1] = 0;
239 Name = NameBuffer;
240 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000241
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000242 Module *M = Caller->getParent();
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000243 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000244 Op->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000245 CallInst *CI = B.CreateCall(Callee, Op, Name);
Dan Gohman79cb8402009-09-25 23:10:17 +0000246 CI->setAttributes(Attrs);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000247 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
248 CI->setCallingConv(F->getCallingConv());
249
250 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000251}
252
253/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
254/// is an integer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000255void LibCallOptimization::EmitPutChar(Value *Char, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000256 Module *M = Caller->getParent();
Owen Anderson1d0be152009-08-13 21:58:54 +0000257 Value *PutChar = M->getOrInsertFunction("putchar", Type::getInt32Ty(*Context),
258 Type::getInt32Ty(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000259 CallInst *CI = B.CreateCall(PutChar,
Eric Christopher37c8b862009-10-07 21:14:25 +0000260 B.CreateIntCast(Char,
261 Type::getInt32Ty(*Context),
262 "chari"),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000263 "putchar");
264
265 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
266 CI->setCallingConv(F->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000267}
268
269/// EmitPutS - Emit a call to the puts function. This assumes that Str is
270/// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000271void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000272 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000273 AttributeWithIndex AWI[2];
274 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
275 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
276
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000277 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000278 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000279 Type::getInt8PtrTy(*Context),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000280 NULL);
281 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
282 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
283 CI->setCallingConv(F->getCallingConv());
284
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000285}
286
287/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
288/// an integer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000289void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000290 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000291 AttributeWithIndex AWI[2];
292 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
293 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
294 Constant *F;
295 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000296 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
297 Type::getInt32Ty(*Context),
298 Type::getInt32Ty(*Context), File->getType(),
299 NULL);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000300 else
Eric Christopher37c8b862009-10-07 21:14:25 +0000301 F = M->getOrInsertFunction("fputc",
302 Type::getInt32Ty(*Context),
303 Type::getInt32Ty(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000304 File->getType(), NULL);
Owen Anderson1d0be152009-08-13 21:58:54 +0000305 Char = B.CreateIntCast(Char, Type::getInt32Ty(*Context), "chari");
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000306 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
307
308 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
309 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000310}
311
312/// EmitFPutS - Emit a call to the puts function. Str is required to be a
313/// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000314void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000315 Module *M = Caller->getParent();
Nick Lewycky225f7472009-02-15 22:47:25 +0000316 AttributeWithIndex AWI[3];
317 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
318 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
319 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000320 Constant *F;
321 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000322 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
323 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000324 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000325 File->getType(), NULL);
326 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000327 F = M->getOrInsertFunction("fputs", Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000328 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000329 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000330 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
331
332 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
333 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000334}
335
336/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
337/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
338void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Eric Christopher7a61d702008-08-08 19:39:37 +0000339 IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000340 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000341 AttributeWithIndex AWI[3];
342 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
343 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
344 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
345 Constant *F;
346 if (isa<PointerType>(File->getType()))
347 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000348 TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000349 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000350 TD->getIntPtrType(*Context),
351 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000352 File->getType(), NULL);
353 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000354 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000355 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000356 TD->getIntPtrType(*Context),
357 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000358 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000359 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Owen Anderson1d0be152009-08-13 21:58:54 +0000360 ConstantInt::get(TD->getIntPtrType(*Context), 1), File);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000361
362 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
363 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000364}
365
366//===----------------------------------------------------------------------===//
367// Helper Functions
368//===----------------------------------------------------------------------===//
369
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000370/// GetStringLengthH - If we can compute the length of the string pointed to by
371/// the specified pointer, return 'len+1'. If we can't, return 0.
372static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
373 // Look through noop bitcast instructions.
374 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
375 return GetStringLengthH(BCI->getOperand(0), PHIs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000376
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000377 // If this is a PHI node, there are two cases: either we have already seen it
378 // or we haven't.
379 if (PHINode *PN = dyn_cast<PHINode>(V)) {
380 if (!PHIs.insert(PN))
381 return ~0ULL; // already in the set.
Eric Christopher37c8b862009-10-07 21:14:25 +0000382
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000383 // If it was new, see if all the input strings are the same length.
384 uint64_t LenSoFar = ~0ULL;
385 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
386 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
387 if (Len == 0) return 0; // Unknown length -> unknown.
Eric Christopher37c8b862009-10-07 21:14:25 +0000388
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000389 if (Len == ~0ULL) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +0000390
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000391 if (Len != LenSoFar && LenSoFar != ~0ULL)
392 return 0; // Disagree -> unknown.
393 LenSoFar = Len;
394 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000395
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000396 // Success, all agree.
397 return LenSoFar;
398 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000399
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000400 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
401 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
402 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
403 if (Len1 == 0) return 0;
404 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
405 if (Len2 == 0) return 0;
406 if (Len1 == ~0ULL) return Len2;
407 if (Len2 == ~0ULL) return Len1;
408 if (Len1 != Len2) return 0;
409 return Len1;
410 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000411
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000412 // If the value is not a GEP instruction nor a constant expression with a
413 // GEP instruction, then return unknown.
414 User *GEP = 0;
415 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
416 GEP = GEPI;
417 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
418 if (CE->getOpcode() != Instruction::GetElementPtr)
419 return 0;
420 GEP = CE;
421 } else {
422 return 0;
423 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000424
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000425 // Make sure the GEP has exactly three arguments.
426 if (GEP->getNumOperands() != 3)
427 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000428
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000429 // Check to make sure that the first operand of the GEP is an integer and
430 // has value 0 so that we are sure we're indexing into the initializer.
431 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
432 if (!Idx->isZero())
433 return 0;
434 } else
435 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000436
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000437 // If the second index isn't a ConstantInt, then this is a variable index
438 // into the array. If this occurs, we can't say anything meaningful about
439 // the string.
440 uint64_t StartIdx = 0;
441 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
442 StartIdx = CI->getZExtValue();
443 else
444 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000445
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000446 // The GEP instruction, constant or instruction, must reference a global
447 // variable that is a constant and is initialized. The referenced constant
448 // initializer is the array that we'll use for optimization.
449 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
Dan Gohman107f41f2009-08-19 00:11:12 +0000450 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
451 GV->mayBeOverridden())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000452 return 0;
453 Constant *GlobalInit = GV->getInitializer();
Eric Christopher37c8b862009-10-07 21:14:25 +0000454
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000455 // Handle the ConstantAggregateZero case, which is a degenerate case. The
456 // initializer is constant zero so the length of the string must be zero.
457 if (isa<ConstantAggregateZero>(GlobalInit))
458 return 1; // Len = 0 offset by 1.
Eric Christopher37c8b862009-10-07 21:14:25 +0000459
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000460 // Must be a Constant Array
461 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
Owen Anderson1d0be152009-08-13 21:58:54 +0000462 if (!Array ||
463 Array->getType()->getElementType() != Type::getInt8Ty(V->getContext()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000464 return false;
Eric Christopher37c8b862009-10-07 21:14:25 +0000465
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000466 // Get the number of elements in the array
467 uint64_t NumElts = Array->getType()->getNumElements();
Eric Christopher37c8b862009-10-07 21:14:25 +0000468
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000469 // Traverse the constant array from StartIdx (derived above) which is
470 // the place the GEP refers to in the array.
471 for (unsigned i = StartIdx; i != NumElts; ++i) {
472 Constant *Elt = Array->getOperand(i);
473 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
474 if (!CI) // This array isn't suitable, non-int initializer.
475 return 0;
476 if (CI->isZero())
477 return i-StartIdx+1; // We found end of string, success!
478 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000479
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000480 return 0; // The array isn't null terminated, conservatively return 'unknown'.
481}
482
483/// GetStringLength - If we can compute the length of the string pointed to by
484/// the specified pointer, return 'len+1'. If we can't, return 0.
485static uint64_t GetStringLength(Value *V) {
486 if (!isa<PointerType>(V->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000487
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000488 SmallPtrSet<PHINode*, 32> PHIs;
489 uint64_t Len = GetStringLengthH(V, PHIs);
490 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
491 // an empty string as a length.
492 return Len == ~0ULL ? 1 : Len;
493}
494
495/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +0000496/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000497static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
498 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
499 UI != E; ++UI) {
500 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
501 if (IC->isEquality())
502 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
503 if (C->isNullValue())
504 continue;
505 // Unknown instruction.
506 return false;
507 }
508 return true;
509}
510
511//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000512// String and Memory LibCall Optimizations
513//===----------------------------------------------------------------------===//
514
515//===---------------------------------------===//
516// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000517namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000518struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000519 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000520 // Verify the "strcat" function prototype.
521 const FunctionType *FT = Callee->getFunctionType();
522 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000523 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000524 FT->getParamType(0) != FT->getReturnType() ||
525 FT->getParamType(1) != FT->getReturnType())
526 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000527
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000528 // Extract some information from the instruction
529 Value *Dst = CI->getOperand(1);
530 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000531
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000532 // See if we can get the length of the input string.
533 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000534 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000535 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000536
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000537 // Handle the simple, do-nothing case: strcat(x, "") -> x
538 if (Len == 0)
539 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000540
541 // These optimizations require TargetData.
542 if (!TD) return 0;
543
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000544 EmitStrLenMemCpy(Src, Dst, Len, B);
545 return Dst;
546 }
547
548 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000549 // We need to find the end of the destination string. That's where the
550 // memory is to be moved to. We just generate a call to strlen.
551 Value *DstLen = EmitStrLen(Dst, B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000552
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000553 // Now that we have the destination's length, we must index into the
554 // destination's pointer to get the actual memcpy destination (end of
555 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000556 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000557
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000558 // We have enough information to now generate the memcpy call to do the
559 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000560 EmitMemCpy(CpyDst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000561 ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000562 }
563};
564
565//===---------------------------------------===//
566// 'strncat' Optimizations
567
Chris Lattner3e8b6632009-09-02 06:11:42 +0000568struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000569 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
570 // Verify the "strncat" function prototype.
571 const FunctionType *FT = Callee->getFunctionType();
572 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000573 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000574 FT->getParamType(0) != FT->getReturnType() ||
575 FT->getParamType(1) != FT->getReturnType() ||
576 !isa<IntegerType>(FT->getParamType(2)))
577 return 0;
578
579 // Extract some information from the instruction
580 Value *Dst = CI->getOperand(1);
581 Value *Src = CI->getOperand(2);
582 uint64_t Len;
583
584 // We don't do anything if length is not constant
585 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
586 Len = LengthArg->getZExtValue();
587 else
588 return 0;
589
590 // See if we can get the length of the input string.
591 uint64_t SrcLen = GetStringLength(Src);
592 if (SrcLen == 0) return 0;
593 --SrcLen; // Unbias length.
594
595 // Handle the simple, do-nothing cases:
596 // strncat(x, "", c) -> x
597 // strncat(x, c, 0) -> x
598 if (SrcLen == 0 || Len == 0) return Dst;
599
Dan Gohmanf14d9192009-08-18 00:48:13 +0000600 // These optimizations require TargetData.
601 if (!TD) return 0;
602
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000603 // We don't optimize this case
604 if (Len < SrcLen) return 0;
605
606 // strncat(x, s, c) -> strcat(x, s)
607 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000608 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000609 return Dst;
610 }
611};
612
613//===---------------------------------------===//
614// 'strchr' Optimizations
615
Chris Lattner3e8b6632009-09-02 06:11:42 +0000616struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000617 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000618 // Verify the "strchr" function prototype.
619 const FunctionType *FT = Callee->getFunctionType();
620 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000621 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000622 FT->getParamType(0) != FT->getReturnType())
623 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000624
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000625 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000626
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000627 // If the second operand is non-constant, see if we can compute the length
628 // of the input string and turn this into memchr.
629 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
630 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000631 // These optimizations require TargetData.
632 if (!TD) return 0;
633
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000634 uint64_t Len = GetStringLength(SrcStr);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000635 if (Len == 0 ||
636 FT->getParamType(1) != Type::getInt32Ty(*Context)) // memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000637 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000638
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000639 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Owen Anderson1d0be152009-08-13 21:58:54 +0000640 ConstantInt::get(TD->getIntPtrType(*Context), Len), B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000641 }
642
643 // Otherwise, the character is a constant, see if the first argument is
644 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000645 std::string Str;
646 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000647 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000648
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000649 // strchr can find the nul character.
650 Str += '\0';
651 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000652
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000653 // Compute the offset.
654 uint64_t i = 0;
655 while (1) {
656 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000657 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000658 // Did we find our match?
659 if (Str[i] == CharValue)
660 break;
661 ++i;
662 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000663
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000664 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000665 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000666 return B.CreateGEP(SrcStr, Idx, "strchr");
667 }
668};
669
670//===---------------------------------------===//
671// 'strcmp' Optimizations
672
Chris Lattner3e8b6632009-09-02 06:11:42 +0000673struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000674 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000675 // Verify the "strcmp" function prototype.
676 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000677 if (FT->getNumParams() != 2 ||
678 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000679 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000680 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000681 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000682
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000683 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
684 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000685 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000686
Bill Wendling0582ae92009-03-13 04:39:26 +0000687 std::string Str1, Str2;
688 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
689 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000690
Bill Wendling0582ae92009-03-13 04:39:26 +0000691 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000692 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000693
Bill Wendling0582ae92009-03-13 04:39:26 +0000694 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000695 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000696
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000697 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000698 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000699 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000700 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000701
702 // strcmp(P, "x") -> memcmp(P, "x", 2)
703 uint64_t Len1 = GetStringLength(Str1P);
704 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000705 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000706 // These optimizations require TargetData.
707 if (!TD) return 0;
708
Nick Lewycky13a09e22008-12-21 00:19:21 +0000709 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000710 ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattner849832c2009-06-19 04:17:36 +0000711 std::min(Len1, Len2)), B);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000712 }
713
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000714 return 0;
715 }
716};
717
718//===---------------------------------------===//
719// 'strncmp' Optimizations
720
Chris Lattner3e8b6632009-09-02 06:11:42 +0000721struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000722 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000723 // Verify the "strncmp" function prototype.
724 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000725 if (FT->getNumParams() != 3 ||
726 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000727 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000728 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000729 !isa<IntegerType>(FT->getParamType(2)))
730 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000731
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000732 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
733 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000734 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000735
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000736 // Get the length argument if it is constant.
737 uint64_t Length;
738 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
739 Length = LengthArg->getZExtValue();
740 else
741 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000742
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000743 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000744 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000745
Bill Wendling0582ae92009-03-13 04:39:26 +0000746 std::string Str1, Str2;
747 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
748 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000749
Bill Wendling0582ae92009-03-13 04:39:26 +0000750 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000751 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000752
Bill Wendling0582ae92009-03-13 04:39:26 +0000753 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000754 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000755
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000756 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000757 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000758 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000759 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000760 return 0;
761 }
762};
763
764
765//===---------------------------------------===//
766// 'strcpy' Optimizations
767
Chris Lattner3e8b6632009-09-02 06:11:42 +0000768struct StrCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000769 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000770 // Verify the "strcpy" function prototype.
771 const FunctionType *FT = Callee->getFunctionType();
772 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
773 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000774 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000775 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000776
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000777 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
778 if (Dst == Src) // strcpy(x,x) -> x
779 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000780
Dan Gohmanf14d9192009-08-18 00:48:13 +0000781 // These optimizations require TargetData.
782 if (!TD) return 0;
783
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000784 // See if we can get the length of the input string.
785 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000786 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000787
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000788 // We have enough information to now generate the memcpy call to do the
789 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000790 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000791 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000792 return Dst;
793 }
794};
795
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000796//===---------------------------------------===//
797// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000798
Chris Lattner3e8b6632009-09-02 06:11:42 +0000799struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000800 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
801 const FunctionType *FT = Callee->getFunctionType();
802 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
803 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000804 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000805 !isa<IntegerType>(FT->getParamType(2)))
806 return 0;
807
808 Value *Dst = CI->getOperand(1);
809 Value *Src = CI->getOperand(2);
810 Value *LenOp = CI->getOperand(3);
811
812 // See if we can get the length of the input string.
813 uint64_t SrcLen = GetStringLength(Src);
814 if (SrcLen == 0) return 0;
815 --SrcLen;
816
817 if (SrcLen == 0) {
818 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000819 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
820 B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000821 return Dst;
822 }
823
824 uint64_t Len;
825 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
826 Len = LengthArg->getZExtValue();
827 else
828 return 0;
829
830 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
831
Dan Gohmanf14d9192009-08-18 00:48:13 +0000832 // These optimizations require TargetData.
833 if (!TD) return 0;
834
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000835 // Let strncpy handle the zero padding
836 if (Len > SrcLen+1) return 0;
837
838 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000839 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000840 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000841
842 return Dst;
843 }
844};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000845
846//===---------------------------------------===//
847// 'strlen' Optimizations
848
Chris Lattner3e8b6632009-09-02 06:11:42 +0000849struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000850 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000851 const FunctionType *FT = Callee->getFunctionType();
852 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000853 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000854 !isa<IntegerType>(FT->getReturnType()))
855 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000856
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000857 Value *Src = CI->getOperand(1);
858
859 // Constant folding: strlen("xyz") -> 3
860 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000861 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000862
863 // Handle strlen(p) != 0.
864 if (!IsOnlyUsedInZeroEqualityComparison(CI)) return 0;
865
866 // strlen(x) != 0 --> *x != 0
867 // strlen(x) == 0 --> *x == 0
868 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
869 }
870};
871
872//===---------------------------------------===//
Nick Lewycky4c498412009-02-13 15:31:46 +0000873// 'strto*' Optimizations
874
Chris Lattner3e8b6632009-09-02 06:11:42 +0000875struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000876 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
877 const FunctionType *FT = Callee->getFunctionType();
878 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
879 !isa<PointerType>(FT->getParamType(0)) ||
880 !isa<PointerType>(FT->getParamType(1)))
881 return 0;
882
883 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000884 if (isa<ConstantPointerNull>(EndPtr)) {
885 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000886 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000887 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000888
889 return 0;
890 }
891};
892
893
894//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000895// 'memcmp' Optimizations
896
Chris Lattner3e8b6632009-09-02 06:11:42 +0000897struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000898 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000899 const FunctionType *FT = Callee->getFunctionType();
900 if (FT->getNumParams() != 3 || !isa<PointerType>(FT->getParamType(0)) ||
901 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000902 FT->getReturnType() != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000903 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000904
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000905 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000906
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000907 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000908 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000909
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000910 // Make sure we have a constant length.
911 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000912 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000913 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000914
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000915 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000916 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000917
918 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
919 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
920 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +0000921 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000922 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000923
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000924 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS ^ *(short*)RHS) != 0
925 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS ^ *(int*)RHS) != 0
926 if ((Len == 2 || Len == 4) && IsOnlyUsedInZeroEqualityComparison(CI)) {
Owen Andersondebcb012009-07-29 22:17:13 +0000927 const Type *PTy = PointerType::getUnqual(Len == 2 ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000928 Type::getInt16Ty(*Context) : Type::getInt32Ty(*Context));
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000929 LHS = B.CreateBitCast(LHS, PTy, "tmp");
930 RHS = B.CreateBitCast(RHS, PTy, "tmp");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000931 LoadInst *LHSV = B.CreateLoad(LHS, "lhsv");
932 LoadInst *RHSV = B.CreateLoad(RHS, "rhsv");
933 LHSV->setAlignment(1); RHSV->setAlignment(1); // Unaligned loads.
934 return B.CreateZExt(B.CreateXor(LHSV, RHSV, "shortdiff"), CI->getType());
935 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000936
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000937 return 0;
938 }
939};
940
941//===---------------------------------------===//
942// 'memcpy' Optimizations
943
Chris Lattner3e8b6632009-09-02 06:11:42 +0000944struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000945 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000946 // These optimizations require TargetData.
947 if (!TD) return 0;
948
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000949 const FunctionType *FT = Callee->getFunctionType();
950 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
951 !isa<PointerType>(FT->getParamType(0)) ||
952 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000953 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000954 return 0;
955
956 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
957 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
958 return CI->getOperand(1);
959 }
960};
961
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000962//===---------------------------------------===//
963// 'memmove' Optimizations
964
Chris Lattner3e8b6632009-09-02 06:11:42 +0000965struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000966 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000967 // These optimizations require TargetData.
968 if (!TD) return 0;
969
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000970 const FunctionType *FT = Callee->getFunctionType();
971 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
972 !isa<PointerType>(FT->getParamType(0)) ||
973 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000974 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000975 return 0;
976
977 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
978 Module *M = Caller->getParent();
979 Intrinsic::ID IID = Intrinsic::memmove;
980 const Type *Tys[1];
Owen Anderson1d0be152009-08-13 21:58:54 +0000981 Tys[0] = TD->getIntPtrType(*Context);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000982 Value *MemMove = Intrinsic::getDeclaration(M, IID, Tys, 1);
983 Value *Dst = CastToCStr(CI->getOperand(1), B);
984 Value *Src = CastToCStr(CI->getOperand(2), B);
985 Value *Size = CI->getOperand(3);
Owen Anderson1d0be152009-08-13 21:58:54 +0000986 Value *Align = ConstantInt::get(Type::getInt32Ty(*Context), 1);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000987 B.CreateCall4(MemMove, Dst, Src, Size, Align);
988 return CI->getOperand(1);
989 }
990};
991
992//===---------------------------------------===//
993// 'memset' Optimizations
994
Chris Lattner3e8b6632009-09-02 06:11:42 +0000995struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000996 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000997 // These optimizations require TargetData.
998 if (!TD) return 0;
999
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001000 const FunctionType *FT = Callee->getFunctionType();
1001 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1002 !isa<PointerType>(FT->getParamType(0)) ||
Eli Friedman62bb4132009-07-18 08:34:51 +00001003 !isa<IntegerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001004 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001005 return 0;
1006
1007 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +00001008 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1009 false);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001010 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001011 return CI->getOperand(1);
1012 }
1013};
1014
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001015//===----------------------------------------------------------------------===//
1016// Math Library Optimizations
1017//===----------------------------------------------------------------------===//
1018
1019//===---------------------------------------===//
1020// 'pow*' Optimizations
1021
Chris Lattner3e8b6632009-09-02 06:11:42 +00001022struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001023 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001024 const FunctionType *FT = Callee->getFunctionType();
1025 // Just make sure this has 2 arguments of the same FP type, which match the
1026 // result type.
1027 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1028 FT->getParamType(0) != FT->getParamType(1) ||
1029 !FT->getParamType(0)->isFloatingPoint())
1030 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001031
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001032 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
1033 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1034 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
1035 return Op1C;
1036 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +00001037 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001038 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001039
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001040 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1041 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001042
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001043 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001044 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001045
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001046 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +00001047 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1048 // This is faster than calling pow, and still handles negative zero
1049 // and negative infinite correctly.
1050 // TODO: In fast-math mode, this could be just sqrt(x).
1051 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +00001052 Value *Inf = ConstantFP::getInfinity(CI->getType());
1053 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +00001054 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1055 Callee->getAttributes());
1056 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1057 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +00001058 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
1059 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
1060 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001061 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001062
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001063 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1064 return Op1;
1065 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001066 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001067 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001068 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001069 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001070 return 0;
1071 }
1072};
1073
1074//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +00001075// 'exp2' Optimizations
1076
Chris Lattner3e8b6632009-09-02 06:11:42 +00001077struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001078 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +00001079 const FunctionType *FT = Callee->getFunctionType();
1080 // Just make sure this has 1 argument of FP type, which matches the
1081 // result type.
1082 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1083 !FT->getParamType(0)->isFloatingPoint())
1084 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001085
Chris Lattnere818f772008-05-02 18:43:35 +00001086 Value *Op = CI->getOperand(1);
1087 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1088 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1089 Value *LdExpArg = 0;
1090 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1091 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001092 LdExpArg = B.CreateSExt(OpC->getOperand(0),
1093 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001094 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1095 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001096 LdExpArg = B.CreateZExt(OpC->getOperand(0),
1097 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001098 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001099
Chris Lattnere818f772008-05-02 18:43:35 +00001100 if (LdExpArg) {
1101 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001102 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001103 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001104 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001105 Name = "ldexp";
1106 else
1107 Name = "ldexpl";
1108
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001109 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001110 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001111 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +00001112
1113 Module *M = Caller->getParent();
1114 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +00001115 Op->getType(),
1116 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001117 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1118 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1119 CI->setCallingConv(F->getCallingConv());
1120
1121 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +00001122 }
1123 return 0;
1124 }
1125};
Chris Lattnere818f772008-05-02 18:43:35 +00001126
1127//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001128// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1129
Chris Lattner3e8b6632009-09-02 06:11:42 +00001130struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001131 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001132 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001133 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1134 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001135 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001136
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001137 // If this is something like 'floor((double)floatval)', convert to floorf.
1138 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001139 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001140 return 0;
1141
1142 // floor((double)floatval) -> (double)floorf(floatval)
1143 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +00001144 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
1145 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +00001146 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001147 }
1148};
1149
1150//===----------------------------------------------------------------------===//
1151// Integer Optimizations
1152//===----------------------------------------------------------------------===//
1153
1154//===---------------------------------------===//
1155// 'ffs*' Optimizations
1156
Chris Lattner3e8b6632009-09-02 06:11:42 +00001157struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001158 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001159 const FunctionType *FT = Callee->getFunctionType();
1160 // Just make sure this has 2 arguments of the same FP type, which match the
1161 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +00001162 if (FT->getNumParams() != 1 ||
1163 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001164 !isa<IntegerType>(FT->getParamType(0)))
1165 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001166
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001167 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001168
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001169 // Constant fold.
1170 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1171 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +00001172 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +00001173 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001174 CI->getValue().countTrailingZeros()+1);
1175 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001176
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001177 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1178 const Type *ArgType = Op->getType();
1179 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1180 Intrinsic::cttz, &ArgType, 1);
1181 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +00001182 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +00001183 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001184
Owen Andersona7235ea2009-07-31 20:28:14 +00001185 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001186 return B.CreateSelect(Cond, V,
1187 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001188 }
1189};
1190
1191//===---------------------------------------===//
1192// 'isdigit' Optimizations
1193
Chris Lattner3e8b6632009-09-02 06:11:42 +00001194struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001195 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001196 const FunctionType *FT = Callee->getFunctionType();
1197 // We require integer(i32)
1198 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001199 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001200 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001201
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001202 // isdigit(c) -> (c-'0') <u 10
1203 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001204 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001205 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001206 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001207 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001208 return B.CreateZExt(Op, CI->getType());
1209 }
1210};
1211
1212//===---------------------------------------===//
1213// 'isascii' Optimizations
1214
Chris Lattner3e8b6632009-09-02 06:11:42 +00001215struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001216 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001217 const FunctionType *FT = Callee->getFunctionType();
1218 // We require integer(i32)
1219 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001220 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001221 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001222
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001223 // isascii(c) -> c <u 128
1224 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001225 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001226 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001227 return B.CreateZExt(Op, CI->getType());
1228 }
1229};
Eric Christopher37c8b862009-10-07 21:14:25 +00001230
Chris Lattner313f0e62008-06-09 08:26:51 +00001231//===---------------------------------------===//
1232// 'abs', 'labs', 'llabs' Optimizations
1233
Chris Lattner3e8b6632009-09-02 06:11:42 +00001234struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001235 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001236 const FunctionType *FT = Callee->getFunctionType();
1237 // We require integer(integer) where the types agree.
1238 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
1239 FT->getParamType(0) != FT->getReturnType())
1240 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001241
Chris Lattner313f0e62008-06-09 08:26:51 +00001242 // abs(x) -> x >s -1 ? x : -x
1243 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001244 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001245 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001246 "ispos");
1247 Value *Neg = B.CreateNeg(Op, "neg");
1248 return B.CreateSelect(Pos, Op, Neg);
1249 }
1250};
Eric Christopher37c8b862009-10-07 21:14:25 +00001251
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001252
1253//===---------------------------------------===//
1254// 'toascii' Optimizations
1255
Chris Lattner3e8b6632009-09-02 06:11:42 +00001256struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001257 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001258 const FunctionType *FT = Callee->getFunctionType();
1259 // We require i32(i32)
1260 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001261 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001262 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001263
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001264 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001265 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +00001266 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001267 }
1268};
1269
1270//===----------------------------------------------------------------------===//
1271// Formatting and IO Optimizations
1272//===----------------------------------------------------------------------===//
1273
1274//===---------------------------------------===//
1275// 'printf' Optimizations
1276
Chris Lattner3e8b6632009-09-02 06:11:42 +00001277struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001278 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001279 // Require one fixed pointer argument and an integer/void result.
1280 const FunctionType *FT = Callee->getFunctionType();
1281 if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
1282 !(isa<IntegerType>(FT->getReturnType()) ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001283 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001284 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001285
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001286 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001287 std::string FormatStr;
1288 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1289 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001290
1291 // Empty format string -> noop.
1292 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001293 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001294 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001295
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001296 // printf("x") -> putchar('x'), even for '%'.
1297 if (FormatStr.size() == 1) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001298 EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context), FormatStr[0]), B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001299 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001300 ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001301 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001302
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001303 // printf("foo\n") --> puts("foo")
1304 if (FormatStr[FormatStr.size()-1] == '\n' &&
1305 FormatStr.find('%') == std::string::npos) { // no format characters.
1306 // Create a string literal with no \n on it. We expect the constant merge
1307 // pass to be run after this pass, to merge duplicate strings.
1308 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001309 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001310 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1311 GlobalVariable::InternalLinkage, C, "str");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001312 EmitPutS(C, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001313 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001314 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001315 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001316
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001317 // Optimize specific format strings.
1318 // printf("%c", chr) --> putchar(*(i8*)dst)
1319 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
1320 isa<IntegerType>(CI->getOperand(2)->getType())) {
1321 EmitPutChar(CI->getOperand(2), B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001322 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001323 ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001324 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001325
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001326 // printf("%s\n", str) --> puts(str)
1327 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
1328 isa<PointerType>(CI->getOperand(2)->getType()) &&
1329 CI->use_empty()) {
1330 EmitPutS(CI->getOperand(2), B);
1331 return CI;
1332 }
1333 return 0;
1334 }
1335};
1336
1337//===---------------------------------------===//
1338// 'sprintf' Optimizations
1339
Chris Lattner3e8b6632009-09-02 06:11:42 +00001340struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001341 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001342 // Require two fixed pointer arguments and an integer result.
1343 const FunctionType *FT = Callee->getFunctionType();
1344 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1345 !isa<PointerType>(FT->getParamType(1)) ||
1346 !isa<IntegerType>(FT->getReturnType()))
1347 return 0;
1348
1349 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001350 std::string FormatStr;
1351 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1352 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001353
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001354 // If we just have a format string (nothing else crazy) transform it.
1355 if (CI->getNumOperands() == 3) {
1356 // Make sure there's no % in the constant array. We could try to handle
1357 // %% -> % in the future if we cared.
1358 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1359 if (FormatStr[i] == '%')
1360 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001361
1362 // These optimizations require TargetData.
1363 if (!TD) return 0;
1364
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001365 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1366 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Owen Anderson1d0be152009-08-13 21:58:54 +00001367 ConstantInt::get(TD->getIntPtrType(*Context), FormatStr.size()+1),1,B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001368 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001369 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001370
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001371 // The remaining optimizations require the format string to be "%s" or "%c"
1372 // and have an extra operand.
1373 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1374 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001375
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001376 // Decode the second character of the format string.
1377 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001378 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001379 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001380 Value *V = B.CreateTrunc(CI->getOperand(3),
1381 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001382 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1383 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001384 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1385 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001386 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001387
Owen Andersoneed707b2009-07-24 23:12:02 +00001388 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001389 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001390
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001391 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001392 // These optimizations require TargetData.
1393 if (!TD) return 0;
1394
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001395 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1396 if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
1397
1398 Value *Len = EmitStrLen(CI->getOperand(3), B);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001399 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001400 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001401 "leninc");
1402 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001403
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001404 // The sprintf result is the unincremented number of bytes in the string.
1405 return B.CreateIntCast(Len, CI->getType(), false);
1406 }
1407 return 0;
1408 }
1409};
1410
1411//===---------------------------------------===//
1412// 'fwrite' Optimizations
1413
Chris Lattner3e8b6632009-09-02 06:11:42 +00001414struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001415 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001416 // Require a pointer, an integer, an integer, a pointer, returning integer.
1417 const FunctionType *FT = Callee->getFunctionType();
1418 if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
1419 !isa<IntegerType>(FT->getParamType(1)) ||
1420 !isa<IntegerType>(FT->getParamType(2)) ||
1421 !isa<PointerType>(FT->getParamType(3)) ||
1422 !isa<IntegerType>(FT->getReturnType()))
1423 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001424
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001425 // Get the element size and count.
1426 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1427 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1428 if (!SizeC || !CountC) return 0;
1429 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001430
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001431 // If this is writing zero records, remove the call (it's a noop).
1432 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001433 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001434
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001435 // If this is writing one byte, turn it into fputc.
1436 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1437 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1438 EmitFPutC(Char, CI->getOperand(4), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001439 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001440 }
1441
1442 return 0;
1443 }
1444};
1445
1446//===---------------------------------------===//
1447// 'fputs' Optimizations
1448
Chris Lattner3e8b6632009-09-02 06:11:42 +00001449struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001450 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001451 // These optimizations require TargetData.
1452 if (!TD) return 0;
1453
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001454 // Require two pointers. Also, we can't optimize if return value is used.
1455 const FunctionType *FT = Callee->getFunctionType();
1456 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1457 !isa<PointerType>(FT->getParamType(1)) ||
1458 !CI->use_empty())
1459 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001460
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001461 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1462 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001463 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001464 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001465 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001466 CI->getOperand(2), B);
1467 return CI; // Known to have no uses (see above).
1468 }
1469};
1470
1471//===---------------------------------------===//
1472// 'fprintf' Optimizations
1473
Chris Lattner3e8b6632009-09-02 06:11:42 +00001474struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001475 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001476 // Require two fixed paramters as pointers and integer result.
1477 const FunctionType *FT = Callee->getFunctionType();
1478 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1479 !isa<PointerType>(FT->getParamType(1)) ||
1480 !isa<IntegerType>(FT->getReturnType()))
1481 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001482
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001483 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001484 std::string FormatStr;
1485 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1486 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001487
1488 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1489 if (CI->getNumOperands() == 3) {
1490 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1491 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001492 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001493
1494 // These optimizations require TargetData.
1495 if (!TD) return 0;
1496
Owen Anderson1d0be152009-08-13 21:58:54 +00001497 EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001498 FormatStr.size()),
1499 CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001500 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001501 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001502
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001503 // The remaining optimizations require the format string to be "%s" or "%c"
1504 // and have an extra operand.
1505 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1506 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001507
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001508 // Decode the second character of the format string.
1509 if (FormatStr[1] == 'c') {
1510 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1511 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1512 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001513 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001514 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001515
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001516 if (FormatStr[1] == 's') {
1517 // fprintf(F, "%s", str) -> fputs(str, F)
1518 if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
1519 return 0;
1520 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1521 return CI;
1522 }
1523 return 0;
1524 }
1525};
1526
Bill Wendlingac178222008-05-05 21:37:59 +00001527} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001528
1529//===----------------------------------------------------------------------===//
1530// SimplifyLibCalls Pass Implementation
1531//===----------------------------------------------------------------------===//
1532
1533namespace {
1534 /// This pass optimizes well known library functions from libc and libm.
1535 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001536 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001537 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001538 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001539 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1540 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
1541 StrToOpt StrTo; MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove;
1542 MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001543 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001544 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001545 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001546 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1547 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001548 // Formatting and IO Optimizations
1549 SPrintFOpt SPrintF; PrintFOpt PrintF;
1550 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001551
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001552 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001553 public:
1554 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001555 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001556
1557 void InitOptimizations();
1558 bool runOnFunction(Function &F);
1559
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001560 void setDoesNotAccessMemory(Function &F);
1561 void setOnlyReadsMemory(Function &F);
1562 void setDoesNotThrow(Function &F);
1563 void setDoesNotCapture(Function &F, unsigned n);
1564 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001565 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001566
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001567 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001568 }
1569 };
1570 char SimplifyLibCalls::ID = 0;
1571} // end anonymous namespace.
1572
1573static RegisterPass<SimplifyLibCalls>
1574X("simplify-libcalls", "Simplify well-known library calls");
1575
1576// Public interface to the Simplify LibCalls pass.
1577FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001578 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001579}
1580
1581/// Optimizations - Populate the Optimizations map with all the optimizations
1582/// we know.
1583void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001584 // String and Memory LibCall Optimizations
1585 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001586 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001587 Optimizations["strchr"] = &StrChr;
1588 Optimizations["strcmp"] = &StrCmp;
1589 Optimizations["strncmp"] = &StrNCmp;
1590 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001591 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001592 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001593 Optimizations["strtol"] = &StrTo;
1594 Optimizations["strtod"] = &StrTo;
1595 Optimizations["strtof"] = &StrTo;
1596 Optimizations["strtoul"] = &StrTo;
1597 Optimizations["strtoll"] = &StrTo;
1598 Optimizations["strtold"] = &StrTo;
1599 Optimizations["strtoull"] = &StrTo;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001600 Optimizations["memcmp"] = &MemCmp;
1601 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001602 Optimizations["memmove"] = &MemMove;
1603 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001604
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001605 // Math Library Optimizations
1606 Optimizations["powf"] = &Pow;
1607 Optimizations["pow"] = &Pow;
1608 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001609 Optimizations["llvm.pow.f32"] = &Pow;
1610 Optimizations["llvm.pow.f64"] = &Pow;
1611 Optimizations["llvm.pow.f80"] = &Pow;
1612 Optimizations["llvm.pow.f128"] = &Pow;
1613 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001614 Optimizations["exp2l"] = &Exp2;
1615 Optimizations["exp2"] = &Exp2;
1616 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001617 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1618 Optimizations["llvm.exp2.f128"] = &Exp2;
1619 Optimizations["llvm.exp2.f80"] = &Exp2;
1620 Optimizations["llvm.exp2.f64"] = &Exp2;
1621 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001622
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001623#ifdef HAVE_FLOORF
1624 Optimizations["floor"] = &UnaryDoubleFP;
1625#endif
1626#ifdef HAVE_CEILF
1627 Optimizations["ceil"] = &UnaryDoubleFP;
1628#endif
1629#ifdef HAVE_ROUNDF
1630 Optimizations["round"] = &UnaryDoubleFP;
1631#endif
1632#ifdef HAVE_RINTF
1633 Optimizations["rint"] = &UnaryDoubleFP;
1634#endif
1635#ifdef HAVE_NEARBYINTF
1636 Optimizations["nearbyint"] = &UnaryDoubleFP;
1637#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001638
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001639 // Integer Optimizations
1640 Optimizations["ffs"] = &FFS;
1641 Optimizations["ffsl"] = &FFS;
1642 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001643 Optimizations["abs"] = &Abs;
1644 Optimizations["labs"] = &Abs;
1645 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001646 Optimizations["isdigit"] = &IsDigit;
1647 Optimizations["isascii"] = &IsAscii;
1648 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001649
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001650 // Formatting and IO Optimizations
1651 Optimizations["sprintf"] = &SPrintF;
1652 Optimizations["printf"] = &PrintF;
1653 Optimizations["fwrite"] = &FWrite;
1654 Optimizations["fputs"] = &FPuts;
1655 Optimizations["fprintf"] = &FPrintF;
1656}
1657
1658
1659/// runOnFunction - Top level algorithm.
1660///
1661bool SimplifyLibCalls::runOnFunction(Function &F) {
1662 if (Optimizations.empty())
1663 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001664
Dan Gohmanf14d9192009-08-18 00:48:13 +00001665 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001666
Owen Andersone922c022009-07-22 00:24:57 +00001667 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001668
1669 bool Changed = false;
1670 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1671 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1672 // Ignore non-calls.
1673 CallInst *CI = dyn_cast<CallInst>(I++);
1674 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001675
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001676 // Ignore indirect calls and calls to non-external functions.
1677 Function *Callee = CI->getCalledFunction();
1678 if (Callee == 0 || !Callee->isDeclaration() ||
1679 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1680 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001681
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001682 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001683 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1684 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001685
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001686 // Set the builder to the instruction after the call.
1687 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001688
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001689 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001690 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001691 if (Result == 0) continue;
1692
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001693 DEBUG(errs() << "SimplifyLibCalls simplified: " << *CI;
1694 errs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001695
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001696 // Something changed!
1697 Changed = true;
1698 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001699
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001700 // Inspect the instruction after the call (which was potentially just
1701 // added) next.
1702 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001703
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001704 if (CI != Result && !CI->use_empty()) {
1705 CI->replaceAllUsesWith(Result);
1706 if (!Result->hasName())
1707 Result->takeName(CI);
1708 }
1709 CI->eraseFromParent();
1710 }
1711 }
1712 return Changed;
1713}
1714
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001715// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001716
1717void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1718 if (!F.doesNotAccessMemory()) {
1719 F.setDoesNotAccessMemory();
1720 ++NumAnnotated;
1721 Modified = true;
1722 }
1723}
1724void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1725 if (!F.onlyReadsMemory()) {
1726 F.setOnlyReadsMemory();
1727 ++NumAnnotated;
1728 Modified = true;
1729 }
1730}
1731void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1732 if (!F.doesNotThrow()) {
1733 F.setDoesNotThrow();
1734 ++NumAnnotated;
1735 Modified = true;
1736 }
1737}
1738void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1739 if (!F.doesNotCapture(n)) {
1740 F.setDoesNotCapture(n);
1741 ++NumAnnotated;
1742 Modified = true;
1743 }
1744}
1745void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1746 if (!F.doesNotAlias(n)) {
1747 F.setDoesNotAlias(n);
1748 ++NumAnnotated;
1749 Modified = true;
1750 }
1751}
1752
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001753/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001754///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001755bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001756 Modified = false;
1757 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1758 Function &F = *I;
1759 if (!F.isDeclaration())
1760 continue;
1761
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001762 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001763 continue;
1764
1765 const FunctionType *FTy = F.getFunctionType();
1766
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001767 StringRef Name = F.getName();
1768 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001769 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001770 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001771 if (FTy->getNumParams() != 1 ||
1772 !isa<PointerType>(FTy->getParamType(0)))
1773 continue;
1774 setOnlyReadsMemory(F);
1775 setDoesNotThrow(F);
1776 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001777 } else if (Name == "strcpy" ||
1778 Name == "stpcpy" ||
1779 Name == "strcat" ||
1780 Name == "strtol" ||
1781 Name == "strtod" ||
1782 Name == "strtof" ||
1783 Name == "strtoul" ||
1784 Name == "strtoll" ||
1785 Name == "strtold" ||
1786 Name == "strncat" ||
1787 Name == "strncpy" ||
1788 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001789 if (FTy->getNumParams() < 2 ||
1790 !isa<PointerType>(FTy->getParamType(1)))
1791 continue;
1792 setDoesNotThrow(F);
1793 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001794 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001795 if (FTy->getNumParams() != 3 ||
1796 !isa<PointerType>(FTy->getParamType(0)) ||
1797 !isa<PointerType>(FTy->getParamType(1)))
1798 continue;
1799 setDoesNotThrow(F);
1800 setDoesNotCapture(F, 1);
1801 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001802 } else if (Name == "strcmp" ||
1803 Name == "strspn" ||
1804 Name == "strncmp" ||
1805 Name ==" strcspn" ||
1806 Name == "strcoll" ||
1807 Name == "strcasecmp" ||
1808 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001809 if (FTy->getNumParams() < 2 ||
1810 !isa<PointerType>(FTy->getParamType(0)) ||
1811 !isa<PointerType>(FTy->getParamType(1)))
1812 continue;
1813 setOnlyReadsMemory(F);
1814 setDoesNotThrow(F);
1815 setDoesNotCapture(F, 1);
1816 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001817 } else if (Name == "strstr" ||
1818 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001819 if (FTy->getNumParams() != 2 ||
1820 !isa<PointerType>(FTy->getParamType(1)))
1821 continue;
1822 setOnlyReadsMemory(F);
1823 setDoesNotThrow(F);
1824 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001825 } else if (Name == "strtok" ||
1826 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001827 if (FTy->getNumParams() < 2 ||
1828 !isa<PointerType>(FTy->getParamType(1)))
1829 continue;
1830 setDoesNotThrow(F);
1831 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001832 } else if (Name == "scanf" ||
1833 Name == "setbuf" ||
1834 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001835 if (FTy->getNumParams() < 1 ||
1836 !isa<PointerType>(FTy->getParamType(0)))
1837 continue;
1838 setDoesNotThrow(F);
1839 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001840 } else if (Name == "strdup" ||
1841 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001842 if (FTy->getNumParams() < 1 ||
1843 !isa<PointerType>(FTy->getReturnType()) ||
1844 !isa<PointerType>(FTy->getParamType(0)))
1845 continue;
1846 setDoesNotThrow(F);
1847 setDoesNotAlias(F, 0);
1848 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001849 } else if (Name == "stat" ||
1850 Name == "sscanf" ||
1851 Name == "sprintf" ||
1852 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001853 if (FTy->getNumParams() < 2 ||
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001854 !isa<PointerType>(FTy->getParamType(0)) ||
1855 !isa<PointerType>(FTy->getParamType(1)))
1856 continue;
1857 setDoesNotThrow(F);
1858 setDoesNotCapture(F, 1);
1859 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001860 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001861 if (FTy->getNumParams() != 3 ||
1862 !isa<PointerType>(FTy->getParamType(0)) ||
1863 !isa<PointerType>(FTy->getParamType(2)))
1864 continue;
1865 setDoesNotThrow(F);
1866 setDoesNotCapture(F, 1);
1867 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001868 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001869 if (FTy->getNumParams() != 3 ||
1870 !isa<PointerType>(FTy->getParamType(1)) ||
1871 !isa<PointerType>(FTy->getParamType(2)))
1872 continue;
1873 setDoesNotThrow(F);
1874 setDoesNotCapture(F, 2);
1875 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001876 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001877 if (FTy->getNumParams() != 1 ||
1878 !isa<PointerType>(FTy->getParamType(0)))
1879 continue;
1880 // May throw; "system" is a valid pthread cancellation point.
1881 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001882 }
1883 break;
1884 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001885 if (Name == "malloc") {
1886 if (FTy->getNumParams() != 1 ||
1887 !isa<PointerType>(FTy->getReturnType()))
1888 continue;
1889 setDoesNotThrow(F);
1890 setDoesNotAlias(F, 0);
1891 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001892 if (FTy->getNumParams() != 3 ||
1893 !isa<PointerType>(FTy->getParamType(0)) ||
1894 !isa<PointerType>(FTy->getParamType(1)))
1895 continue;
1896 setOnlyReadsMemory(F);
1897 setDoesNotThrow(F);
1898 setDoesNotCapture(F, 1);
1899 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001900 } else if (Name == "memchr" ||
1901 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001902 if (FTy->getNumParams() != 3)
1903 continue;
1904 setOnlyReadsMemory(F);
1905 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001906 } else if (Name == "modf" ||
1907 Name == "modff" ||
1908 Name == "modfl" ||
1909 Name == "memcpy" ||
1910 Name == "memccpy" ||
1911 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001912 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001913 !isa<PointerType>(FTy->getParamType(1)))
1914 continue;
1915 setDoesNotThrow(F);
1916 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001917 } else if (Name == "memalign") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001918 if (!isa<PointerType>(FTy->getReturnType()))
1919 continue;
1920 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001921 } else if (Name == "mkdir" ||
1922 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001923 if (FTy->getNumParams() == 0 ||
1924 !isa<PointerType>(FTy->getParamType(0)))
1925 continue;
1926 setDoesNotThrow(F);
1927 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001928 }
1929 break;
1930 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001931 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001932 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001933 !isa<PointerType>(FTy->getParamType(0)) ||
1934 !isa<PointerType>(FTy->getReturnType()))
1935 continue;
1936 setDoesNotThrow(F);
1937 setDoesNotAlias(F, 0);
1938 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001939 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001940 if (FTy->getNumParams() != 3 ||
1941 !isa<PointerType>(FTy->getParamType(1)))
1942 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001943 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001944 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001945 } else if (Name == "rmdir" ||
1946 Name == "rewind" ||
1947 Name == "remove" ||
1948 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001949 if (FTy->getNumParams() < 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001950 !isa<PointerType>(FTy->getParamType(0)))
1951 continue;
1952 setDoesNotThrow(F);
1953 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001954 } else if (Name == "rename" ||
1955 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001956 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001957 !isa<PointerType>(FTy->getParamType(0)) ||
1958 !isa<PointerType>(FTy->getParamType(1)))
1959 continue;
1960 setDoesNotThrow(F);
1961 setDoesNotCapture(F, 1);
1962 setDoesNotCapture(F, 2);
1963 }
1964 break;
1965 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001966 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001967 if (FTy->getNumParams() != 3 ||
1968 !isa<PointerType>(FTy->getParamType(1)))
1969 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001970 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001971 setDoesNotCapture(F, 2);
1972 }
1973 break;
1974 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001975 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001976 if (FTy->getNumParams() != 3 ||
1977 !isa<PointerType>(FTy->getParamType(0)) ||
1978 !isa<PointerType>(FTy->getParamType(1)))
1979 continue;
1980 setDoesNotThrow(F);
1981 setDoesNotCapture(F, 1);
1982 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001983 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001984 if (FTy->getNumParams() != 3 ||
1985 !isa<PointerType>(FTy->getParamType(0)) ||
1986 !isa<PointerType>(FTy->getParamType(1)))
1987 continue;
1988 setDoesNotThrow(F);
1989 setOnlyReadsMemory(F);
1990 setDoesNotCapture(F, 1);
1991 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001992 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001993 if (FTy->getNumParams() != 2 ||
1994 !isa<PointerType>(FTy->getParamType(0)))
1995 continue;
1996 setDoesNotThrow(F);
1997 setDoesNotCapture(F, 1);
1998 }
1999 break;
2000 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002001 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002002 if (FTy->getNumParams() != 2 ||
2003 !isa<PointerType>(FTy->getReturnType()))
2004 continue;
2005 setDoesNotThrow(F);
2006 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002007 } else if (Name == "chmod" ||
2008 Name == "chown" ||
2009 Name == "ctermid" ||
2010 Name == "clearerr" ||
2011 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002012 if (FTy->getNumParams() == 0 ||
2013 !isa<PointerType>(FTy->getParamType(0)))
2014 continue;
2015 setDoesNotThrow(F);
2016 setDoesNotCapture(F, 1);
2017 }
2018 break;
2019 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002020 if (Name == "atoi" ||
2021 Name == "atol" ||
2022 Name == "atof" ||
2023 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002024 if (FTy->getNumParams() != 1 ||
2025 !isa<PointerType>(FTy->getParamType(0)))
2026 continue;
2027 setDoesNotThrow(F);
2028 setOnlyReadsMemory(F);
2029 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002030 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002031 if (FTy->getNumParams() != 2 ||
2032 !isa<PointerType>(FTy->getParamType(0)))
2033 continue;
2034 setDoesNotThrow(F);
2035 setDoesNotCapture(F, 1);
2036 }
2037 break;
2038 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002039 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002040 if (FTy->getNumParams() != 2 ||
2041 !isa<PointerType>(FTy->getReturnType()) ||
2042 !isa<PointerType>(FTy->getParamType(0)) ||
2043 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002044 continue;
2045 setDoesNotThrow(F);
2046 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002047 setDoesNotCapture(F, 1);
2048 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002049 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002050 if (FTy->getNumParams() != 2 ||
2051 !isa<PointerType>(FTy->getReturnType()) ||
2052 !isa<PointerType>(FTy->getParamType(1)))
2053 continue;
2054 setDoesNotThrow(F);
2055 setDoesNotAlias(F, 0);
2056 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002057 } else if (Name == "feof" ||
2058 Name == "free" ||
2059 Name == "fseek" ||
2060 Name == "ftell" ||
2061 Name == "fgetc" ||
2062 Name == "fseeko" ||
2063 Name == "ftello" ||
2064 Name == "fileno" ||
2065 Name == "fflush" ||
2066 Name == "fclose" ||
2067 Name == "fsetpos" ||
2068 Name == "flockfile" ||
2069 Name == "funlockfile" ||
2070 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002071 if (FTy->getNumParams() == 0 ||
2072 !isa<PointerType>(FTy->getParamType(0)))
2073 continue;
2074 setDoesNotThrow(F);
2075 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002076 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002077 if (FTy->getNumParams() != 1 ||
2078 !isa<PointerType>(FTy->getParamType(0)))
2079 continue;
2080 setDoesNotThrow(F);
2081 setDoesNotCapture(F, 1);
2082 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002083 } else if (Name == "fputc" ||
2084 Name == "fstat" ||
2085 Name == "frexp" ||
2086 Name == "frexpf" ||
2087 Name == "frexpl" ||
2088 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002089 if (FTy->getNumParams() != 2 ||
2090 !isa<PointerType>(FTy->getParamType(1)))
2091 continue;
2092 setDoesNotThrow(F);
2093 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002094 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002095 if (FTy->getNumParams() != 3 ||
2096 !isa<PointerType>(FTy->getParamType(0)) ||
2097 !isa<PointerType>(FTy->getParamType(2)))
2098 continue;
2099 setDoesNotThrow(F);
2100 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002101 } else if (Name == "fread" ||
2102 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002103 if (FTy->getNumParams() != 4 ||
2104 !isa<PointerType>(FTy->getParamType(0)) ||
2105 !isa<PointerType>(FTy->getParamType(3)))
2106 continue;
2107 setDoesNotThrow(F);
2108 setDoesNotCapture(F, 1);
2109 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002110 } else if (Name == "fputs" ||
2111 Name == "fscanf" ||
2112 Name == "fprintf" ||
2113 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002114 if (FTy->getNumParams() < 2 ||
2115 !isa<PointerType>(FTy->getParamType(0)) ||
2116 !isa<PointerType>(FTy->getParamType(1)))
2117 continue;
2118 setDoesNotThrow(F);
2119 setDoesNotCapture(F, 1);
2120 setDoesNotCapture(F, 2);
2121 }
2122 break;
2123 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002124 if (Name == "getc" ||
2125 Name == "getlogin_r" ||
2126 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002127 if (FTy->getNumParams() == 0 ||
2128 !isa<PointerType>(FTy->getParamType(0)))
2129 continue;
2130 setDoesNotThrow(F);
2131 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002132 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002133 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002134 !isa<PointerType>(FTy->getParamType(0)))
2135 continue;
2136 setDoesNotThrow(F);
2137 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002138 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002139 } else if (Name == "gets" ||
2140 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002141 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002142 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002143 if (FTy->getNumParams() != 2 ||
2144 !isa<PointerType>(FTy->getParamType(1)))
2145 continue;
2146 setDoesNotThrow(F);
2147 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002148 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002149 if (FTy->getNumParams() != 1 ||
2150 !isa<PointerType>(FTy->getParamType(0)))
2151 continue;
2152 setDoesNotThrow(F);
2153 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002154 }
2155 break;
2156 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002157 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002158 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002159 !isa<PointerType>(FTy->getParamType(1)))
2160 continue;
2161 setDoesNotThrow(F);
2162 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002163 } else if (Name == "uname" ||
2164 Name == "unlink" ||
2165 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002166 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002167 !isa<PointerType>(FTy->getParamType(0)))
2168 continue;
2169 setDoesNotThrow(F);
2170 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002171 } else if (Name == "utime" ||
2172 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002173 if (FTy->getNumParams() != 2 ||
2174 !isa<PointerType>(FTy->getParamType(0)) ||
2175 !isa<PointerType>(FTy->getParamType(1)))
2176 continue;
2177 setDoesNotThrow(F);
2178 setDoesNotCapture(F, 1);
2179 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002180 }
2181 break;
2182 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002183 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002184 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002185 !isa<PointerType>(FTy->getParamType(1)))
2186 continue;
2187 setDoesNotThrow(F);
2188 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002189 } else if (Name == "puts" ||
2190 Name == "printf" ||
2191 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002192 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002193 !isa<PointerType>(FTy->getParamType(0)))
2194 continue;
2195 setDoesNotThrow(F);
2196 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002197 } else if (Name == "pread" ||
2198 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002199 if (FTy->getNumParams() != 4 ||
2200 !isa<PointerType>(FTy->getParamType(1)))
2201 continue;
2202 // May throw; these are valid pthread cancellation points.
2203 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002204 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002205 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002206 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002207 if (FTy->getNumParams() != 2 ||
2208 !isa<PointerType>(FTy->getReturnType()) ||
2209 !isa<PointerType>(FTy->getParamType(0)) ||
2210 !isa<PointerType>(FTy->getParamType(1)))
2211 continue;
2212 setDoesNotThrow(F);
2213 setDoesNotAlias(F, 0);
2214 setDoesNotCapture(F, 1);
2215 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002216 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002217 if (FTy->getNumParams() != 1 ||
2218 !isa<PointerType>(FTy->getParamType(0)))
2219 continue;
2220 setDoesNotThrow(F);
2221 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002222 }
2223 break;
2224 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002225 if (Name == "vscanf") {
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, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002231 } else if (Name == "vsscanf" ||
2232 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002233 if (FTy->getNumParams() != 3 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002234 !isa<PointerType>(FTy->getParamType(1)) ||
2235 !isa<PointerType>(FTy->getParamType(2)))
2236 continue;
2237 setDoesNotThrow(F);
2238 setDoesNotCapture(F, 1);
2239 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002240 } else if (Name == "valloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002241 if (!isa<PointerType>(FTy->getReturnType()))
2242 continue;
2243 setDoesNotThrow(F);
2244 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002245 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002246 if (FTy->getNumParams() != 2 ||
2247 !isa<PointerType>(FTy->getParamType(0)))
2248 continue;
2249 setDoesNotThrow(F);
2250 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002251 } else if (Name == "vfprintf" ||
2252 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002253 if (FTy->getNumParams() != 3 ||
2254 !isa<PointerType>(FTy->getParamType(0)) ||
2255 !isa<PointerType>(FTy->getParamType(1)))
2256 continue;
2257 setDoesNotThrow(F);
2258 setDoesNotCapture(F, 1);
2259 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002260 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002261 if (FTy->getNumParams() != 4 ||
2262 !isa<PointerType>(FTy->getParamType(0)) ||
2263 !isa<PointerType>(FTy->getParamType(2)))
2264 continue;
2265 setDoesNotThrow(F);
2266 setDoesNotCapture(F, 1);
2267 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002268 }
2269 break;
2270 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002271 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002272 if (FTy->getNumParams() < 2 ||
2273 !isa<PointerType>(FTy->getParamType(0)))
2274 continue;
2275 // May throw; "open" is a valid pthread cancellation point.
2276 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002277 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002278 if (FTy->getNumParams() != 1 ||
2279 !isa<PointerType>(FTy->getReturnType()) ||
2280 !isa<PointerType>(FTy->getParamType(0)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002281 continue;
2282 setDoesNotThrow(F);
2283 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002284 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002285 }
2286 break;
2287 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002288 if (Name == "tmpfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002289 if (!isa<PointerType>(FTy->getReturnType()))
2290 continue;
2291 setDoesNotThrow(F);
2292 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002293 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002294 if (FTy->getNumParams() != 1 ||
2295 !isa<PointerType>(FTy->getParamType(0)))
2296 continue;
2297 setDoesNotThrow(F);
2298 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002299 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002300 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002301 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002302 if (Name == "htonl" ||
2303 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002304 setDoesNotThrow(F);
2305 setDoesNotAccessMemory(F);
2306 }
2307 break;
2308 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002309 if (Name == "ntohl" ||
2310 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002311 setDoesNotThrow(F);
2312 setDoesNotAccessMemory(F);
2313 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002314 break;
2315 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002316 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002317 if (FTy->getNumParams() != 2 ||
2318 !isa<PointerType>(FTy->getParamType(0)) ||
2319 !isa<PointerType>(FTy->getParamType(1)))
2320 continue;
2321 setDoesNotThrow(F);
2322 setDoesNotCapture(F, 1);
2323 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002324 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002325 if (FTy->getNumParams() != 3 ||
2326 !isa<PointerType>(FTy->getParamType(0)))
2327 continue;
2328 setDoesNotThrow(F);
2329 setDoesNotCapture(F, 1);
2330 }
2331 break;
2332 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002333 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002334 if (FTy->getNumParams() != 4 ||
2335 !isa<PointerType>(FTy->getParamType(3)))
2336 continue;
2337 // May throw; places call through function pointer.
2338 setDoesNotCapture(F, 4);
2339 }
2340 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002341 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002342 if (Name == "__strdup" ||
2343 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002344 if (FTy->getNumParams() < 1 ||
2345 !isa<PointerType>(FTy->getReturnType()) ||
2346 !isa<PointerType>(FTy->getParamType(0)))
2347 continue;
2348 setDoesNotThrow(F);
2349 setDoesNotAlias(F, 0);
2350 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002351 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002352 if (FTy->getNumParams() != 3 ||
2353 !isa<PointerType>(FTy->getParamType(1)))
2354 continue;
2355 setDoesNotThrow(F);
2356 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002357 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002358 if (FTy->getNumParams() != 1 ||
2359 !isa<PointerType>(FTy->getParamType(0)))
2360 continue;
2361 setDoesNotThrow(F);
2362 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002363 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002364 if (FTy->getNumParams() != 2 ||
2365 !isa<PointerType>(FTy->getParamType(1)))
2366 continue;
2367 setDoesNotThrow(F);
2368 setDoesNotCapture(F, 2);
2369 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002370 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002371 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002372 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002373 if (FTy->getNumParams() < 1 ||
2374 !isa<PointerType>(FTy->getParamType(0)))
2375 continue;
2376 setDoesNotThrow(F);
2377 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002378 } else if (Name == "\1stat64" ||
2379 Name == "\1lstat64" ||
2380 Name == "\1statvfs64" ||
2381 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002382 if (FTy->getNumParams() < 1 ||
Nick Lewycky225f7472009-02-15 22:47:25 +00002383 !isa<PointerType>(FTy->getParamType(0)) ||
2384 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002385 continue;
2386 setDoesNotThrow(F);
2387 setDoesNotCapture(F, 1);
2388 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002389 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002390 if (FTy->getNumParams() != 2 ||
2391 !isa<PointerType>(FTy->getReturnType()) ||
2392 !isa<PointerType>(FTy->getParamType(0)) ||
2393 !isa<PointerType>(FTy->getParamType(1)))
2394 continue;
2395 setDoesNotThrow(F);
2396 setDoesNotAlias(F, 0);
2397 setDoesNotCapture(F, 1);
2398 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002399 } else if (Name == "\1fseeko64" ||
2400 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002401 if (FTy->getNumParams() == 0 ||
2402 !isa<PointerType>(FTy->getParamType(0)))
2403 continue;
2404 setDoesNotThrow(F);
2405 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002406 } else if (Name == "\1tmpfile64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002407 if (!isa<PointerType>(FTy->getReturnType()))
2408 continue;
2409 setDoesNotThrow(F);
2410 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002411 } else if (Name == "\1fstat64" ||
2412 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002413 if (FTy->getNumParams() != 2 ||
2414 !isa<PointerType>(FTy->getParamType(1)))
2415 continue;
2416 setDoesNotThrow(F);
2417 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002418 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002419 if (FTy->getNumParams() < 2 ||
2420 !isa<PointerType>(FTy->getParamType(0)))
2421 continue;
2422 // May throw; "open" is a valid pthread cancellation point.
2423 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002424 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002425 break;
2426 }
2427 }
2428 return Modified;
2429}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002430
2431// TODO:
2432// Additional cases that we need to add to this file:
2433//
2434// cbrt:
2435// * cbrt(expN(X)) -> expN(x/3)
2436// * cbrt(sqrt(x)) -> pow(x,1/6)
2437// * cbrt(sqrt(x)) -> pow(x,1/9)
2438//
2439// cos, cosf, cosl:
2440// * cos(-x) -> cos(x)
2441//
2442// exp, expf, expl:
2443// * exp(log(x)) -> x
2444//
2445// log, logf, logl:
2446// * log(exp(x)) -> x
2447// * log(x**y) -> y*log(x)
2448// * log(exp(y)) -> y*log(e)
2449// * log(exp2(y)) -> y*log(2)
2450// * log(exp10(y)) -> y*log(10)
2451// * log(sqrt(x)) -> 0.5*log(x)
2452// * log(pow(x,y)) -> y*log(x)
2453//
2454// lround, lroundf, lroundl:
2455// * lround(cnst) -> cnst'
2456//
2457// memcmp:
2458// * memcmp(x,y,l) -> cnst
2459// (if all arguments are constant and strlen(x) <= l and strlen(y) <= l)
2460//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002461// pow, powf, powl:
2462// * pow(exp(x),y) -> exp(x*y)
2463// * pow(sqrt(x),y) -> pow(x,y*0.5)
2464// * pow(pow(x,y),z)-> pow(x,y*z)
2465//
2466// puts:
2467// * puts("") -> putchar("\n")
2468//
2469// round, roundf, roundl:
2470// * round(cnst) -> cnst'
2471//
2472// signbit:
2473// * signbit(cnst) -> cnst'
2474// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2475//
2476// sqrt, sqrtf, sqrtl:
2477// * sqrt(expN(x)) -> expN(x*0.5)
2478// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2479// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2480//
2481// stpcpy:
2482// * stpcpy(str, "literal") ->
2483// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2484// strrchr:
2485// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2486// (if c is a constant integer and s is a constant string)
2487// * strrchr(s1,0) -> strchr(s1,0)
2488//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002489// strpbrk:
2490// * strpbrk(s,a) -> offset_in_for(s,a)
2491// (if s and a are both constant strings)
2492// * strpbrk(s,"") -> 0
2493// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2494//
2495// strspn, strcspn:
2496// * strspn(s,a) -> const_int (if both args are constant)
2497// * strspn("",a) -> 0
2498// * strspn(s,"") -> 0
2499// * strcspn(s,a) -> const_int (if both args are constant)
2500// * strcspn("",a) -> 0
2501// * strcspn(s,"") -> strlen(a)
2502//
2503// strstr:
2504// * strstr(x,x) -> x
2505// * strstr(s1,s2) -> offset_of_s2_in(s1)
2506// (if s1 and s2 are constant strings)
2507//
2508// tan, tanf, tanl:
2509// * tan(atan(x)) -> x
2510//
2511// trunc, truncf, truncl:
2512// * trunc(cnst) -> cnst'
2513//
2514//