blob: 0d03e55eba7644f17867d9b65ddf4a0fe8562199 [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
Eric Christopher80bf1d52009-11-21 01:01:30 +000084 /// EmitMemMove - Emit a call to the memmove function to the builder. This
85 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
86 Value *EmitMemMove(Value *Dst, Value *Src, Value *Len,
87 unsigned Align, IRBuilder<> &B);
88
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000089 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
90 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
Eric Christopher7a61d702008-08-08 19:39:37 +000091 Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B);
Nick Lewycky13a09e22008-12-21 00:19:21 +000092
93 /// EmitMemCmp - Emit a call to the memcmp function.
94 Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B);
95
Chris Lattnerf5b6bc72009-04-12 05:06:39 +000096 /// EmitMemSet - Emit a call to the memset function
97 Value *EmitMemSet(Value *Dst, Value *Val, Value *Len, IRBuilder<> &B);
98
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000099 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
100 /// 'floor'). This function is known to take a single of type matching 'Op'
101 /// and returns one value with the same type. If 'Op' is a long double, 'l'
102 /// is added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
Dan Gohman79cb8402009-09-25 23:10:17 +0000103 Value *EmitUnaryFloatFnCall(Value *Op, const char *Name, IRBuilder<> &B,
104 const AttrListPtr &Attrs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000105
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000106 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
107 /// is an integer.
Chris Lattner74965f22009-11-09 04:57:04 +0000108 Value *EmitPutChar(Value *Char, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000109
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000110 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
111 /// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000112 void EmitPutS(Value *Str, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000113
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000114 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
115 /// an i32, and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000116 void EmitFPutC(Value *Char, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000117
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000118 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
119 /// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000120 void EmitFPutS(Value *Str, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000121
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000122 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
123 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000124 void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000125
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000126};
127} // End anonymous namespace.
128
129/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Eric Christopher7a61d702008-08-08 19:39:37 +0000130Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) {
Chris Lattnerbf796322009-12-02 06:05:42 +0000131 return B.CreateBitCast(V, Type::getInt8PtrTy(*Context), "cstr");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000132}
133
134/// EmitStrLen - Emit a call to the strlen function to the builder, for the
135/// specified pointer. This always returns an integer value of size intptr_t.
Eric Christopher7a61d702008-08-08 19:39:37 +0000136Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000137 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000138 AttributeWithIndex AWI[2];
139 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
140 AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
141 Attribute::NoUnwind);
142
143 Constant *StrLen =M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000144 TD->getIntPtrType(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000145 Type::getInt8PtrTy(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000146 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000147 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
148 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
149 CI->setCallingConv(F->getCallingConv());
150
151 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000152}
153
154/// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
155/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
156Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
Eric Christopher7a61d702008-08-08 19:39:37 +0000157 unsigned Align, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000158 Module *M = Caller->getParent();
Chris Lattnerbf796322009-12-02 06:05:42 +0000159 const Type *Ty = Len->getType();
160 Value *MemCpy = Intrinsic::getDeclaration(M, Intrinsic::memcpy, &Ty, 1);
161 Dst = CastToCStr(Dst, B);
162 Src = CastToCStr(Src, B);
163 return B.CreateCall4(MemCpy, Dst, Src, Len,
Owen Anderson1d0be152009-08-13 21:58:54 +0000164 ConstantInt::get(Type::getInt32Ty(*Context), Align));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000165}
166
Chris Lattnerbf796322009-12-02 06:05:42 +0000167/// EmitMemMove - Emit a call to the memmove function to the builder. This
Eric Christopher80bf1d52009-11-21 01:01:30 +0000168/// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
169Value *LibCallOptimization::EmitMemMove(Value *Dst, Value *Src, Value *Len,
170 unsigned Align, IRBuilder<> &B) {
171 Module *M = Caller->getParent();
Chris Lattnerbf796322009-12-02 06:05:42 +0000172 const Type *Ty = TD->getIntPtrType(*Context);
173 Value *MemMove = Intrinsic::getDeclaration(M, Intrinsic::memmove, &Ty, 1);
174 Dst = CastToCStr(Dst, B);
175 Src = CastToCStr(Src, B);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000176 Value *A = ConstantInt::get(Type::getInt32Ty(*Context), Align);
Chris Lattnerbf796322009-12-02 06:05:42 +0000177 return B.CreateCall4(MemMove, Dst, Src, Len, A);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000178}
179
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000180/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
181/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
182Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
Eric Christopher7a61d702008-08-08 19:39:37 +0000183 Value *Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000184 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000185 AttributeWithIndex AWI;
186 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
187
188 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
Eric Christopher37c8b862009-10-07 21:14:25 +0000189 Type::getInt8PtrTy(*Context),
190 Type::getInt8PtrTy(*Context),
191 Type::getInt32Ty(*Context),
192 TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000193 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000194 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
195
196 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
197 CI->setCallingConv(F->getCallingConv());
198
199 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000200}
201
Nick Lewycky13a09e22008-12-21 00:19:21 +0000202/// EmitMemCmp - Emit a call to the memcmp function.
203Value *LibCallOptimization::EmitMemCmp(Value *Ptr1, Value *Ptr2,
204 Value *Len, IRBuilder<> &B) {
205 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000206 AttributeWithIndex AWI[3];
207 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
208 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
209 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
210 Attribute::NoUnwind);
211
212 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000213 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000214 Type::getInt8PtrTy(*Context),
215 Type::getInt8PtrTy(*Context),
Owen Anderson1d0be152009-08-13 21:58:54 +0000216 TD->getIntPtrType(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000217 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
218 Len, "memcmp");
219
220 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
221 CI->setCallingConv(F->getCallingConv());
222
223 return CI;
Nick Lewycky13a09e22008-12-21 00:19:21 +0000224}
225
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000226/// EmitMemSet - Emit a call to the memset function
227Value *LibCallOptimization::EmitMemSet(Value *Dst, Value *Val,
228 Value *Len, IRBuilder<> &B) {
229 Module *M = Caller->getParent();
230 Intrinsic::ID IID = Intrinsic::memset;
231 const Type *Tys[1];
232 Tys[0] = Len->getType();
233 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000234 Value *Align = ConstantInt::get(Type::getInt32Ty(*Context), 1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000235 return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
236}
237
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000238/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
239/// 'floor'). This function is known to take a single of type matching 'Op' and
240/// returns one value with the same type. If 'Op' is a long double, 'l' is
241/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
242Value *LibCallOptimization::EmitUnaryFloatFnCall(Value *Op, const char *Name,
Dan Gohman79cb8402009-09-25 23:10:17 +0000243 IRBuilder<> &B,
244 const AttrListPtr &Attrs) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000245 char NameBuffer[20];
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000246 if (!Op->getType()->isDoubleTy()) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000247 // If we need to add a suffix, copy into NameBuffer.
248 unsigned NameLen = strlen(Name);
249 assert(NameLen < sizeof(NameBuffer)-2);
250 memcpy(NameBuffer, Name, NameLen);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000251 if (Op->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000252 NameBuffer[NameLen] = 'f'; // floorf
253 else
254 NameBuffer[NameLen] = 'l'; // floorl
255 NameBuffer[NameLen+1] = 0;
256 Name = NameBuffer;
257 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000258
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000259 Module *M = Caller->getParent();
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000260 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000261 Op->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000262 CallInst *CI = B.CreateCall(Callee, Op, Name);
Dan Gohman79cb8402009-09-25 23:10:17 +0000263 CI->setAttributes(Attrs);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000264 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
265 CI->setCallingConv(F->getCallingConv());
266
267 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000268}
269
270/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
271/// is an integer.
Chris Lattner74965f22009-11-09 04:57:04 +0000272Value *LibCallOptimization::EmitPutChar(Value *Char, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000273 Module *M = Caller->getParent();
Owen Anderson1d0be152009-08-13 21:58:54 +0000274 Value *PutChar = M->getOrInsertFunction("putchar", Type::getInt32Ty(*Context),
275 Type::getInt32Ty(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000276 CallInst *CI = B.CreateCall(PutChar,
Eric Christopher37c8b862009-10-07 21:14:25 +0000277 B.CreateIntCast(Char,
278 Type::getInt32Ty(*Context),
Duncan Sandsf63c4102009-11-16 12:32:28 +0000279 /*isSigned*/true,
Eric Christopher37c8b862009-10-07 21:14:25 +0000280 "chari"),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000281 "putchar");
282
283 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
284 CI->setCallingConv(F->getCallingConv());
Chris Lattner74965f22009-11-09 04:57:04 +0000285 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000286}
287
288/// EmitPutS - Emit a call to the puts function. This assumes that Str is
289/// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000290void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000291 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000292 AttributeWithIndex AWI[2];
293 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
294 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
295
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000296 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000297 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000298 Type::getInt8PtrTy(*Context),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000299 NULL);
300 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
301 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
302 CI->setCallingConv(F->getCallingConv());
303
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000304}
305
306/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
307/// an integer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000308void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000309 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000310 AttributeWithIndex AWI[2];
311 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
312 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
313 Constant *F;
314 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000315 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
316 Type::getInt32Ty(*Context),
317 Type::getInt32Ty(*Context), File->getType(),
318 NULL);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000319 else
Eric Christopher37c8b862009-10-07 21:14:25 +0000320 F = M->getOrInsertFunction("fputc",
321 Type::getInt32Ty(*Context),
322 Type::getInt32Ty(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000323 File->getType(), NULL);
Duncan Sandsf63c4102009-11-16 12:32:28 +0000324 Char = B.CreateIntCast(Char, Type::getInt32Ty(*Context), /*isSigned*/true,
325 "chari");
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000326 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
327
328 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
329 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000330}
331
332/// EmitFPutS - Emit a call to the puts function. Str is required to be a
333/// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000334void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000335 Module *M = Caller->getParent();
Nick Lewycky225f7472009-02-15 22:47:25 +0000336 AttributeWithIndex AWI[3];
337 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
338 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
339 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000340 Constant *F;
341 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000342 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
343 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000344 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000345 File->getType(), NULL);
346 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000347 F = M->getOrInsertFunction("fputs", Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000348 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000349 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000350 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
351
352 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
353 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000354}
355
356/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
357/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
358void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Eric Christopher7a61d702008-08-08 19:39:37 +0000359 IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000360 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000361 AttributeWithIndex AWI[3];
362 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
363 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
364 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
365 Constant *F;
366 if (isa<PointerType>(File->getType()))
367 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000368 TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000369 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000370 TD->getIntPtrType(*Context),
371 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000372 File->getType(), NULL);
373 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000374 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000375 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000376 TD->getIntPtrType(*Context),
377 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000378 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000379 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Owen Anderson1d0be152009-08-13 21:58:54 +0000380 ConstantInt::get(TD->getIntPtrType(*Context), 1), File);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000381
382 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
383 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000384}
385
386//===----------------------------------------------------------------------===//
387// Helper Functions
388//===----------------------------------------------------------------------===//
389
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000390/// GetStringLengthH - If we can compute the length of the string pointed to by
391/// the specified pointer, return 'len+1'. If we can't, return 0.
392static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
393 // Look through noop bitcast instructions.
394 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
395 return GetStringLengthH(BCI->getOperand(0), PHIs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000396
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000397 // If this is a PHI node, there are two cases: either we have already seen it
398 // or we haven't.
399 if (PHINode *PN = dyn_cast<PHINode>(V)) {
400 if (!PHIs.insert(PN))
401 return ~0ULL; // already in the set.
Eric Christopher37c8b862009-10-07 21:14:25 +0000402
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000403 // If it was new, see if all the input strings are the same length.
404 uint64_t LenSoFar = ~0ULL;
405 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
406 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
407 if (Len == 0) return 0; // Unknown length -> unknown.
Eric Christopher37c8b862009-10-07 21:14:25 +0000408
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000409 if (Len == ~0ULL) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +0000410
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000411 if (Len != LenSoFar && LenSoFar != ~0ULL)
412 return 0; // Disagree -> unknown.
413 LenSoFar = Len;
414 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000415
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000416 // Success, all agree.
417 return LenSoFar;
418 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000419
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000420 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
421 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
422 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
423 if (Len1 == 0) return 0;
424 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
425 if (Len2 == 0) return 0;
426 if (Len1 == ~0ULL) return Len2;
427 if (Len2 == ~0ULL) return Len1;
428 if (Len1 != Len2) return 0;
429 return Len1;
430 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000431
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000432 // If the value is not a GEP instruction nor a constant expression with a
433 // GEP instruction, then return unknown.
434 User *GEP = 0;
435 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
436 GEP = GEPI;
437 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
438 if (CE->getOpcode() != Instruction::GetElementPtr)
439 return 0;
440 GEP = CE;
441 } else {
442 return 0;
443 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000444
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000445 // Make sure the GEP has exactly three arguments.
446 if (GEP->getNumOperands() != 3)
447 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000448
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000449 // Check to make sure that the first operand of the GEP is an integer and
450 // has value 0 so that we are sure we're indexing into the initializer.
451 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
452 if (!Idx->isZero())
453 return 0;
454 } else
455 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000456
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000457 // If the second index isn't a ConstantInt, then this is a variable index
458 // into the array. If this occurs, we can't say anything meaningful about
459 // the string.
460 uint64_t StartIdx = 0;
461 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
462 StartIdx = CI->getZExtValue();
463 else
464 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000465
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000466 // The GEP instruction, constant or instruction, must reference a global
467 // variable that is a constant and is initialized. The referenced constant
468 // initializer is the array that we'll use for optimization.
469 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
Dan Gohman107f41f2009-08-19 00:11:12 +0000470 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
471 GV->mayBeOverridden())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000472 return 0;
473 Constant *GlobalInit = GV->getInitializer();
Eric Christopher37c8b862009-10-07 21:14:25 +0000474
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000475 // Handle the ConstantAggregateZero case, which is a degenerate case. The
476 // initializer is constant zero so the length of the string must be zero.
477 if (isa<ConstantAggregateZero>(GlobalInit))
478 return 1; // Len = 0 offset by 1.
Eric Christopher37c8b862009-10-07 21:14:25 +0000479
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000480 // Must be a Constant Array
481 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
Owen Anderson1d0be152009-08-13 21:58:54 +0000482 if (!Array ||
483 Array->getType()->getElementType() != Type::getInt8Ty(V->getContext()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000484 return false;
Eric Christopher37c8b862009-10-07 21:14:25 +0000485
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000486 // Get the number of elements in the array
487 uint64_t NumElts = Array->getType()->getNumElements();
Eric Christopher37c8b862009-10-07 21:14:25 +0000488
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000489 // Traverse the constant array from StartIdx (derived above) which is
490 // the place the GEP refers to in the array.
491 for (unsigned i = StartIdx; i != NumElts; ++i) {
492 Constant *Elt = Array->getOperand(i);
493 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
494 if (!CI) // This array isn't suitable, non-int initializer.
495 return 0;
496 if (CI->isZero())
497 return i-StartIdx+1; // We found end of string, success!
498 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000499
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000500 return 0; // The array isn't null terminated, conservatively return 'unknown'.
501}
502
503/// GetStringLength - If we can compute the length of the string pointed to by
504/// the specified pointer, return 'len+1'. If we can't, return 0.
505static uint64_t GetStringLength(Value *V) {
506 if (!isa<PointerType>(V->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000507
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000508 SmallPtrSet<PHINode*, 32> PHIs;
509 uint64_t Len = GetStringLengthH(V, PHIs);
510 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
511 // an empty string as a length.
512 return Len == ~0ULL ? 1 : Len;
513}
514
515/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +0000516/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000517static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
518 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
519 UI != E; ++UI) {
520 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
521 if (IC->isEquality())
522 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
523 if (C->isNullValue())
524 continue;
525 // Unknown instruction.
526 return false;
527 }
528 return true;
529}
530
531//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000532// String and Memory LibCall Optimizations
533//===----------------------------------------------------------------------===//
534
535//===---------------------------------------===//
536// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000537namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000538struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000539 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000540 // Verify the "strcat" function prototype.
541 const FunctionType *FT = Callee->getFunctionType();
542 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000543 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000544 FT->getParamType(0) != FT->getReturnType() ||
545 FT->getParamType(1) != FT->getReturnType())
546 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000547
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000548 // Extract some information from the instruction
549 Value *Dst = CI->getOperand(1);
550 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000551
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000552 // See if we can get the length of the input string.
553 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000554 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000555 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000556
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000557 // Handle the simple, do-nothing case: strcat(x, "") -> x
558 if (Len == 0)
559 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000560
561 // These optimizations require TargetData.
562 if (!TD) return 0;
563
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000564 EmitStrLenMemCpy(Src, Dst, Len, B);
565 return Dst;
566 }
567
568 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000569 // We need to find the end of the destination string. That's where the
570 // memory is to be moved to. We just generate a call to strlen.
571 Value *DstLen = EmitStrLen(Dst, B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000572
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000573 // Now that we have the destination's length, we must index into the
574 // destination's pointer to get the actual memcpy destination (end of
575 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000576 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000577
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000578 // We have enough information to now generate the memcpy call to do the
579 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000580 EmitMemCpy(CpyDst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000581 ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000582 }
583};
584
585//===---------------------------------------===//
586// 'strncat' Optimizations
587
Chris Lattner3e8b6632009-09-02 06:11:42 +0000588struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000589 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
590 // Verify the "strncat" function prototype.
591 const FunctionType *FT = Callee->getFunctionType();
592 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000593 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000594 FT->getParamType(0) != FT->getReturnType() ||
595 FT->getParamType(1) != FT->getReturnType() ||
596 !isa<IntegerType>(FT->getParamType(2)))
597 return 0;
598
599 // Extract some information from the instruction
600 Value *Dst = CI->getOperand(1);
601 Value *Src = CI->getOperand(2);
602 uint64_t Len;
603
604 // We don't do anything if length is not constant
605 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
606 Len = LengthArg->getZExtValue();
607 else
608 return 0;
609
610 // See if we can get the length of the input string.
611 uint64_t SrcLen = GetStringLength(Src);
612 if (SrcLen == 0) return 0;
613 --SrcLen; // Unbias length.
614
615 // Handle the simple, do-nothing cases:
616 // strncat(x, "", c) -> x
617 // strncat(x, c, 0) -> x
618 if (SrcLen == 0 || Len == 0) return Dst;
619
Dan Gohmanf14d9192009-08-18 00:48:13 +0000620 // These optimizations require TargetData.
621 if (!TD) return 0;
622
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000623 // We don't optimize this case
624 if (Len < SrcLen) return 0;
625
626 // strncat(x, s, c) -> strcat(x, s)
627 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000628 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000629 return Dst;
630 }
631};
632
633//===---------------------------------------===//
634// 'strchr' Optimizations
635
Chris Lattner3e8b6632009-09-02 06:11:42 +0000636struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000637 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000638 // Verify the "strchr" function prototype.
639 const FunctionType *FT = Callee->getFunctionType();
640 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000641 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000642 FT->getParamType(0) != FT->getReturnType())
643 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000644
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000645 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000646
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000647 // If the second operand is non-constant, see if we can compute the length
648 // of the input string and turn this into memchr.
649 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
650 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000651 // These optimizations require TargetData.
652 if (!TD) return 0;
653
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000654 uint64_t Len = GetStringLength(SrcStr);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000655 if (Len == 0 ||
656 FT->getParamType(1) != Type::getInt32Ty(*Context)) // memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000657 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000658
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000659 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Owen Anderson1d0be152009-08-13 21:58:54 +0000660 ConstantInt::get(TD->getIntPtrType(*Context), Len), B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000661 }
662
663 // Otherwise, the character is a constant, see if the first argument is
664 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000665 std::string Str;
666 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000667 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000668
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000669 // strchr can find the nul character.
670 Str += '\0';
671 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000672
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000673 // Compute the offset.
674 uint64_t i = 0;
675 while (1) {
676 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000677 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000678 // Did we find our match?
679 if (Str[i] == CharValue)
680 break;
681 ++i;
682 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000683
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000684 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000685 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000686 return B.CreateGEP(SrcStr, Idx, "strchr");
687 }
688};
689
690//===---------------------------------------===//
691// 'strcmp' Optimizations
692
Chris Lattner3e8b6632009-09-02 06:11:42 +0000693struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000694 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000695 // Verify the "strcmp" function prototype.
696 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000697 if (FT->getNumParams() != 2 ||
698 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000699 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000700 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000701 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000702
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000703 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
704 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000705 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000706
Bill Wendling0582ae92009-03-13 04:39:26 +0000707 std::string Str1, Str2;
708 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
709 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000710
Bill Wendling0582ae92009-03-13 04:39:26 +0000711 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000712 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000713
Bill Wendling0582ae92009-03-13 04:39:26 +0000714 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000715 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000716
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000717 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000718 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000719 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000720 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000721
722 // strcmp(P, "x") -> memcmp(P, "x", 2)
723 uint64_t Len1 = GetStringLength(Str1P);
724 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000725 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000726 // These optimizations require TargetData.
727 if (!TD) return 0;
728
Nick Lewycky13a09e22008-12-21 00:19:21 +0000729 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000730 ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattner849832c2009-06-19 04:17:36 +0000731 std::min(Len1, Len2)), B);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000732 }
733
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000734 return 0;
735 }
736};
737
738//===---------------------------------------===//
739// 'strncmp' Optimizations
740
Chris Lattner3e8b6632009-09-02 06:11:42 +0000741struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000742 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000743 // Verify the "strncmp" function prototype.
744 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000745 if (FT->getNumParams() != 3 ||
746 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000747 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000748 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000749 !isa<IntegerType>(FT->getParamType(2)))
750 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000751
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000752 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
753 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000754 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000755
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000756 // Get the length argument if it is constant.
757 uint64_t Length;
758 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
759 Length = LengthArg->getZExtValue();
760 else
761 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000762
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000763 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000764 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000765
Bill Wendling0582ae92009-03-13 04:39:26 +0000766 std::string Str1, Str2;
767 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
768 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000769
Bill Wendling0582ae92009-03-13 04:39:26 +0000770 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000771 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000772
Bill Wendling0582ae92009-03-13 04:39:26 +0000773 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000774 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000775
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000776 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000777 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000778 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000779 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000780 return 0;
781 }
782};
783
784
785//===---------------------------------------===//
786// 'strcpy' Optimizations
787
Chris Lattner3e8b6632009-09-02 06:11:42 +0000788struct StrCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000789 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000790 // Verify the "strcpy" function prototype.
791 const FunctionType *FT = Callee->getFunctionType();
792 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
793 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000794 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000795 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000796
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000797 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
798 if (Dst == Src) // strcpy(x,x) -> x
799 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000800
Dan Gohmanf14d9192009-08-18 00:48:13 +0000801 // These optimizations require TargetData.
802 if (!TD) return 0;
803
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000804 // See if we can get the length of the input string.
805 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000806 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000807
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000808 // We have enough information to now generate the memcpy call to do the
809 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000810 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000811 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000812 return Dst;
813 }
814};
815
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000816//===---------------------------------------===//
817// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000818
Chris Lattner3e8b6632009-09-02 06:11:42 +0000819struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000820 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
821 const FunctionType *FT = Callee->getFunctionType();
822 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
823 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000824 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000825 !isa<IntegerType>(FT->getParamType(2)))
826 return 0;
827
828 Value *Dst = CI->getOperand(1);
829 Value *Src = CI->getOperand(2);
830 Value *LenOp = CI->getOperand(3);
831
832 // See if we can get the length of the input string.
833 uint64_t SrcLen = GetStringLength(Src);
834 if (SrcLen == 0) return 0;
835 --SrcLen;
836
837 if (SrcLen == 0) {
838 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000839 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
840 B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000841 return Dst;
842 }
843
844 uint64_t Len;
845 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
846 Len = LengthArg->getZExtValue();
847 else
848 return 0;
849
850 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
851
Dan Gohmanf14d9192009-08-18 00:48:13 +0000852 // These optimizations require TargetData.
853 if (!TD) return 0;
854
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000855 // Let strncpy handle the zero padding
856 if (Len > SrcLen+1) return 0;
857
858 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000859 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000860 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000861
862 return Dst;
863 }
864};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000865
866//===---------------------------------------===//
867// 'strlen' Optimizations
868
Chris Lattner3e8b6632009-09-02 06:11:42 +0000869struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000870 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000871 const FunctionType *FT = Callee->getFunctionType();
872 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000873 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000874 !isa<IntegerType>(FT->getReturnType()))
875 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000876
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000877 Value *Src = CI->getOperand(1);
878
879 // Constant folding: strlen("xyz") -> 3
880 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000881 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000882
883 // Handle strlen(p) != 0.
884 if (!IsOnlyUsedInZeroEqualityComparison(CI)) return 0;
885
886 // strlen(x) != 0 --> *x != 0
887 // strlen(x) == 0 --> *x == 0
888 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
889 }
890};
891
892//===---------------------------------------===//
Nick Lewycky4c498412009-02-13 15:31:46 +0000893// 'strto*' Optimizations
894
Chris Lattner3e8b6632009-09-02 06:11:42 +0000895struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000896 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
897 const FunctionType *FT = Callee->getFunctionType();
898 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
899 !isa<PointerType>(FT->getParamType(0)) ||
900 !isa<PointerType>(FT->getParamType(1)))
901 return 0;
902
903 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000904 if (isa<ConstantPointerNull>(EndPtr)) {
905 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000906 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000907 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000908
909 return 0;
910 }
911};
912
913
914//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000915// 'memcmp' Optimizations
916
Chris Lattner3e8b6632009-09-02 06:11:42 +0000917struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000918 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000919 const FunctionType *FT = Callee->getFunctionType();
920 if (FT->getNumParams() != 3 || !isa<PointerType>(FT->getParamType(0)) ||
921 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000922 FT->getReturnType() != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000923 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000924
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000925 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000926
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000927 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000928 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000929
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000930 // Make sure we have a constant length.
931 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000932 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000933 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000934
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000935 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000936 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000937
938 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
939 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
940 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +0000941 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000942 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000943
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000944 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS ^ *(short*)RHS) != 0
945 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS ^ *(int*)RHS) != 0
946 if ((Len == 2 || Len == 4) && IsOnlyUsedInZeroEqualityComparison(CI)) {
Owen Andersondebcb012009-07-29 22:17:13 +0000947 const Type *PTy = PointerType::getUnqual(Len == 2 ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000948 Type::getInt16Ty(*Context) : Type::getInt32Ty(*Context));
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000949 LHS = B.CreateBitCast(LHS, PTy, "tmp");
950 RHS = B.CreateBitCast(RHS, PTy, "tmp");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000951 LoadInst *LHSV = B.CreateLoad(LHS, "lhsv");
952 LoadInst *RHSV = B.CreateLoad(RHS, "rhsv");
953 LHSV->setAlignment(1); RHSV->setAlignment(1); // Unaligned loads.
954 return B.CreateZExt(B.CreateXor(LHSV, RHSV, "shortdiff"), CI->getType());
955 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000956
Benjamin Kramer992a6372009-11-05 17:44:22 +0000957 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
958 std::string LHSStr, RHSStr;
959 if (GetConstantStringInfo(LHS, LHSStr) &&
960 GetConstantStringInfo(RHS, RHSStr)) {
961 // Make sure we're not reading out-of-bounds memory.
962 if (Len > LHSStr.length() || Len > RHSStr.length())
963 return 0;
964 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
965 return ConstantInt::get(CI->getType(), Ret);
966 }
967
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000968 return 0;
969 }
970};
971
972//===---------------------------------------===//
973// 'memcpy' Optimizations
974
Chris Lattner3e8b6632009-09-02 06:11:42 +0000975struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000976 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000977 // These optimizations require TargetData.
978 if (!TD) return 0;
979
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000980 const FunctionType *FT = Callee->getFunctionType();
981 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
982 !isa<PointerType>(FT->getParamType(0)) ||
983 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000984 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000985 return 0;
986
987 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
988 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
989 return CI->getOperand(1);
990 }
991};
992
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000993//===---------------------------------------===//
994// 'memmove' Optimizations
995
Chris Lattner3e8b6632009-09-02 06:11:42 +0000996struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000997 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000998 // These optimizations require TargetData.
999 if (!TD) return 0;
1000
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001001 const FunctionType *FT = Callee->getFunctionType();
1002 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1003 !isa<PointerType>(FT->getParamType(0)) ||
1004 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001005 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001006 return 0;
1007
1008 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopher80bf1d52009-11-21 01:01:30 +00001009 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001010 return CI->getOperand(1);
1011 }
1012};
1013
1014//===---------------------------------------===//
1015// 'memset' Optimizations
1016
Chris Lattner3e8b6632009-09-02 06:11:42 +00001017struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001018 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001019 // These optimizations require TargetData.
1020 if (!TD) return 0;
1021
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001022 const FunctionType *FT = Callee->getFunctionType();
1023 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1024 !isa<PointerType>(FT->getParamType(0)) ||
Eli Friedman62bb4132009-07-18 08:34:51 +00001025 !isa<IntegerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001026 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001027 return 0;
1028
1029 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +00001030 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1031 false);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001032 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001033 return CI->getOperand(1);
1034 }
1035};
1036
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001037//===----------------------------------------------------------------------===//
Eric Christopher80bf1d52009-11-21 01:01:30 +00001038// Object Size Checking Optimizations
1039//===----------------------------------------------------------------------===//
1040
1041//===---------------------------------------===//
1042// 'object size'
1043namespace {
1044struct SizeOpt : public LibCallOptimization {
1045 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1046 // TODO: We can do more with this, but delaying to here should be no change
1047 // in behavior.
1048 ConstantInt *Const = dyn_cast<ConstantInt>(CI->getOperand(2));
1049
1050 if (!Const) return 0;
1051
1052 const Type *Ty = Callee->getFunctionType()->getReturnType();
1053
1054 if (Const->getZExtValue() < 2)
1055 return Constant::getAllOnesValue(Ty);
1056 else
1057 return ConstantInt::get(Ty, 0);
1058 }
1059};
1060}
1061
1062//===---------------------------------------===//
1063// 'memcpy_chk' Optimizations
1064
1065struct MemCpyChkOpt : public LibCallOptimization {
1066 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1067 // These optimizations require TargetData.
1068 if (!TD) return 0;
1069
1070 const FunctionType *FT = Callee->getFunctionType();
1071 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1072 !isa<PointerType>(FT->getParamType(0)) ||
1073 !isa<PointerType>(FT->getParamType(1)) ||
1074 !isa<IntegerType>(FT->getParamType(3)) ||
1075 FT->getParamType(2) != TD->getIntPtrType(*Context))
1076 return 0;
1077
1078 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1079 if (!SizeCI)
1080 return 0;
1081 if (SizeCI->isAllOnesValue()) {
1082 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
1083 return CI->getOperand(1);
1084 }
1085
1086 return 0;
1087 }
1088};
1089
1090//===---------------------------------------===//
1091// 'memset_chk' Optimizations
1092
1093struct MemSetChkOpt : public LibCallOptimization {
1094 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1095 // These optimizations require TargetData.
1096 if (!TD) return 0;
1097
1098 const FunctionType *FT = Callee->getFunctionType();
1099 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1100 !isa<PointerType>(FT->getParamType(0)) ||
1101 !isa<IntegerType>(FT->getParamType(1)) ||
1102 !isa<IntegerType>(FT->getParamType(3)) ||
1103 FT->getParamType(2) != TD->getIntPtrType(*Context))
1104 return 0;
1105
1106 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1107 if (!SizeCI)
1108 return 0;
1109 if (SizeCI->isAllOnesValue()) {
1110 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1111 false);
1112 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
1113 return CI->getOperand(1);
1114 }
1115
1116 return 0;
1117 }
1118};
1119
1120//===---------------------------------------===//
1121// 'memmove_chk' Optimizations
1122
1123struct MemMoveChkOpt : public LibCallOptimization {
1124 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1125 // These optimizations require TargetData.
1126 if (!TD) return 0;
1127
1128 const FunctionType *FT = Callee->getFunctionType();
1129 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1130 !isa<PointerType>(FT->getParamType(0)) ||
1131 !isa<PointerType>(FT->getParamType(1)) ||
1132 !isa<IntegerType>(FT->getParamType(3)) ||
1133 FT->getParamType(2) != TD->getIntPtrType(*Context))
1134 return 0;
1135
1136 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1137 if (!SizeCI)
1138 return 0;
1139 if (SizeCI->isAllOnesValue()) {
1140 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
1141 1, B);
1142 return CI->getOperand(1);
1143 }
1144
1145 return 0;
1146 }
1147};
1148
1149//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001150// Math Library Optimizations
1151//===----------------------------------------------------------------------===//
1152
1153//===---------------------------------------===//
1154// 'pow*' Optimizations
1155
Chris Lattner3e8b6632009-09-02 06:11:42 +00001156struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001157 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001158 const FunctionType *FT = Callee->getFunctionType();
1159 // Just make sure this has 2 arguments of the same FP type, which match the
1160 // result type.
1161 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1162 FT->getParamType(0) != FT->getParamType(1) ||
1163 !FT->getParamType(0)->isFloatingPoint())
1164 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001165
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001166 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
1167 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1168 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
1169 return Op1C;
1170 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +00001171 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001172 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001173
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001174 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1175 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001176
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001177 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001178 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001179
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001180 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +00001181 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1182 // This is faster than calling pow, and still handles negative zero
1183 // and negative infinite correctly.
1184 // TODO: In fast-math mode, this could be just sqrt(x).
1185 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +00001186 Value *Inf = ConstantFP::getInfinity(CI->getType());
1187 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +00001188 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1189 Callee->getAttributes());
1190 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1191 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +00001192 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
1193 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
1194 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001195 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001196
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001197 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1198 return Op1;
1199 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001200 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001201 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001202 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001203 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001204 return 0;
1205 }
1206};
1207
1208//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +00001209// 'exp2' Optimizations
1210
Chris Lattner3e8b6632009-09-02 06:11:42 +00001211struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001212 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +00001213 const FunctionType *FT = Callee->getFunctionType();
1214 // Just make sure this has 1 argument of FP type, which matches the
1215 // result type.
1216 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1217 !FT->getParamType(0)->isFloatingPoint())
1218 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001219
Chris Lattnere818f772008-05-02 18:43:35 +00001220 Value *Op = CI->getOperand(1);
1221 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1222 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1223 Value *LdExpArg = 0;
1224 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1225 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001226 LdExpArg = B.CreateSExt(OpC->getOperand(0),
1227 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001228 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1229 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001230 LdExpArg = B.CreateZExt(OpC->getOperand(0),
1231 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001232 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001233
Chris Lattnere818f772008-05-02 18:43:35 +00001234 if (LdExpArg) {
1235 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001236 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001237 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001238 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001239 Name = "ldexp";
1240 else
1241 Name = "ldexpl";
1242
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001243 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001244 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001245 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +00001246
1247 Module *M = Caller->getParent();
1248 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +00001249 Op->getType(),
1250 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001251 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1252 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1253 CI->setCallingConv(F->getCallingConv());
1254
1255 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +00001256 }
1257 return 0;
1258 }
1259};
Chris Lattnere818f772008-05-02 18:43:35 +00001260
1261//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001262// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1263
Chris Lattner3e8b6632009-09-02 06:11:42 +00001264struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001265 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001266 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001267 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1268 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001269 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001270
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001271 // If this is something like 'floor((double)floatval)', convert to floorf.
1272 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001273 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001274 return 0;
1275
1276 // floor((double)floatval) -> (double)floorf(floatval)
1277 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +00001278 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
1279 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +00001280 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001281 }
1282};
1283
1284//===----------------------------------------------------------------------===//
1285// Integer Optimizations
1286//===----------------------------------------------------------------------===//
1287
1288//===---------------------------------------===//
1289// 'ffs*' Optimizations
1290
Chris Lattner3e8b6632009-09-02 06:11:42 +00001291struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001292 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001293 const FunctionType *FT = Callee->getFunctionType();
1294 // Just make sure this has 2 arguments of the same FP type, which match the
1295 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +00001296 if (FT->getNumParams() != 1 ||
1297 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001298 !isa<IntegerType>(FT->getParamType(0)))
1299 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001300
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001301 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001302
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001303 // Constant fold.
1304 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1305 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +00001306 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +00001307 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001308 CI->getValue().countTrailingZeros()+1);
1309 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001310
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001311 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1312 const Type *ArgType = Op->getType();
1313 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1314 Intrinsic::cttz, &ArgType, 1);
1315 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +00001316 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +00001317 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001318
Owen Andersona7235ea2009-07-31 20:28:14 +00001319 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001320 return B.CreateSelect(Cond, V,
1321 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001322 }
1323};
1324
1325//===---------------------------------------===//
1326// 'isdigit' Optimizations
1327
Chris Lattner3e8b6632009-09-02 06:11:42 +00001328struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001329 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001330 const FunctionType *FT = Callee->getFunctionType();
1331 // We require integer(i32)
1332 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001333 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001334 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001335
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001336 // isdigit(c) -> (c-'0') <u 10
1337 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001338 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001339 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001340 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001341 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001342 return B.CreateZExt(Op, CI->getType());
1343 }
1344};
1345
1346//===---------------------------------------===//
1347// 'isascii' Optimizations
1348
Chris Lattner3e8b6632009-09-02 06:11:42 +00001349struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001350 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001351 const FunctionType *FT = Callee->getFunctionType();
1352 // We require integer(i32)
1353 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001354 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001355 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001356
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001357 // isascii(c) -> c <u 128
1358 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001359 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001360 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001361 return B.CreateZExt(Op, CI->getType());
1362 }
1363};
Eric Christopher37c8b862009-10-07 21:14:25 +00001364
Chris Lattner313f0e62008-06-09 08:26:51 +00001365//===---------------------------------------===//
1366// 'abs', 'labs', 'llabs' Optimizations
1367
Chris Lattner3e8b6632009-09-02 06:11:42 +00001368struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001369 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001370 const FunctionType *FT = Callee->getFunctionType();
1371 // We require integer(integer) where the types agree.
1372 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
1373 FT->getParamType(0) != FT->getReturnType())
1374 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001375
Chris Lattner313f0e62008-06-09 08:26:51 +00001376 // abs(x) -> x >s -1 ? x : -x
1377 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001378 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001379 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001380 "ispos");
1381 Value *Neg = B.CreateNeg(Op, "neg");
1382 return B.CreateSelect(Pos, Op, Neg);
1383 }
1384};
Eric Christopher37c8b862009-10-07 21:14:25 +00001385
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001386
1387//===---------------------------------------===//
1388// 'toascii' Optimizations
1389
Chris Lattner3e8b6632009-09-02 06:11:42 +00001390struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001391 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001392 const FunctionType *FT = Callee->getFunctionType();
1393 // We require i32(i32)
1394 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001395 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001396 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001397
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001398 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001399 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +00001400 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001401 }
1402};
1403
1404//===----------------------------------------------------------------------===//
1405// Formatting and IO Optimizations
1406//===----------------------------------------------------------------------===//
1407
1408//===---------------------------------------===//
1409// 'printf' Optimizations
1410
Chris Lattner3e8b6632009-09-02 06:11:42 +00001411struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001412 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001413 // Require one fixed pointer argument and an integer/void result.
1414 const FunctionType *FT = Callee->getFunctionType();
1415 if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
1416 !(isa<IntegerType>(FT->getReturnType()) ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001417 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001418 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001419
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001420 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001421 std::string FormatStr;
1422 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1423 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001424
1425 // Empty format string -> noop.
1426 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001427 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001428 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001429
Chris Lattner74965f22009-11-09 04:57:04 +00001430 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1431 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001432 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001433 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
1434 FormatStr[0]), B);
1435 if (CI->use_empty()) return CI;
1436 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001437 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001438
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001439 // printf("foo\n") --> puts("foo")
1440 if (FormatStr[FormatStr.size()-1] == '\n' &&
1441 FormatStr.find('%') == std::string::npos) { // no format characters.
1442 // Create a string literal with no \n on it. We expect the constant merge
1443 // pass to be run after this pass, to merge duplicate strings.
1444 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001445 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001446 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1447 GlobalVariable::InternalLinkage, C, "str");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001448 EmitPutS(C, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001449 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001450 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001451 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001452
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001453 // Optimize specific format strings.
1454 // printf("%c", chr) --> putchar(*(i8*)dst)
1455 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
1456 isa<IntegerType>(CI->getOperand(2)->getType())) {
Chris Lattner74965f22009-11-09 04:57:04 +00001457 Value *Res = EmitPutChar(CI->getOperand(2), B);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001458
Chris Lattner74965f22009-11-09 04:57:04 +00001459 if (CI->use_empty()) return CI;
1460 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001461 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001462
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001463 // printf("%s\n", str) --> puts(str)
1464 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
1465 isa<PointerType>(CI->getOperand(2)->getType()) &&
1466 CI->use_empty()) {
1467 EmitPutS(CI->getOperand(2), B);
1468 return CI;
1469 }
1470 return 0;
1471 }
1472};
1473
1474//===---------------------------------------===//
1475// 'sprintf' Optimizations
1476
Chris Lattner3e8b6632009-09-02 06:11:42 +00001477struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001478 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001479 // Require two fixed pointer arguments and an integer result.
1480 const FunctionType *FT = Callee->getFunctionType();
1481 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1482 !isa<PointerType>(FT->getParamType(1)) ||
1483 !isa<IntegerType>(FT->getReturnType()))
1484 return 0;
1485
1486 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001487 std::string FormatStr;
1488 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1489 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001490
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001491 // If we just have a format string (nothing else crazy) transform it.
1492 if (CI->getNumOperands() == 3) {
1493 // Make sure there's no % in the constant array. We could try to handle
1494 // %% -> % in the future if we cared.
1495 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1496 if (FormatStr[i] == '%')
1497 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001498
1499 // These optimizations require TargetData.
1500 if (!TD) return 0;
1501
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001502 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1503 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Owen Anderson1d0be152009-08-13 21:58:54 +00001504 ConstantInt::get(TD->getIntPtrType(*Context), FormatStr.size()+1),1,B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001505 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001506 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001507
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001508 // The remaining optimizations require the format string to be "%s" or "%c"
1509 // and have an extra operand.
1510 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1511 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001512
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001513 // Decode the second character of the format string.
1514 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001515 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001516 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001517 Value *V = B.CreateTrunc(CI->getOperand(3),
1518 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001519 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1520 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001521 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1522 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001523 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001524
Owen Andersoneed707b2009-07-24 23:12:02 +00001525 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001526 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001527
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001528 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001529 // These optimizations require TargetData.
1530 if (!TD) return 0;
1531
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001532 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1533 if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
1534
1535 Value *Len = EmitStrLen(CI->getOperand(3), B);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001536 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001537 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001538 "leninc");
1539 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001540
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001541 // The sprintf result is the unincremented number of bytes in the string.
1542 return B.CreateIntCast(Len, CI->getType(), false);
1543 }
1544 return 0;
1545 }
1546};
1547
1548//===---------------------------------------===//
1549// 'fwrite' Optimizations
1550
Chris Lattner3e8b6632009-09-02 06:11:42 +00001551struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001552 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001553 // Require a pointer, an integer, an integer, a pointer, returning integer.
1554 const FunctionType *FT = Callee->getFunctionType();
1555 if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
1556 !isa<IntegerType>(FT->getParamType(1)) ||
1557 !isa<IntegerType>(FT->getParamType(2)) ||
1558 !isa<PointerType>(FT->getParamType(3)) ||
1559 !isa<IntegerType>(FT->getReturnType()))
1560 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001561
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001562 // Get the element size and count.
1563 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1564 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1565 if (!SizeC || !CountC) return 0;
1566 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001567
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001568 // If this is writing zero records, remove the call (it's a noop).
1569 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001570 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001571
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001572 // If this is writing one byte, turn it into fputc.
1573 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1574 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1575 EmitFPutC(Char, CI->getOperand(4), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001576 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001577 }
1578
1579 return 0;
1580 }
1581};
1582
1583//===---------------------------------------===//
1584// 'fputs' Optimizations
1585
Chris Lattner3e8b6632009-09-02 06:11:42 +00001586struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001587 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001588 // These optimizations require TargetData.
1589 if (!TD) return 0;
1590
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001591 // Require two pointers. Also, we can't optimize if return value is used.
1592 const FunctionType *FT = Callee->getFunctionType();
1593 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1594 !isa<PointerType>(FT->getParamType(1)) ||
1595 !CI->use_empty())
1596 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001597
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001598 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1599 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001600 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001601 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001602 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001603 CI->getOperand(2), B);
1604 return CI; // Known to have no uses (see above).
1605 }
1606};
1607
1608//===---------------------------------------===//
1609// 'fprintf' Optimizations
1610
Chris Lattner3e8b6632009-09-02 06:11:42 +00001611struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001612 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001613 // Require two fixed paramters as pointers and integer result.
1614 const FunctionType *FT = Callee->getFunctionType();
1615 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1616 !isa<PointerType>(FT->getParamType(1)) ||
1617 !isa<IntegerType>(FT->getReturnType()))
1618 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001619
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001620 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001621 std::string FormatStr;
1622 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1623 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001624
1625 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1626 if (CI->getNumOperands() == 3) {
1627 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1628 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001629 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001630
1631 // These optimizations require TargetData.
1632 if (!TD) return 0;
1633
Owen Anderson1d0be152009-08-13 21:58:54 +00001634 EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001635 FormatStr.size()),
1636 CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001637 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001638 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001639
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001640 // The remaining optimizations require the format string to be "%s" or "%c"
1641 // and have an extra operand.
1642 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1643 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001644
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001645 // Decode the second character of the format string.
1646 if (FormatStr[1] == 'c') {
1647 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1648 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1649 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001650 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001651 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001652
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001653 if (FormatStr[1] == 's') {
1654 // fprintf(F, "%s", str) -> fputs(str, F)
1655 if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
1656 return 0;
1657 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1658 return CI;
1659 }
1660 return 0;
1661 }
1662};
1663
Bill Wendlingac178222008-05-05 21:37:59 +00001664} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001665
1666//===----------------------------------------------------------------------===//
1667// SimplifyLibCalls Pass Implementation
1668//===----------------------------------------------------------------------===//
1669
1670namespace {
1671 /// This pass optimizes well known library functions from libc and libm.
1672 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001673 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001674 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001675 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001676 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1677 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
1678 StrToOpt StrTo; MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove;
1679 MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001680 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001681 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001682 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001683 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1684 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001685 // Formatting and IO Optimizations
1686 SPrintFOpt SPrintF; PrintFOpt PrintF;
1687 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001688
1689 // Object Size Checking
Eric Christopher7b5e6172009-10-27 00:52:25 +00001690 SizeOpt ObjectSize;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001691 MemCpyChkOpt MemCpyChk; MemSetChkOpt MemSetChk; MemMoveChkOpt MemMoveChk;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001692
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001693 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001694 public:
1695 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001696 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001697
1698 void InitOptimizations();
1699 bool runOnFunction(Function &F);
1700
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001701 void setDoesNotAccessMemory(Function &F);
1702 void setOnlyReadsMemory(Function &F);
1703 void setDoesNotThrow(Function &F);
1704 void setDoesNotCapture(Function &F, unsigned n);
1705 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001706 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001707
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001708 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001709 }
1710 };
1711 char SimplifyLibCalls::ID = 0;
1712} // end anonymous namespace.
1713
1714static RegisterPass<SimplifyLibCalls>
1715X("simplify-libcalls", "Simplify well-known library calls");
1716
1717// Public interface to the Simplify LibCalls pass.
1718FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001719 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001720}
1721
1722/// Optimizations - Populate the Optimizations map with all the optimizations
1723/// we know.
1724void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001725 // String and Memory LibCall Optimizations
1726 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001727 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001728 Optimizations["strchr"] = &StrChr;
1729 Optimizations["strcmp"] = &StrCmp;
1730 Optimizations["strncmp"] = &StrNCmp;
1731 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001732 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001733 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001734 Optimizations["strtol"] = &StrTo;
1735 Optimizations["strtod"] = &StrTo;
1736 Optimizations["strtof"] = &StrTo;
1737 Optimizations["strtoul"] = &StrTo;
1738 Optimizations["strtoll"] = &StrTo;
1739 Optimizations["strtold"] = &StrTo;
1740 Optimizations["strtoull"] = &StrTo;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001741 Optimizations["memcmp"] = &MemCmp;
1742 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001743 Optimizations["memmove"] = &MemMove;
1744 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001745
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001746 // Math Library Optimizations
1747 Optimizations["powf"] = &Pow;
1748 Optimizations["pow"] = &Pow;
1749 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001750 Optimizations["llvm.pow.f32"] = &Pow;
1751 Optimizations["llvm.pow.f64"] = &Pow;
1752 Optimizations["llvm.pow.f80"] = &Pow;
1753 Optimizations["llvm.pow.f128"] = &Pow;
1754 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001755 Optimizations["exp2l"] = &Exp2;
1756 Optimizations["exp2"] = &Exp2;
1757 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001758 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1759 Optimizations["llvm.exp2.f128"] = &Exp2;
1760 Optimizations["llvm.exp2.f80"] = &Exp2;
1761 Optimizations["llvm.exp2.f64"] = &Exp2;
1762 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001763
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001764#ifdef HAVE_FLOORF
1765 Optimizations["floor"] = &UnaryDoubleFP;
1766#endif
1767#ifdef HAVE_CEILF
1768 Optimizations["ceil"] = &UnaryDoubleFP;
1769#endif
1770#ifdef HAVE_ROUNDF
1771 Optimizations["round"] = &UnaryDoubleFP;
1772#endif
1773#ifdef HAVE_RINTF
1774 Optimizations["rint"] = &UnaryDoubleFP;
1775#endif
1776#ifdef HAVE_NEARBYINTF
1777 Optimizations["nearbyint"] = &UnaryDoubleFP;
1778#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001779
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001780 // Integer Optimizations
1781 Optimizations["ffs"] = &FFS;
1782 Optimizations["ffsl"] = &FFS;
1783 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001784 Optimizations["abs"] = &Abs;
1785 Optimizations["labs"] = &Abs;
1786 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001787 Optimizations["isdigit"] = &IsDigit;
1788 Optimizations["isascii"] = &IsAscii;
1789 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001790
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001791 // Formatting and IO Optimizations
1792 Optimizations["sprintf"] = &SPrintF;
1793 Optimizations["printf"] = &PrintF;
1794 Optimizations["fwrite"] = &FWrite;
1795 Optimizations["fputs"] = &FPuts;
1796 Optimizations["fprintf"] = &FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001797
1798 // Object Size Checking
1799 Optimizations["llvm.objectsize.i32"] = &ObjectSize;
1800 Optimizations["llvm.objectsize.i64"] = &ObjectSize;
1801 Optimizations["__memcpy_chk"] = &MemCpyChk;
1802 Optimizations["__memset_chk"] = &MemSetChk;
1803 Optimizations["__memmove_chk"] = &MemMoveChk;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001804}
1805
1806
1807/// runOnFunction - Top level algorithm.
1808///
1809bool SimplifyLibCalls::runOnFunction(Function &F) {
1810 if (Optimizations.empty())
1811 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001812
Dan Gohmanf14d9192009-08-18 00:48:13 +00001813 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001814
Owen Andersone922c022009-07-22 00:24:57 +00001815 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001816
1817 bool Changed = false;
1818 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1819 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1820 // Ignore non-calls.
1821 CallInst *CI = dyn_cast<CallInst>(I++);
1822 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001823
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001824 // Ignore indirect calls and calls to non-external functions.
1825 Function *Callee = CI->getCalledFunction();
1826 if (Callee == 0 || !Callee->isDeclaration() ||
1827 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1828 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001829
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001830 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001831 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1832 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001833
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001834 // Set the builder to the instruction after the call.
1835 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001836
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001837 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001838 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001839 if (Result == 0) continue;
1840
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001841 DEBUG(errs() << "SimplifyLibCalls simplified: " << *CI;
1842 errs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001843
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001844 // Something changed!
1845 Changed = true;
1846 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001847
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001848 // Inspect the instruction after the call (which was potentially just
1849 // added) next.
1850 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001851
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001852 if (CI != Result && !CI->use_empty()) {
1853 CI->replaceAllUsesWith(Result);
1854 if (!Result->hasName())
1855 Result->takeName(CI);
1856 }
1857 CI->eraseFromParent();
1858 }
1859 }
1860 return Changed;
1861}
1862
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001863// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001864
1865void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1866 if (!F.doesNotAccessMemory()) {
1867 F.setDoesNotAccessMemory();
1868 ++NumAnnotated;
1869 Modified = true;
1870 }
1871}
1872void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1873 if (!F.onlyReadsMemory()) {
1874 F.setOnlyReadsMemory();
1875 ++NumAnnotated;
1876 Modified = true;
1877 }
1878}
1879void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1880 if (!F.doesNotThrow()) {
1881 F.setDoesNotThrow();
1882 ++NumAnnotated;
1883 Modified = true;
1884 }
1885}
1886void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1887 if (!F.doesNotCapture(n)) {
1888 F.setDoesNotCapture(n);
1889 ++NumAnnotated;
1890 Modified = true;
1891 }
1892}
1893void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1894 if (!F.doesNotAlias(n)) {
1895 F.setDoesNotAlias(n);
1896 ++NumAnnotated;
1897 Modified = true;
1898 }
1899}
1900
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001901/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001902///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001903bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001904 Modified = false;
1905 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1906 Function &F = *I;
1907 if (!F.isDeclaration())
1908 continue;
1909
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001910 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001911 continue;
1912
1913 const FunctionType *FTy = F.getFunctionType();
1914
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001915 StringRef Name = F.getName();
1916 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001917 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001918 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001919 if (FTy->getNumParams() != 1 ||
1920 !isa<PointerType>(FTy->getParamType(0)))
1921 continue;
1922 setOnlyReadsMemory(F);
1923 setDoesNotThrow(F);
1924 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001925 } else if (Name == "strcpy" ||
1926 Name == "stpcpy" ||
1927 Name == "strcat" ||
1928 Name == "strtol" ||
1929 Name == "strtod" ||
1930 Name == "strtof" ||
1931 Name == "strtoul" ||
1932 Name == "strtoll" ||
1933 Name == "strtold" ||
1934 Name == "strncat" ||
1935 Name == "strncpy" ||
1936 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001937 if (FTy->getNumParams() < 2 ||
1938 !isa<PointerType>(FTy->getParamType(1)))
1939 continue;
1940 setDoesNotThrow(F);
1941 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001942 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001943 if (FTy->getNumParams() != 3 ||
1944 !isa<PointerType>(FTy->getParamType(0)) ||
1945 !isa<PointerType>(FTy->getParamType(1)))
1946 continue;
1947 setDoesNotThrow(F);
1948 setDoesNotCapture(F, 1);
1949 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001950 } else if (Name == "strcmp" ||
1951 Name == "strspn" ||
1952 Name == "strncmp" ||
1953 Name ==" strcspn" ||
1954 Name == "strcoll" ||
1955 Name == "strcasecmp" ||
1956 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001957 if (FTy->getNumParams() < 2 ||
1958 !isa<PointerType>(FTy->getParamType(0)) ||
1959 !isa<PointerType>(FTy->getParamType(1)))
1960 continue;
1961 setOnlyReadsMemory(F);
1962 setDoesNotThrow(F);
1963 setDoesNotCapture(F, 1);
1964 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001965 } else if (Name == "strstr" ||
1966 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001967 if (FTy->getNumParams() != 2 ||
1968 !isa<PointerType>(FTy->getParamType(1)))
1969 continue;
1970 setOnlyReadsMemory(F);
1971 setDoesNotThrow(F);
1972 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001973 } else if (Name == "strtok" ||
1974 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001975 if (FTy->getNumParams() < 2 ||
1976 !isa<PointerType>(FTy->getParamType(1)))
1977 continue;
1978 setDoesNotThrow(F);
1979 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001980 } else if (Name == "scanf" ||
1981 Name == "setbuf" ||
1982 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001983 if (FTy->getNumParams() < 1 ||
1984 !isa<PointerType>(FTy->getParamType(0)))
1985 continue;
1986 setDoesNotThrow(F);
1987 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001988 } else if (Name == "strdup" ||
1989 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001990 if (FTy->getNumParams() < 1 ||
1991 !isa<PointerType>(FTy->getReturnType()) ||
1992 !isa<PointerType>(FTy->getParamType(0)))
1993 continue;
1994 setDoesNotThrow(F);
1995 setDoesNotAlias(F, 0);
1996 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001997 } else if (Name == "stat" ||
1998 Name == "sscanf" ||
1999 Name == "sprintf" ||
2000 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002001 if (FTy->getNumParams() < 2 ||
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002002 !isa<PointerType>(FTy->getParamType(0)) ||
2003 !isa<PointerType>(FTy->getParamType(1)))
2004 continue;
2005 setDoesNotThrow(F);
2006 setDoesNotCapture(F, 1);
2007 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002008 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002009 if (FTy->getNumParams() != 3 ||
2010 !isa<PointerType>(FTy->getParamType(0)) ||
2011 !isa<PointerType>(FTy->getParamType(2)))
2012 continue;
2013 setDoesNotThrow(F);
2014 setDoesNotCapture(F, 1);
2015 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002016 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002017 if (FTy->getNumParams() != 3 ||
2018 !isa<PointerType>(FTy->getParamType(1)) ||
2019 !isa<PointerType>(FTy->getParamType(2)))
2020 continue;
2021 setDoesNotThrow(F);
2022 setDoesNotCapture(F, 2);
2023 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002024 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002025 if (FTy->getNumParams() != 1 ||
2026 !isa<PointerType>(FTy->getParamType(0)))
2027 continue;
2028 // May throw; "system" is a valid pthread cancellation point.
2029 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002030 }
2031 break;
2032 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00002033 if (Name == "malloc") {
2034 if (FTy->getNumParams() != 1 ||
2035 !isa<PointerType>(FTy->getReturnType()))
2036 continue;
2037 setDoesNotThrow(F);
2038 setDoesNotAlias(F, 0);
2039 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002040 if (FTy->getNumParams() != 3 ||
2041 !isa<PointerType>(FTy->getParamType(0)) ||
2042 !isa<PointerType>(FTy->getParamType(1)))
2043 continue;
2044 setOnlyReadsMemory(F);
2045 setDoesNotThrow(F);
2046 setDoesNotCapture(F, 1);
2047 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002048 } else if (Name == "memchr" ||
2049 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002050 if (FTy->getNumParams() != 3)
2051 continue;
2052 setOnlyReadsMemory(F);
2053 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002054 } else if (Name == "modf" ||
2055 Name == "modff" ||
2056 Name == "modfl" ||
2057 Name == "memcpy" ||
2058 Name == "memccpy" ||
2059 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002060 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002061 !isa<PointerType>(FTy->getParamType(1)))
2062 continue;
2063 setDoesNotThrow(F);
2064 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002065 } else if (Name == "memalign") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002066 if (!isa<PointerType>(FTy->getReturnType()))
2067 continue;
2068 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002069 } else if (Name == "mkdir" ||
2070 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002071 if (FTy->getNumParams() == 0 ||
2072 !isa<PointerType>(FTy->getParamType(0)))
2073 continue;
2074 setDoesNotThrow(F);
2075 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002076 }
2077 break;
2078 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002079 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002080 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002081 !isa<PointerType>(FTy->getParamType(0)) ||
2082 !isa<PointerType>(FTy->getReturnType()))
2083 continue;
2084 setDoesNotThrow(F);
2085 setDoesNotAlias(F, 0);
2086 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002087 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002088 if (FTy->getNumParams() != 3 ||
2089 !isa<PointerType>(FTy->getParamType(1)))
2090 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002091 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002092 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002093 } else if (Name == "rmdir" ||
2094 Name == "rewind" ||
2095 Name == "remove" ||
2096 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002097 if (FTy->getNumParams() < 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002098 !isa<PointerType>(FTy->getParamType(0)))
2099 continue;
2100 setDoesNotThrow(F);
2101 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002102 } else if (Name == "rename" ||
2103 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002104 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002105 !isa<PointerType>(FTy->getParamType(0)) ||
2106 !isa<PointerType>(FTy->getParamType(1)))
2107 continue;
2108 setDoesNotThrow(F);
2109 setDoesNotCapture(F, 1);
2110 setDoesNotCapture(F, 2);
2111 }
2112 break;
2113 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002114 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002115 if (FTy->getNumParams() != 3 ||
2116 !isa<PointerType>(FTy->getParamType(1)))
2117 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002118 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002119 setDoesNotCapture(F, 2);
2120 }
2121 break;
2122 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002123 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002124 if (FTy->getNumParams() != 3 ||
2125 !isa<PointerType>(FTy->getParamType(0)) ||
2126 !isa<PointerType>(FTy->getParamType(1)))
2127 continue;
2128 setDoesNotThrow(F);
2129 setDoesNotCapture(F, 1);
2130 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002131 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002132 if (FTy->getNumParams() != 3 ||
2133 !isa<PointerType>(FTy->getParamType(0)) ||
2134 !isa<PointerType>(FTy->getParamType(1)))
2135 continue;
2136 setDoesNotThrow(F);
2137 setOnlyReadsMemory(F);
2138 setDoesNotCapture(F, 1);
2139 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002140 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002141 if (FTy->getNumParams() != 2 ||
2142 !isa<PointerType>(FTy->getParamType(0)))
2143 continue;
2144 setDoesNotThrow(F);
2145 setDoesNotCapture(F, 1);
2146 }
2147 break;
2148 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002149 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002150 if (FTy->getNumParams() != 2 ||
2151 !isa<PointerType>(FTy->getReturnType()))
2152 continue;
2153 setDoesNotThrow(F);
2154 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002155 } else if (Name == "chmod" ||
2156 Name == "chown" ||
2157 Name == "ctermid" ||
2158 Name == "clearerr" ||
2159 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002160 if (FTy->getNumParams() == 0 ||
2161 !isa<PointerType>(FTy->getParamType(0)))
2162 continue;
2163 setDoesNotThrow(F);
2164 setDoesNotCapture(F, 1);
2165 }
2166 break;
2167 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002168 if (Name == "atoi" ||
2169 Name == "atol" ||
2170 Name == "atof" ||
2171 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002172 if (FTy->getNumParams() != 1 ||
2173 !isa<PointerType>(FTy->getParamType(0)))
2174 continue;
2175 setDoesNotThrow(F);
2176 setOnlyReadsMemory(F);
2177 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002178 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002179 if (FTy->getNumParams() != 2 ||
2180 !isa<PointerType>(FTy->getParamType(0)))
2181 continue;
2182 setDoesNotThrow(F);
2183 setDoesNotCapture(F, 1);
2184 }
2185 break;
2186 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002187 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002188 if (FTy->getNumParams() != 2 ||
2189 !isa<PointerType>(FTy->getReturnType()) ||
2190 !isa<PointerType>(FTy->getParamType(0)) ||
2191 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002192 continue;
2193 setDoesNotThrow(F);
2194 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002195 setDoesNotCapture(F, 1);
2196 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002197 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002198 if (FTy->getNumParams() != 2 ||
2199 !isa<PointerType>(FTy->getReturnType()) ||
2200 !isa<PointerType>(FTy->getParamType(1)))
2201 continue;
2202 setDoesNotThrow(F);
2203 setDoesNotAlias(F, 0);
2204 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002205 } else if (Name == "feof" ||
2206 Name == "free" ||
2207 Name == "fseek" ||
2208 Name == "ftell" ||
2209 Name == "fgetc" ||
2210 Name == "fseeko" ||
2211 Name == "ftello" ||
2212 Name == "fileno" ||
2213 Name == "fflush" ||
2214 Name == "fclose" ||
2215 Name == "fsetpos" ||
2216 Name == "flockfile" ||
2217 Name == "funlockfile" ||
2218 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002219 if (FTy->getNumParams() == 0 ||
2220 !isa<PointerType>(FTy->getParamType(0)))
2221 continue;
2222 setDoesNotThrow(F);
2223 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002224 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002225 if (FTy->getNumParams() != 1 ||
2226 !isa<PointerType>(FTy->getParamType(0)))
2227 continue;
2228 setDoesNotThrow(F);
2229 setDoesNotCapture(F, 1);
2230 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002231 } else if (Name == "fputc" ||
2232 Name == "fstat" ||
2233 Name == "frexp" ||
2234 Name == "frexpf" ||
2235 Name == "frexpl" ||
2236 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002237 if (FTy->getNumParams() != 2 ||
2238 !isa<PointerType>(FTy->getParamType(1)))
2239 continue;
2240 setDoesNotThrow(F);
2241 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002242 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002243 if (FTy->getNumParams() != 3 ||
2244 !isa<PointerType>(FTy->getParamType(0)) ||
2245 !isa<PointerType>(FTy->getParamType(2)))
2246 continue;
2247 setDoesNotThrow(F);
2248 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002249 } else if (Name == "fread" ||
2250 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002251 if (FTy->getNumParams() != 4 ||
2252 !isa<PointerType>(FTy->getParamType(0)) ||
2253 !isa<PointerType>(FTy->getParamType(3)))
2254 continue;
2255 setDoesNotThrow(F);
2256 setDoesNotCapture(F, 1);
2257 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002258 } else if (Name == "fputs" ||
2259 Name == "fscanf" ||
2260 Name == "fprintf" ||
2261 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002262 if (FTy->getNumParams() < 2 ||
2263 !isa<PointerType>(FTy->getParamType(0)) ||
2264 !isa<PointerType>(FTy->getParamType(1)))
2265 continue;
2266 setDoesNotThrow(F);
2267 setDoesNotCapture(F, 1);
2268 setDoesNotCapture(F, 2);
2269 }
2270 break;
2271 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002272 if (Name == "getc" ||
2273 Name == "getlogin_r" ||
2274 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002275 if (FTy->getNumParams() == 0 ||
2276 !isa<PointerType>(FTy->getParamType(0)))
2277 continue;
2278 setDoesNotThrow(F);
2279 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002280 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002281 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002282 !isa<PointerType>(FTy->getParamType(0)))
2283 continue;
2284 setDoesNotThrow(F);
2285 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002286 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002287 } else if (Name == "gets" ||
2288 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002289 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002290 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002291 if (FTy->getNumParams() != 2 ||
2292 !isa<PointerType>(FTy->getParamType(1)))
2293 continue;
2294 setDoesNotThrow(F);
2295 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002296 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002297 if (FTy->getNumParams() != 1 ||
2298 !isa<PointerType>(FTy->getParamType(0)))
2299 continue;
2300 setDoesNotThrow(F);
2301 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002302 }
2303 break;
2304 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002305 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002306 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002307 !isa<PointerType>(FTy->getParamType(1)))
2308 continue;
2309 setDoesNotThrow(F);
2310 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002311 } else if (Name == "uname" ||
2312 Name == "unlink" ||
2313 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002314 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002315 !isa<PointerType>(FTy->getParamType(0)))
2316 continue;
2317 setDoesNotThrow(F);
2318 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002319 } else if (Name == "utime" ||
2320 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002321 if (FTy->getNumParams() != 2 ||
2322 !isa<PointerType>(FTy->getParamType(0)) ||
2323 !isa<PointerType>(FTy->getParamType(1)))
2324 continue;
2325 setDoesNotThrow(F);
2326 setDoesNotCapture(F, 1);
2327 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002328 }
2329 break;
2330 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002331 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002332 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002333 !isa<PointerType>(FTy->getParamType(1)))
2334 continue;
2335 setDoesNotThrow(F);
2336 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002337 } else if (Name == "puts" ||
2338 Name == "printf" ||
2339 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002340 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002341 !isa<PointerType>(FTy->getParamType(0)))
2342 continue;
2343 setDoesNotThrow(F);
2344 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002345 } else if (Name == "pread" ||
2346 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002347 if (FTy->getNumParams() != 4 ||
2348 !isa<PointerType>(FTy->getParamType(1)))
2349 continue;
2350 // May throw; these are valid pthread cancellation points.
2351 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002352 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002353 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002354 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002355 if (FTy->getNumParams() != 2 ||
2356 !isa<PointerType>(FTy->getReturnType()) ||
2357 !isa<PointerType>(FTy->getParamType(0)) ||
2358 !isa<PointerType>(FTy->getParamType(1)))
2359 continue;
2360 setDoesNotThrow(F);
2361 setDoesNotAlias(F, 0);
2362 setDoesNotCapture(F, 1);
2363 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002364 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002365 if (FTy->getNumParams() != 1 ||
2366 !isa<PointerType>(FTy->getParamType(0)))
2367 continue;
2368 setDoesNotThrow(F);
2369 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002370 }
2371 break;
2372 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002373 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002374 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002375 !isa<PointerType>(FTy->getParamType(1)))
2376 continue;
2377 setDoesNotThrow(F);
2378 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002379 } else if (Name == "vsscanf" ||
2380 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002381 if (FTy->getNumParams() != 3 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002382 !isa<PointerType>(FTy->getParamType(1)) ||
2383 !isa<PointerType>(FTy->getParamType(2)))
2384 continue;
2385 setDoesNotThrow(F);
2386 setDoesNotCapture(F, 1);
2387 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002388 } else if (Name == "valloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002389 if (!isa<PointerType>(FTy->getReturnType()))
2390 continue;
2391 setDoesNotThrow(F);
2392 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002393 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002394 if (FTy->getNumParams() != 2 ||
2395 !isa<PointerType>(FTy->getParamType(0)))
2396 continue;
2397 setDoesNotThrow(F);
2398 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002399 } else if (Name == "vfprintf" ||
2400 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002401 if (FTy->getNumParams() != 3 ||
2402 !isa<PointerType>(FTy->getParamType(0)) ||
2403 !isa<PointerType>(FTy->getParamType(1)))
2404 continue;
2405 setDoesNotThrow(F);
2406 setDoesNotCapture(F, 1);
2407 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002408 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002409 if (FTy->getNumParams() != 4 ||
2410 !isa<PointerType>(FTy->getParamType(0)) ||
2411 !isa<PointerType>(FTy->getParamType(2)))
2412 continue;
2413 setDoesNotThrow(F);
2414 setDoesNotCapture(F, 1);
2415 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002416 }
2417 break;
2418 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002419 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002420 if (FTy->getNumParams() < 2 ||
2421 !isa<PointerType>(FTy->getParamType(0)))
2422 continue;
2423 // May throw; "open" is a valid pthread cancellation point.
2424 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002425 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002426 if (FTy->getNumParams() != 1 ||
2427 !isa<PointerType>(FTy->getReturnType()) ||
2428 !isa<PointerType>(FTy->getParamType(0)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002429 continue;
2430 setDoesNotThrow(F);
2431 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002432 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002433 }
2434 break;
2435 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002436 if (Name == "tmpfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002437 if (!isa<PointerType>(FTy->getReturnType()))
2438 continue;
2439 setDoesNotThrow(F);
2440 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002441 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002442 if (FTy->getNumParams() != 1 ||
2443 !isa<PointerType>(FTy->getParamType(0)))
2444 continue;
2445 setDoesNotThrow(F);
2446 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002447 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002448 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002449 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002450 if (Name == "htonl" ||
2451 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002452 setDoesNotThrow(F);
2453 setDoesNotAccessMemory(F);
2454 }
2455 break;
2456 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002457 if (Name == "ntohl" ||
2458 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002459 setDoesNotThrow(F);
2460 setDoesNotAccessMemory(F);
2461 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002462 break;
2463 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002464 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002465 if (FTy->getNumParams() != 2 ||
2466 !isa<PointerType>(FTy->getParamType(0)) ||
2467 !isa<PointerType>(FTy->getParamType(1)))
2468 continue;
2469 setDoesNotThrow(F);
2470 setDoesNotCapture(F, 1);
2471 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002472 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002473 if (FTy->getNumParams() != 3 ||
2474 !isa<PointerType>(FTy->getParamType(0)))
2475 continue;
2476 setDoesNotThrow(F);
2477 setDoesNotCapture(F, 1);
2478 }
2479 break;
2480 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002481 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002482 if (FTy->getNumParams() != 4 ||
2483 !isa<PointerType>(FTy->getParamType(3)))
2484 continue;
2485 // May throw; places call through function pointer.
2486 setDoesNotCapture(F, 4);
2487 }
2488 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002489 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002490 if (Name == "__strdup" ||
2491 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002492 if (FTy->getNumParams() < 1 ||
2493 !isa<PointerType>(FTy->getReturnType()) ||
2494 !isa<PointerType>(FTy->getParamType(0)))
2495 continue;
2496 setDoesNotThrow(F);
2497 setDoesNotAlias(F, 0);
2498 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002499 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002500 if (FTy->getNumParams() != 3 ||
2501 !isa<PointerType>(FTy->getParamType(1)))
2502 continue;
2503 setDoesNotThrow(F);
2504 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002505 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002506 if (FTy->getNumParams() != 1 ||
2507 !isa<PointerType>(FTy->getParamType(0)))
2508 continue;
2509 setDoesNotThrow(F);
2510 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002511 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002512 if (FTy->getNumParams() != 2 ||
2513 !isa<PointerType>(FTy->getParamType(1)))
2514 continue;
2515 setDoesNotThrow(F);
2516 setDoesNotCapture(F, 2);
2517 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002518 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002519 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002520 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002521 if (FTy->getNumParams() < 1 ||
2522 !isa<PointerType>(FTy->getParamType(0)))
2523 continue;
2524 setDoesNotThrow(F);
2525 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002526 } else if (Name == "\1stat64" ||
2527 Name == "\1lstat64" ||
2528 Name == "\1statvfs64" ||
2529 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002530 if (FTy->getNumParams() < 1 ||
Nick Lewycky225f7472009-02-15 22:47:25 +00002531 !isa<PointerType>(FTy->getParamType(0)) ||
2532 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002533 continue;
2534 setDoesNotThrow(F);
2535 setDoesNotCapture(F, 1);
2536 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002537 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002538 if (FTy->getNumParams() != 2 ||
2539 !isa<PointerType>(FTy->getReturnType()) ||
2540 !isa<PointerType>(FTy->getParamType(0)) ||
2541 !isa<PointerType>(FTy->getParamType(1)))
2542 continue;
2543 setDoesNotThrow(F);
2544 setDoesNotAlias(F, 0);
2545 setDoesNotCapture(F, 1);
2546 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002547 } else if (Name == "\1fseeko64" ||
2548 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002549 if (FTy->getNumParams() == 0 ||
2550 !isa<PointerType>(FTy->getParamType(0)))
2551 continue;
2552 setDoesNotThrow(F);
2553 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002554 } else if (Name == "\1tmpfile64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002555 if (!isa<PointerType>(FTy->getReturnType()))
2556 continue;
2557 setDoesNotThrow(F);
2558 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002559 } else if (Name == "\1fstat64" ||
2560 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002561 if (FTy->getNumParams() != 2 ||
2562 !isa<PointerType>(FTy->getParamType(1)))
2563 continue;
2564 setDoesNotThrow(F);
2565 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002566 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002567 if (FTy->getNumParams() < 2 ||
2568 !isa<PointerType>(FTy->getParamType(0)))
2569 continue;
2570 // May throw; "open" is a valid pthread cancellation point.
2571 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002572 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002573 break;
2574 }
2575 }
2576 return Modified;
2577}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002578
2579// TODO:
2580// Additional cases that we need to add to this file:
2581//
2582// cbrt:
2583// * cbrt(expN(X)) -> expN(x/3)
2584// * cbrt(sqrt(x)) -> pow(x,1/6)
2585// * cbrt(sqrt(x)) -> pow(x,1/9)
2586//
2587// cos, cosf, cosl:
2588// * cos(-x) -> cos(x)
2589//
2590// exp, expf, expl:
2591// * exp(log(x)) -> x
2592//
2593// log, logf, logl:
2594// * log(exp(x)) -> x
2595// * log(x**y) -> y*log(x)
2596// * log(exp(y)) -> y*log(e)
2597// * log(exp2(y)) -> y*log(2)
2598// * log(exp10(y)) -> y*log(10)
2599// * log(sqrt(x)) -> 0.5*log(x)
2600// * log(pow(x,y)) -> y*log(x)
2601//
2602// lround, lroundf, lroundl:
2603// * lround(cnst) -> cnst'
2604//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002605// pow, powf, powl:
2606// * pow(exp(x),y) -> exp(x*y)
2607// * pow(sqrt(x),y) -> pow(x,y*0.5)
2608// * pow(pow(x,y),z)-> pow(x,y*z)
2609//
2610// puts:
2611// * puts("") -> putchar("\n")
2612//
2613// round, roundf, roundl:
2614// * round(cnst) -> cnst'
2615//
2616// signbit:
2617// * signbit(cnst) -> cnst'
2618// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2619//
2620// sqrt, sqrtf, sqrtl:
2621// * sqrt(expN(x)) -> expN(x*0.5)
2622// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2623// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2624//
2625// stpcpy:
2626// * stpcpy(str, "literal") ->
2627// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2628// strrchr:
2629// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2630// (if c is a constant integer and s is a constant string)
2631// * strrchr(s1,0) -> strchr(s1,0)
2632//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002633// strpbrk:
2634// * strpbrk(s,a) -> offset_in_for(s,a)
2635// (if s and a are both constant strings)
2636// * strpbrk(s,"") -> 0
2637// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2638//
2639// strspn, strcspn:
2640// * strspn(s,a) -> const_int (if both args are constant)
2641// * strspn("",a) -> 0
2642// * strspn(s,"") -> 0
2643// * strcspn(s,a) -> const_int (if both args are constant)
2644// * strcspn("",a) -> 0
2645// * strcspn(s,"") -> strlen(a)
2646//
2647// strstr:
2648// * strstr(x,x) -> x
2649// * strstr(s1,s2) -> offset_of_s2_in(s1)
2650// (if s1 and s2 are constant strings)
2651//
2652// tan, tanf, tanl:
2653// * tan(atan(x)) -> x
2654//
2655// trunc, truncf, truncl:
2656// * trunc(cnst) -> cnst'
2657//
2658//