blob: 92af5bb2da6cfb39f5b2a9df129657e2d2b72b61 [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 Christopher3bcaa8a2009-11-20 19:57:37 +000084 Value *EmitMemMove(Value *Dst, Value *Src, Value *Len,
85 unsigned Align, IRBuilder<> &B);
86
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000087 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
88 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
Eric Christopher7a61d702008-08-08 19:39:37 +000089 Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B);
Nick Lewycky13a09e22008-12-21 00:19:21 +000090
91 /// EmitMemCmp - Emit a call to the memcmp function.
92 Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B);
93
Chris Lattnerf5b6bc72009-04-12 05:06:39 +000094 /// EmitMemSet - Emit a call to the memset function
95 Value *EmitMemSet(Value *Dst, Value *Val, Value *Len, IRBuilder<> &B);
96
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000097 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
98 /// 'floor'). This function is known to take a single of type matching 'Op'
99 /// and returns one value with the same type. If 'Op' is a long double, 'l'
100 /// is added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
Dan Gohman79cb8402009-09-25 23:10:17 +0000101 Value *EmitUnaryFloatFnCall(Value *Op, const char *Name, IRBuilder<> &B,
102 const AttrListPtr &Attrs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000103
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000104 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
105 /// is an integer.
Chris Lattner74965f22009-11-09 04:57:04 +0000106 Value *EmitPutChar(Value *Char, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000107
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000108 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
109 /// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000110 void EmitPutS(Value *Str, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000111
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000112 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
113 /// an i32, and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000114 void EmitFPutC(Value *Char, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000115
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000116 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
117 /// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000118 void EmitFPutS(Value *Str, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000119
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000120 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
121 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000122 void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000123
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000124};
125} // End anonymous namespace.
126
127/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Eric Christopher7a61d702008-08-08 19:39:37 +0000128Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) {
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000129 return
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000130 B.CreateBitCast(V, Type::getInt8PtrTy(*Context), "cstr");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000131}
132
133/// EmitStrLen - Emit a call to the strlen function to the builder, for the
134/// specified pointer. This always returns an integer value of size intptr_t.
Eric Christopher7a61d702008-08-08 19:39:37 +0000135Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000136 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000137 AttributeWithIndex AWI[2];
138 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
139 AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
140 Attribute::NoUnwind);
141
142 Constant *StrLen =M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000143 TD->getIntPtrType(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000144 Type::getInt8PtrTy(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000145 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000146 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
147 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
148 CI->setCallingConv(F->getCallingConv());
149
150 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000151}
152
153/// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
154/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
155Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
Eric Christopher7a61d702008-08-08 19:39:37 +0000156 unsigned Align, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000157 Module *M = Caller->getParent();
Chris Lattner824b9582008-11-21 16:42:48 +0000158 Intrinsic::ID IID = Intrinsic::memcpy;
159 const Type *Tys[1];
160 Tys[0] = Len->getType();
161 Value *MemCpy = Intrinsic::getDeclaration(M, IID, Tys, 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000162 return B.CreateCall4(MemCpy, CastToCStr(Dst, B), CastToCStr(Src, B), Len,
Owen Anderson1d0be152009-08-13 21:58:54 +0000163 ConstantInt::get(Type::getInt32Ty(*Context), Align));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000164}
165
Eric Christopher3bcaa8a2009-11-20 19:57:37 +0000166/// EmitMemMOve - Emit a call to the memmove function to the builder. This
167/// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
168Value *LibCallOptimization::EmitMemMove(Value *Dst, Value *Src, Value *Len,
169 unsigned Align, IRBuilder<> &B) {
170 Module *M = Caller->getParent();
171 Intrinsic::ID IID = Intrinsic::memmove;
172 const Type *Tys[1];
173 Tys[0] = TD->getIntPtrType(*Context);
174 Value *MemMove = Intrinsic::getDeclaration(M, IID, Tys, 1);
175 Value *Dst = CastToCStr(CI->getOperand(1), B);
176 Value *Src = CastToCStr(CI->getOperand(2), B);
177 Value *Size = CI->getOperand(3);
178 Value *Align = ConstantInt::get(Type::getInt32Ty(*Context), 1);
179 return B.CreateCall4(MemMove, Dst, Src, Size, Align);
180}
181
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000182/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
183/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
184Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
Eric Christopher7a61d702008-08-08 19:39:37 +0000185 Value *Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000186 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000187 AttributeWithIndex AWI;
188 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
189
190 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
Eric Christopher37c8b862009-10-07 21:14:25 +0000191 Type::getInt8PtrTy(*Context),
192 Type::getInt8PtrTy(*Context),
193 Type::getInt32Ty(*Context),
194 TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000195 NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000196 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
197
198 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
199 CI->setCallingConv(F->getCallingConv());
200
201 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000202}
203
Nick Lewycky13a09e22008-12-21 00:19:21 +0000204/// EmitMemCmp - Emit a call to the memcmp function.
205Value *LibCallOptimization::EmitMemCmp(Value *Ptr1, Value *Ptr2,
206 Value *Len, IRBuilder<> &B) {
207 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000208 AttributeWithIndex AWI[3];
209 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
210 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
211 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
212 Attribute::NoUnwind);
213
214 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000215 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000216 Type::getInt8PtrTy(*Context),
217 Type::getInt8PtrTy(*Context),
Owen Anderson1d0be152009-08-13 21:58:54 +0000218 TD->getIntPtrType(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000219 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
220 Len, "memcmp");
221
222 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
223 CI->setCallingConv(F->getCallingConv());
224
225 return CI;
Nick Lewycky13a09e22008-12-21 00:19:21 +0000226}
227
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000228/// EmitMemSet - Emit a call to the memset function
229Value *LibCallOptimization::EmitMemSet(Value *Dst, Value *Val,
230 Value *Len, IRBuilder<> &B) {
231 Module *M = Caller->getParent();
232 Intrinsic::ID IID = Intrinsic::memset;
233 const Type *Tys[1];
234 Tys[0] = Len->getType();
235 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000236 Value *Align = ConstantInt::get(Type::getInt32Ty(*Context), 1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000237 return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
238}
239
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000240/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
241/// 'floor'). This function is known to take a single of type matching 'Op' and
242/// returns one value with the same type. If 'Op' is a long double, 'l' is
243/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
244Value *LibCallOptimization::EmitUnaryFloatFnCall(Value *Op, const char *Name,
Dan Gohman79cb8402009-09-25 23:10:17 +0000245 IRBuilder<> &B,
246 const AttrListPtr &Attrs) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000247 char NameBuffer[20];
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000248 if (!Op->getType()->isDoubleTy()) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000249 // If we need to add a suffix, copy into NameBuffer.
250 unsigned NameLen = strlen(Name);
251 assert(NameLen < sizeof(NameBuffer)-2);
252 memcpy(NameBuffer, Name, NameLen);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000253 if (Op->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000254 NameBuffer[NameLen] = 'f'; // floorf
255 else
256 NameBuffer[NameLen] = 'l'; // floorl
257 NameBuffer[NameLen+1] = 0;
258 Name = NameBuffer;
259 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000260
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000261 Module *M = Caller->getParent();
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000262 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000263 Op->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000264 CallInst *CI = B.CreateCall(Callee, Op, Name);
Dan Gohman79cb8402009-09-25 23:10:17 +0000265 CI->setAttributes(Attrs);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000266 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
267 CI->setCallingConv(F->getCallingConv());
268
269 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000270}
271
272/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
273/// is an integer.
Chris Lattner74965f22009-11-09 04:57:04 +0000274Value *LibCallOptimization::EmitPutChar(Value *Char, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000275 Module *M = Caller->getParent();
Owen Anderson1d0be152009-08-13 21:58:54 +0000276 Value *PutChar = M->getOrInsertFunction("putchar", Type::getInt32Ty(*Context),
277 Type::getInt32Ty(*Context), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000278 CallInst *CI = B.CreateCall(PutChar,
Eric Christopher37c8b862009-10-07 21:14:25 +0000279 B.CreateIntCast(Char,
280 Type::getInt32Ty(*Context),
Duncan Sandsf63c4102009-11-16 12:32:28 +0000281 /*isSigned*/true,
Eric Christopher37c8b862009-10-07 21:14:25 +0000282 "chari"),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000283 "putchar");
284
285 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
286 CI->setCallingConv(F->getCallingConv());
Chris Lattner74965f22009-11-09 04:57:04 +0000287 return CI;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000288}
289
290/// EmitPutS - Emit a call to the puts function. This assumes that Str is
291/// some pointer.
Eric Christopher7a61d702008-08-08 19:39:37 +0000292void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000293 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000294 AttributeWithIndex AWI[2];
295 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
296 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
297
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000298 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
Owen Anderson1d0be152009-08-13 21:58:54 +0000299 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000300 Type::getInt8PtrTy(*Context),
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000301 NULL);
302 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
303 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
304 CI->setCallingConv(F->getCallingConv());
305
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000306}
307
308/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
309/// an integer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000310void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000311 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000312 AttributeWithIndex AWI[2];
313 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
314 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
315 Constant *F;
316 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000317 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
318 Type::getInt32Ty(*Context),
319 Type::getInt32Ty(*Context), File->getType(),
320 NULL);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000321 else
Eric Christopher37c8b862009-10-07 21:14:25 +0000322 F = M->getOrInsertFunction("fputc",
323 Type::getInt32Ty(*Context),
324 Type::getInt32Ty(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000325 File->getType(), NULL);
Duncan Sandsf63c4102009-11-16 12:32:28 +0000326 Char = B.CreateIntCast(Char, Type::getInt32Ty(*Context), /*isSigned*/true,
327 "chari");
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000328 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
329
330 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
331 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000332}
333
334/// EmitFPutS - Emit a call to the puts function. Str is required to be a
335/// pointer and File is a pointer to FILE.
Eric Christopher7a61d702008-08-08 19:39:37 +0000336void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000337 Module *M = Caller->getParent();
Nick Lewycky225f7472009-02-15 22:47:25 +0000338 AttributeWithIndex AWI[3];
339 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
340 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
341 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000342 Constant *F;
343 if (isa<PointerType>(File->getType()))
Eric Christopher37c8b862009-10-07 21:14:25 +0000344 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
345 Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000346 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000347 File->getType(), NULL);
348 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000349 F = M->getOrInsertFunction("fputs", Type::getInt32Ty(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000350 Type::getInt8PtrTy(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000351 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000352 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
353
354 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
355 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000356}
357
358/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
359/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
360void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Eric Christopher7a61d702008-08-08 19:39:37 +0000361 IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000362 Module *M = Caller->getParent();
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000363 AttributeWithIndex AWI[3];
364 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
365 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
366 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
367 Constant *F;
368 if (isa<PointerType>(File->getType()))
369 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
Owen Anderson1d0be152009-08-13 21:58:54 +0000370 TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000371 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000372 TD->getIntPtrType(*Context),
373 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000374 File->getType(), NULL);
375 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000376 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(*Context),
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000377 Type::getInt8PtrTy(*Context),
Eric Christopher37c8b862009-10-07 21:14:25 +0000378 TD->getIntPtrType(*Context),
379 TD->getIntPtrType(*Context),
Nick Lewycky6cd0c042009-01-05 00:07:50 +0000380 File->getType(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000381 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Owen Anderson1d0be152009-08-13 21:58:54 +0000382 ConstantInt::get(TD->getIntPtrType(*Context), 1), File);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000383
384 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
385 CI->setCallingConv(Fn->getCallingConv());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000386}
387
388//===----------------------------------------------------------------------===//
389// Helper Functions
390//===----------------------------------------------------------------------===//
391
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000392/// GetStringLengthH - If we can compute the length of the string pointed to by
393/// the specified pointer, return 'len+1'. If we can't, return 0.
394static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
395 // Look through noop bitcast instructions.
396 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
397 return GetStringLengthH(BCI->getOperand(0), PHIs);
Eric Christopher37c8b862009-10-07 21:14:25 +0000398
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000399 // If this is a PHI node, there are two cases: either we have already seen it
400 // or we haven't.
401 if (PHINode *PN = dyn_cast<PHINode>(V)) {
402 if (!PHIs.insert(PN))
403 return ~0ULL; // already in the set.
Eric Christopher37c8b862009-10-07 21:14:25 +0000404
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000405 // If it was new, see if all the input strings are the same length.
406 uint64_t LenSoFar = ~0ULL;
407 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
408 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
409 if (Len == 0) return 0; // Unknown length -> unknown.
Eric Christopher37c8b862009-10-07 21:14:25 +0000410
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000411 if (Len == ~0ULL) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +0000412
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000413 if (Len != LenSoFar && LenSoFar != ~0ULL)
414 return 0; // Disagree -> unknown.
415 LenSoFar = Len;
416 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000417
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000418 // Success, all agree.
419 return LenSoFar;
420 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000421
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000422 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
423 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
424 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
425 if (Len1 == 0) return 0;
426 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
427 if (Len2 == 0) return 0;
428 if (Len1 == ~0ULL) return Len2;
429 if (Len2 == ~0ULL) return Len1;
430 if (Len1 != Len2) return 0;
431 return Len1;
432 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000433
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000434 // If the value is not a GEP instruction nor a constant expression with a
435 // GEP instruction, then return unknown.
436 User *GEP = 0;
437 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
438 GEP = GEPI;
439 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
440 if (CE->getOpcode() != Instruction::GetElementPtr)
441 return 0;
442 GEP = CE;
443 } else {
444 return 0;
445 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000446
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000447 // Make sure the GEP has exactly three arguments.
448 if (GEP->getNumOperands() != 3)
449 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000450
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000451 // Check to make sure that the first operand of the GEP is an integer and
452 // has value 0 so that we are sure we're indexing into the initializer.
453 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
454 if (!Idx->isZero())
455 return 0;
456 } else
457 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000458
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000459 // If the second index isn't a ConstantInt, then this is a variable index
460 // into the array. If this occurs, we can't say anything meaningful about
461 // the string.
462 uint64_t StartIdx = 0;
463 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
464 StartIdx = CI->getZExtValue();
465 else
466 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000467
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000468 // The GEP instruction, constant or instruction, must reference a global
469 // variable that is a constant and is initialized. The referenced constant
470 // initializer is the array that we'll use for optimization.
471 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
Dan Gohman107f41f2009-08-19 00:11:12 +0000472 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
473 GV->mayBeOverridden())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000474 return 0;
475 Constant *GlobalInit = GV->getInitializer();
Eric Christopher37c8b862009-10-07 21:14:25 +0000476
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000477 // Handle the ConstantAggregateZero case, which is a degenerate case. The
478 // initializer is constant zero so the length of the string must be zero.
479 if (isa<ConstantAggregateZero>(GlobalInit))
480 return 1; // Len = 0 offset by 1.
Eric Christopher37c8b862009-10-07 21:14:25 +0000481
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000482 // Must be a Constant Array
483 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
Owen Anderson1d0be152009-08-13 21:58:54 +0000484 if (!Array ||
485 Array->getType()->getElementType() != Type::getInt8Ty(V->getContext()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000486 return false;
Eric Christopher37c8b862009-10-07 21:14:25 +0000487
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000488 // Get the number of elements in the array
489 uint64_t NumElts = Array->getType()->getNumElements();
Eric Christopher37c8b862009-10-07 21:14:25 +0000490
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000491 // Traverse the constant array from StartIdx (derived above) which is
492 // the place the GEP refers to in the array.
493 for (unsigned i = StartIdx; i != NumElts; ++i) {
494 Constant *Elt = Array->getOperand(i);
495 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
496 if (!CI) // This array isn't suitable, non-int initializer.
497 return 0;
498 if (CI->isZero())
499 return i-StartIdx+1; // We found end of string, success!
500 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000501
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000502 return 0; // The array isn't null terminated, conservatively return 'unknown'.
503}
504
505/// GetStringLength - If we can compute the length of the string pointed to by
506/// the specified pointer, return 'len+1'. If we can't, return 0.
507static uint64_t GetStringLength(Value *V) {
508 if (!isa<PointerType>(V->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000509
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000510 SmallPtrSet<PHINode*, 32> PHIs;
511 uint64_t Len = GetStringLengthH(V, PHIs);
512 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
513 // an empty string as a length.
514 return Len == ~0ULL ? 1 : Len;
515}
516
517/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +0000518/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000519static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
520 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
521 UI != E; ++UI) {
522 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
523 if (IC->isEquality())
524 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
525 if (C->isNullValue())
526 continue;
527 // Unknown instruction.
528 return false;
529 }
530 return true;
531}
532
533//===----------------------------------------------------------------------===//
Eric Christopher7b5e6172009-10-27 00:52:25 +0000534// Miscellaneous LibCall/Intrinsic Optimizations
535//===----------------------------------------------------------------------===//
536
537namespace {
538struct SizeOpt : public LibCallOptimization {
539 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
540 // TODO: We can do more with this, but delaying to here should be no change
541 // in behavior.
542 ConstantInt *Const = dyn_cast<ConstantInt>(CI->getOperand(2));
543
544 if (!Const) return 0;
545
546 if (Const->getZExtValue() < 2)
547 return Constant::getAllOnesValue(Const->getType());
548 else
549 return ConstantInt::get(Const->getType(), 0);
550 }
551};
552}
553
554//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000555// String and Memory LibCall Optimizations
556//===----------------------------------------------------------------------===//
557
558//===---------------------------------------===//
559// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000560namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000561struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000562 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000563 // Verify the "strcat" function prototype.
564 const FunctionType *FT = Callee->getFunctionType();
565 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000566 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000567 FT->getParamType(0) != FT->getReturnType() ||
568 FT->getParamType(1) != FT->getReturnType())
569 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000570
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000571 // Extract some information from the instruction
572 Value *Dst = CI->getOperand(1);
573 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000574
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000575 // See if we can get the length of the input string.
576 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000577 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000578 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000579
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000580 // Handle the simple, do-nothing case: strcat(x, "") -> x
581 if (Len == 0)
582 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000583
584 // These optimizations require TargetData.
585 if (!TD) return 0;
586
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000587 EmitStrLenMemCpy(Src, Dst, Len, B);
588 return Dst;
589 }
590
591 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000592 // We need to find the end of the destination string. That's where the
593 // memory is to be moved to. We just generate a call to strlen.
594 Value *DstLen = EmitStrLen(Dst, B);
Eric Christopher37c8b862009-10-07 21:14:25 +0000595
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000596 // Now that we have the destination's length, we must index into the
597 // destination's pointer to get the actual memcpy destination (end of
598 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000599 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000600
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000601 // We have enough information to now generate the memcpy call to do the
602 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000603 EmitMemCpy(CpyDst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000604 ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000605 }
606};
607
608//===---------------------------------------===//
609// 'strncat' Optimizations
610
Chris Lattner3e8b6632009-09-02 06:11:42 +0000611struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000612 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
613 // Verify the "strncat" function prototype.
614 const FunctionType *FT = Callee->getFunctionType();
615 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000616 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000617 FT->getParamType(0) != FT->getReturnType() ||
618 FT->getParamType(1) != FT->getReturnType() ||
619 !isa<IntegerType>(FT->getParamType(2)))
620 return 0;
621
622 // Extract some information from the instruction
623 Value *Dst = CI->getOperand(1);
624 Value *Src = CI->getOperand(2);
625 uint64_t Len;
626
627 // We don't do anything if length is not constant
628 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
629 Len = LengthArg->getZExtValue();
630 else
631 return 0;
632
633 // See if we can get the length of the input string.
634 uint64_t SrcLen = GetStringLength(Src);
635 if (SrcLen == 0) return 0;
636 --SrcLen; // Unbias length.
637
638 // Handle the simple, do-nothing cases:
639 // strncat(x, "", c) -> x
640 // strncat(x, c, 0) -> x
641 if (SrcLen == 0 || Len == 0) return Dst;
642
Dan Gohmanf14d9192009-08-18 00:48:13 +0000643 // These optimizations require TargetData.
644 if (!TD) return 0;
645
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000646 // We don't optimize this case
647 if (Len < SrcLen) return 0;
648
649 // strncat(x, s, c) -> strcat(x, s)
650 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000651 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000652 return Dst;
653 }
654};
655
656//===---------------------------------------===//
657// 'strchr' Optimizations
658
Chris Lattner3e8b6632009-09-02 06:11:42 +0000659struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000660 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000661 // Verify the "strchr" function prototype.
662 const FunctionType *FT = Callee->getFunctionType();
663 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000664 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000665 FT->getParamType(0) != FT->getReturnType())
666 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000667
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000668 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000669
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000670 // If the second operand is non-constant, see if we can compute the length
671 // of the input string and turn this into memchr.
672 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
673 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000674 // These optimizations require TargetData.
675 if (!TD) return 0;
676
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000677 uint64_t Len = GetStringLength(SrcStr);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000678 if (Len == 0 ||
679 FT->getParamType(1) != Type::getInt32Ty(*Context)) // memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000680 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000681
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000682 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Owen Anderson1d0be152009-08-13 21:58:54 +0000683 ConstantInt::get(TD->getIntPtrType(*Context), Len), B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000684 }
685
686 // Otherwise, the character is a constant, see if the first argument is
687 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000688 std::string Str;
689 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000690 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000691
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000692 // strchr can find the nul character.
693 Str += '\0';
694 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000695
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000696 // Compute the offset.
697 uint64_t i = 0;
698 while (1) {
699 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000700 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000701 // Did we find our match?
702 if (Str[i] == CharValue)
703 break;
704 ++i;
705 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000706
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000707 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000708 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000709 return B.CreateGEP(SrcStr, Idx, "strchr");
710 }
711};
712
713//===---------------------------------------===//
714// 'strcmp' Optimizations
715
Chris Lattner3e8b6632009-09-02 06:11:42 +0000716struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000717 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000718 // Verify the "strcmp" function prototype.
719 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000720 if (FT->getNumParams() != 2 ||
721 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000722 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000723 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000724 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000725
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000726 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
727 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000728 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000729
Bill Wendling0582ae92009-03-13 04:39:26 +0000730 std::string Str1, Str2;
731 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
732 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000733
Bill Wendling0582ae92009-03-13 04:39:26 +0000734 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000735 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000736
Bill Wendling0582ae92009-03-13 04:39:26 +0000737 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000738 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000739
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000740 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000741 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000742 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000743 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000744
745 // strcmp(P, "x") -> memcmp(P, "x", 2)
746 uint64_t Len1 = GetStringLength(Str1P);
747 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000748 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000749 // These optimizations require TargetData.
750 if (!TD) return 0;
751
Nick Lewycky13a09e22008-12-21 00:19:21 +0000752 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000753 ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattner849832c2009-06-19 04:17:36 +0000754 std::min(Len1, Len2)), B);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000755 }
756
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000757 return 0;
758 }
759};
760
761//===---------------------------------------===//
762// 'strncmp' Optimizations
763
Chris Lattner3e8b6632009-09-02 06:11:42 +0000764struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000765 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000766 // Verify the "strncmp" function prototype.
767 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000768 if (FT->getNumParams() != 3 ||
769 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000770 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000771 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000772 !isa<IntegerType>(FT->getParamType(2)))
773 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000774
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000775 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
776 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000777 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000778
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000779 // Get the length argument if it is constant.
780 uint64_t Length;
781 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
782 Length = LengthArg->getZExtValue();
783 else
784 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000785
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000786 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000787 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000788
Bill Wendling0582ae92009-03-13 04:39:26 +0000789 std::string Str1, Str2;
790 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
791 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000792
Bill Wendling0582ae92009-03-13 04:39:26 +0000793 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000794 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000795
Bill Wendling0582ae92009-03-13 04:39:26 +0000796 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000797 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000798
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000799 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000800 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000801 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000802 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000803 return 0;
804 }
805};
806
807
808//===---------------------------------------===//
809// 'strcpy' Optimizations
810
Chris Lattner3e8b6632009-09-02 06:11:42 +0000811struct StrCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000812 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000813 // Verify the "strcpy" function prototype.
814 const FunctionType *FT = Callee->getFunctionType();
815 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
816 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000817 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000818 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000819
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000820 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
821 if (Dst == Src) // strcpy(x,x) -> x
822 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000823
Dan Gohmanf14d9192009-08-18 00:48:13 +0000824 // These optimizations require TargetData.
825 if (!TD) return 0;
826
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000827 // See if we can get the length of the input string.
828 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000829 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000830
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000831 // We have enough information to now generate the memcpy call to do the
832 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000833 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000834 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000835 return Dst;
836 }
837};
838
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000839//===---------------------------------------===//
840// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000841
Chris Lattner3e8b6632009-09-02 06:11:42 +0000842struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000843 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
844 const FunctionType *FT = Callee->getFunctionType();
845 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
846 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000847 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000848 !isa<IntegerType>(FT->getParamType(2)))
849 return 0;
850
851 Value *Dst = CI->getOperand(1);
852 Value *Src = CI->getOperand(2);
853 Value *LenOp = CI->getOperand(3);
854
855 // See if we can get the length of the input string.
856 uint64_t SrcLen = GetStringLength(Src);
857 if (SrcLen == 0) return 0;
858 --SrcLen;
859
860 if (SrcLen == 0) {
861 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000862 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
863 B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000864 return Dst;
865 }
866
867 uint64_t Len;
868 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
869 Len = LengthArg->getZExtValue();
870 else
871 return 0;
872
873 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
874
Dan Gohmanf14d9192009-08-18 00:48:13 +0000875 // These optimizations require TargetData.
876 if (!TD) return 0;
877
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000878 // Let strncpy handle the zero padding
879 if (Len > SrcLen+1) return 0;
880
881 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000882 EmitMemCpy(Dst, Src,
Owen Anderson1d0be152009-08-13 21:58:54 +0000883 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000884
885 return Dst;
886 }
887};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000888
889//===---------------------------------------===//
890// 'strlen' Optimizations
891
Chris Lattner3e8b6632009-09-02 06:11:42 +0000892struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000893 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000894 const FunctionType *FT = Callee->getFunctionType();
895 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000896 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000897 !isa<IntegerType>(FT->getReturnType()))
898 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000899
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000900 Value *Src = CI->getOperand(1);
901
902 // Constant folding: strlen("xyz") -> 3
903 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000904 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000905
906 // Handle strlen(p) != 0.
907 if (!IsOnlyUsedInZeroEqualityComparison(CI)) return 0;
908
909 // strlen(x) != 0 --> *x != 0
910 // strlen(x) == 0 --> *x == 0
911 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
912 }
913};
914
915//===---------------------------------------===//
Nick Lewycky4c498412009-02-13 15:31:46 +0000916// 'strto*' Optimizations
917
Chris Lattner3e8b6632009-09-02 06:11:42 +0000918struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000919 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
920 const FunctionType *FT = Callee->getFunctionType();
921 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
922 !isa<PointerType>(FT->getParamType(0)) ||
923 !isa<PointerType>(FT->getParamType(1)))
924 return 0;
925
926 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000927 if (isa<ConstantPointerNull>(EndPtr)) {
928 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000929 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000930 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000931
932 return 0;
933 }
934};
935
936
937//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000938// 'memcmp' Optimizations
939
Chris Lattner3e8b6632009-09-02 06:11:42 +0000940struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000941 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000942 const FunctionType *FT = Callee->getFunctionType();
943 if (FT->getNumParams() != 3 || !isa<PointerType>(FT->getParamType(0)) ||
944 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000945 FT->getReturnType() != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000946 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000947
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000948 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000949
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000950 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000951 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000952
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000953 // Make sure we have a constant length.
954 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000955 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000956 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000957
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000958 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000959 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000960
961 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
962 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
963 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +0000964 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000965 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000966
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000967 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS ^ *(short*)RHS) != 0
968 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS ^ *(int*)RHS) != 0
969 if ((Len == 2 || Len == 4) && IsOnlyUsedInZeroEqualityComparison(CI)) {
Owen Andersondebcb012009-07-29 22:17:13 +0000970 const Type *PTy = PointerType::getUnqual(Len == 2 ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000971 Type::getInt16Ty(*Context) : Type::getInt32Ty(*Context));
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000972 LHS = B.CreateBitCast(LHS, PTy, "tmp");
973 RHS = B.CreateBitCast(RHS, PTy, "tmp");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000974 LoadInst *LHSV = B.CreateLoad(LHS, "lhsv");
975 LoadInst *RHSV = B.CreateLoad(RHS, "rhsv");
976 LHSV->setAlignment(1); RHSV->setAlignment(1); // Unaligned loads.
977 return B.CreateZExt(B.CreateXor(LHSV, RHSV, "shortdiff"), CI->getType());
978 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000979
Benjamin Kramer992a6372009-11-05 17:44:22 +0000980 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
981 std::string LHSStr, RHSStr;
982 if (GetConstantStringInfo(LHS, LHSStr) &&
983 GetConstantStringInfo(RHS, RHSStr)) {
984 // Make sure we're not reading out-of-bounds memory.
985 if (Len > LHSStr.length() || Len > RHSStr.length())
986 return 0;
987 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
988 return ConstantInt::get(CI->getType(), Ret);
989 }
990
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000991 return 0;
992 }
993};
994
995//===---------------------------------------===//
996// 'memcpy' Optimizations
997
Chris Lattner3e8b6632009-09-02 06:11:42 +0000998struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000999 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001000 // These optimizations require TargetData.
1001 if (!TD) return 0;
1002
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001003 const FunctionType *FT = Callee->getFunctionType();
1004 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1005 !isa<PointerType>(FT->getParamType(0)) ||
1006 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001007 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001008 return 0;
1009
1010 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
1011 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
1012 return CI->getOperand(1);
1013 }
1014};
1015
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001016//===---------------------------------------===//
1017// 'memmove' Optimizations
1018
Chris Lattner3e8b6632009-09-02 06:11:42 +00001019struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001020 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001021 // These optimizations require TargetData.
1022 if (!TD) return 0;
1023
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001024 const FunctionType *FT = Callee->getFunctionType();
1025 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1026 !isa<PointerType>(FT->getParamType(0)) ||
1027 !isa<PointerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001028 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001029 return 0;
1030
1031 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopher3bcaa8a2009-11-20 19:57:37 +00001032 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001033 return CI->getOperand(1);
1034 }
1035};
1036
1037//===---------------------------------------===//
1038// 'memset' Optimizations
1039
Chris Lattner3e8b6632009-09-02 06:11:42 +00001040struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001041 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001042 // These optimizations require TargetData.
1043 if (!TD) return 0;
1044
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001045 const FunctionType *FT = Callee->getFunctionType();
1046 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1047 !isa<PointerType>(FT->getParamType(0)) ||
Eli Friedman62bb4132009-07-18 08:34:51 +00001048 !isa<IntegerType>(FT->getParamType(1)) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001049 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001050 return 0;
1051
1052 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +00001053 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1054 false);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001055 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001056 return CI->getOperand(1);
1057 }
1058};
1059
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001060//===----------------------------------------------------------------------===//
Eric Christopher3bcaa8a2009-11-20 19:57:37 +00001061// Object Size Checking Optimizations
1062//===----------------------------------------------------------------------===//
1063//===---------------------------------------===//
1064// 'memcpy_chk' Optimizations
1065
1066struct MemCpyChkOpt : public LibCallOptimization {
1067 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1068 // These optimizations require TargetData.
1069 if (!TD) return 0;
1070
1071 const FunctionType *FT = Callee->getFunctionType();
1072 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1073 !isa<PointerType>(FT->getParamType(0)) ||
1074 !isa<PointerType>(FT->getParamType(1)) ||
1075 !isa<IntegerType>(FT->getParamType(3)) ||
1076 FT->getParamType(2) != TD->getIntPtrType(*Context))
1077 return 0;
1078
1079 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1080 if (!SizeCI)
1081 return 0;
1082 if (SizeCI->isAllOnesValue()) {
1083 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
1084 return CI->getOperand(1);
1085 }
1086
1087 return 0;
1088 }
1089};
1090
1091//===---------------------------------------===//
1092// 'memset_chk' Optimizations
1093
1094struct MemSetChkOpt : public LibCallOptimization {
1095 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1096 // These optimizations require TargetData.
1097 if (!TD) return 0;
1098
1099 const FunctionType *FT = Callee->getFunctionType();
1100 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1101 !isa<PointerType>(FT->getParamType(0)) ||
1102 !isa<IntegerType>(FT->getParamType(1)) ||
1103 !isa<IntegerType>(FT->getParamType(3)) ||
1104 FT->getParamType(2) != TD->getIntPtrType(*Context))
1105 return 0;
1106
1107 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1108 if (!SizeCI)
1109 return 0;
1110 if (SizeCI->isAllOnesValue()) {
1111 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
1112 false);
1113 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B);
1114 return CI->getOperand(1);
1115 }
1116
1117 return 0;
1118 }
1119};
1120
1121//===---------------------------------------===//
1122// 'memmove_chk' Optimizations
1123
1124struct MemMoveChkOpt : public LibCallOptimization {
1125 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1126 // These optimizations require TargetData.
1127 if (!TD) return 0;
1128
1129 const FunctionType *FT = Callee->getFunctionType();
1130 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
1131 !isa<PointerType>(FT->getParamType(0)) ||
1132 !isa<PointerType>(FT->getParamType(1)) ||
1133 !isa<IntegerType>(FT->getParamType(3)) ||
1134 FT->getParamType(2) != TD->getIntPtrType(*Context))
1135 return 0;
1136
1137 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
1138 if (!SizeCI)
1139 return 0;
1140 if (SizeCI->isAllOnesValue()) {
1141 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
1142 1, B);
1143 return CI->getOperand(1);
1144 }
1145
1146 return 0;
1147 }
1148};
1149
1150//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001151// Math Library Optimizations
1152//===----------------------------------------------------------------------===//
1153
1154//===---------------------------------------===//
1155// 'pow*' Optimizations
1156
Chris Lattner3e8b6632009-09-02 06:11:42 +00001157struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001158 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001159 const FunctionType *FT = Callee->getFunctionType();
1160 // Just make sure this has 2 arguments of the same FP type, which match the
1161 // result type.
1162 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1163 FT->getParamType(0) != FT->getParamType(1) ||
1164 !FT->getParamType(0)->isFloatingPoint())
1165 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001166
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001167 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
1168 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1169 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
1170 return Op1C;
1171 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +00001172 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001173 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001174
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001175 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1176 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001177
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001178 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001179 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001180
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001181 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +00001182 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1183 // This is faster than calling pow, and still handles negative zero
1184 // and negative infinite correctly.
1185 // TODO: In fast-math mode, this could be just sqrt(x).
1186 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +00001187 Value *Inf = ConstantFP::getInfinity(CI->getType());
1188 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +00001189 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1190 Callee->getAttributes());
1191 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1192 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +00001193 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
1194 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
1195 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001196 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001197
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001198 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1199 return Op1;
1200 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001201 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001202 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001203 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001204 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001205 return 0;
1206 }
1207};
1208
1209//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +00001210// 'exp2' Optimizations
1211
Chris Lattner3e8b6632009-09-02 06:11:42 +00001212struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001213 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +00001214 const FunctionType *FT = Callee->getFunctionType();
1215 // Just make sure this has 1 argument of FP type, which matches the
1216 // result type.
1217 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1218 !FT->getParamType(0)->isFloatingPoint())
1219 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001220
Chris Lattnere818f772008-05-02 18:43:35 +00001221 Value *Op = CI->getOperand(1);
1222 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1223 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1224 Value *LdExpArg = 0;
1225 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1226 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001227 LdExpArg = B.CreateSExt(OpC->getOperand(0),
1228 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001229 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1230 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +00001231 LdExpArg = B.CreateZExt(OpC->getOperand(0),
1232 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +00001233 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001234
Chris Lattnere818f772008-05-02 18:43:35 +00001235 if (LdExpArg) {
1236 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001237 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001238 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001239 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +00001240 Name = "ldexp";
1241 else
1242 Name = "ldexpl";
1243
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001244 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001245 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001246 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +00001247
1248 Module *M = Caller->getParent();
1249 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +00001250 Op->getType(),
1251 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001252 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1253 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1254 CI->setCallingConv(F->getCallingConv());
1255
1256 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +00001257 }
1258 return 0;
1259 }
1260};
Chris Lattnere818f772008-05-02 18:43:35 +00001261
1262//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001263// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1264
Chris Lattner3e8b6632009-09-02 06:11:42 +00001265struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001266 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001267 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001268 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1269 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001270 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +00001271
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001272 // If this is something like 'floor((double)floatval)', convert to floorf.
1273 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001274 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001275 return 0;
1276
1277 // floor((double)floatval) -> (double)floorf(floatval)
1278 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +00001279 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
1280 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +00001281 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001282 }
1283};
1284
1285//===----------------------------------------------------------------------===//
1286// Integer Optimizations
1287//===----------------------------------------------------------------------===//
1288
1289//===---------------------------------------===//
1290// 'ffs*' Optimizations
1291
Chris Lattner3e8b6632009-09-02 06:11:42 +00001292struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001293 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001294 const FunctionType *FT = Callee->getFunctionType();
1295 // Just make sure this has 2 arguments of the same FP type, which match the
1296 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +00001297 if (FT->getNumParams() != 1 ||
1298 FT->getReturnType() != Type::getInt32Ty(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001299 !isa<IntegerType>(FT->getParamType(0)))
1300 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001301
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001302 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001303
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001304 // Constant fold.
1305 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1306 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +00001307 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +00001308 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001309 CI->getValue().countTrailingZeros()+1);
1310 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001311
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001312 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1313 const Type *ArgType = Op->getType();
1314 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1315 Intrinsic::cttz, &ArgType, 1);
1316 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +00001317 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +00001318 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001319
Owen Andersona7235ea2009-07-31 20:28:14 +00001320 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001321 return B.CreateSelect(Cond, V,
1322 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001323 }
1324};
1325
1326//===---------------------------------------===//
1327// 'isdigit' Optimizations
1328
Chris Lattner3e8b6632009-09-02 06:11:42 +00001329struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001330 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001331 const FunctionType *FT = Callee->getFunctionType();
1332 // We require integer(i32)
1333 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001334 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001335 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001336
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001337 // isdigit(c) -> (c-'0') <u 10
1338 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001339 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001340 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001341 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001342 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001343 return B.CreateZExt(Op, CI->getType());
1344 }
1345};
1346
1347//===---------------------------------------===//
1348// 'isascii' Optimizations
1349
Chris Lattner3e8b6632009-09-02 06:11:42 +00001350struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001351 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001352 const FunctionType *FT = Callee->getFunctionType();
1353 // We require integer(i32)
1354 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001355 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001356 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001357
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001358 // isascii(c) -> c <u 128
1359 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001360 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001361 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001362 return B.CreateZExt(Op, CI->getType());
1363 }
1364};
Eric Christopher37c8b862009-10-07 21:14:25 +00001365
Chris Lattner313f0e62008-06-09 08:26:51 +00001366//===---------------------------------------===//
1367// 'abs', 'labs', 'llabs' Optimizations
1368
Chris Lattner3e8b6632009-09-02 06:11:42 +00001369struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001370 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001371 const FunctionType *FT = Callee->getFunctionType();
1372 // We require integer(integer) where the types agree.
1373 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
1374 FT->getParamType(0) != FT->getReturnType())
1375 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001376
Chris Lattner313f0e62008-06-09 08:26:51 +00001377 // abs(x) -> x >s -1 ? x : -x
1378 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001379 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001380 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001381 "ispos");
1382 Value *Neg = B.CreateNeg(Op, "neg");
1383 return B.CreateSelect(Pos, Op, Neg);
1384 }
1385};
Eric Christopher37c8b862009-10-07 21:14:25 +00001386
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001387
1388//===---------------------------------------===//
1389// 'toascii' Optimizations
1390
Chris Lattner3e8b6632009-09-02 06:11:42 +00001391struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001392 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001393 const FunctionType *FT = Callee->getFunctionType();
1394 // We require i32(i32)
1395 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00001396 FT->getParamType(0) != Type::getInt32Ty(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001397 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001398
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001399 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001400 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +00001401 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001402 }
1403};
1404
1405//===----------------------------------------------------------------------===//
1406// Formatting and IO Optimizations
1407//===----------------------------------------------------------------------===//
1408
1409//===---------------------------------------===//
1410// 'printf' Optimizations
1411
Chris Lattner3e8b6632009-09-02 06:11:42 +00001412struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001413 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001414 // Require one fixed pointer argument and an integer/void result.
1415 const FunctionType *FT = Callee->getFunctionType();
1416 if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
1417 !(isa<IntegerType>(FT->getReturnType()) ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001418 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001419 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001420
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001421 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001422 std::string FormatStr;
1423 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1424 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001425
1426 // Empty format string -> noop.
1427 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001428 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001429 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001430
Chris Lattner74965f22009-11-09 04:57:04 +00001431 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1432 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001433 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001434 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
1435 FormatStr[0]), B);
1436 if (CI->use_empty()) return CI;
1437 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001438 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001439
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001440 // printf("foo\n") --> puts("foo")
1441 if (FormatStr[FormatStr.size()-1] == '\n' &&
1442 FormatStr.find('%') == std::string::npos) { // no format characters.
1443 // Create a string literal with no \n on it. We expect the constant merge
1444 // pass to be run after this pass, to merge duplicate strings.
1445 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001446 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001447 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1448 GlobalVariable::InternalLinkage, C, "str");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001449 EmitPutS(C, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001450 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001451 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001452 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001453
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001454 // Optimize specific format strings.
1455 // printf("%c", chr) --> putchar(*(i8*)dst)
1456 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
1457 isa<IntegerType>(CI->getOperand(2)->getType())) {
Chris Lattner74965f22009-11-09 04:57:04 +00001458 Value *Res = EmitPutChar(CI->getOperand(2), B);
1459
1460 if (CI->use_empty()) return CI;
1461 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001462 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001463
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001464 // printf("%s\n", str) --> puts(str)
1465 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
1466 isa<PointerType>(CI->getOperand(2)->getType()) &&
1467 CI->use_empty()) {
1468 EmitPutS(CI->getOperand(2), B);
1469 return CI;
1470 }
1471 return 0;
1472 }
1473};
1474
1475//===---------------------------------------===//
1476// 'sprintf' Optimizations
1477
Chris Lattner3e8b6632009-09-02 06:11:42 +00001478struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001479 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001480 // Require two fixed pointer arguments and an integer result.
1481 const FunctionType *FT = Callee->getFunctionType();
1482 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1483 !isa<PointerType>(FT->getParamType(1)) ||
1484 !isa<IntegerType>(FT->getReturnType()))
1485 return 0;
1486
1487 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001488 std::string FormatStr;
1489 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1490 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001491
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001492 // If we just have a format string (nothing else crazy) transform it.
1493 if (CI->getNumOperands() == 3) {
1494 // Make sure there's no % in the constant array. We could try to handle
1495 // %% -> % in the future if we cared.
1496 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1497 if (FormatStr[i] == '%')
1498 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001499
1500 // These optimizations require TargetData.
1501 if (!TD) return 0;
1502
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001503 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1504 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Owen Anderson1d0be152009-08-13 21:58:54 +00001505 ConstantInt::get(TD->getIntPtrType(*Context), FormatStr.size()+1),1,B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001506 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001507 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001508
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001509 // The remaining optimizations require the format string to be "%s" or "%c"
1510 // and have an extra operand.
1511 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1512 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001513
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001514 // Decode the second character of the format string.
1515 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001516 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001517 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001518 Value *V = B.CreateTrunc(CI->getOperand(3),
1519 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001520 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1521 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001522 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1523 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001524 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001525
Owen Andersoneed707b2009-07-24 23:12:02 +00001526 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001527 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001528
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001529 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001530 // These optimizations require TargetData.
1531 if (!TD) return 0;
1532
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001533 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1534 if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
1535
1536 Value *Len = EmitStrLen(CI->getOperand(3), B);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001537 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001538 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001539 "leninc");
1540 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
Eric Christopher37c8b862009-10-07 21:14:25 +00001541
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001542 // The sprintf result is the unincremented number of bytes in the string.
1543 return B.CreateIntCast(Len, CI->getType(), false);
1544 }
1545 return 0;
1546 }
1547};
1548
1549//===---------------------------------------===//
1550// 'fwrite' Optimizations
1551
Chris Lattner3e8b6632009-09-02 06:11:42 +00001552struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001553 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001554 // Require a pointer, an integer, an integer, a pointer, returning integer.
1555 const FunctionType *FT = Callee->getFunctionType();
1556 if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
1557 !isa<IntegerType>(FT->getParamType(1)) ||
1558 !isa<IntegerType>(FT->getParamType(2)) ||
1559 !isa<PointerType>(FT->getParamType(3)) ||
1560 !isa<IntegerType>(FT->getReturnType()))
1561 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001562
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001563 // Get the element size and count.
1564 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1565 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1566 if (!SizeC || !CountC) return 0;
1567 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001568
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001569 // If this is writing zero records, remove the call (it's a noop).
1570 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001571 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001572
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001573 // If this is writing one byte, turn it into fputc.
1574 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1575 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1576 EmitFPutC(Char, CI->getOperand(4), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001577 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001578 }
1579
1580 return 0;
1581 }
1582};
1583
1584//===---------------------------------------===//
1585// 'fputs' Optimizations
1586
Chris Lattner3e8b6632009-09-02 06:11:42 +00001587struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001588 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001589 // These optimizations require TargetData.
1590 if (!TD) return 0;
1591
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001592 // Require two pointers. Also, we can't optimize if return value is used.
1593 const FunctionType *FT = Callee->getFunctionType();
1594 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1595 !isa<PointerType>(FT->getParamType(1)) ||
1596 !CI->use_empty())
1597 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001598
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001599 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1600 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001601 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001602 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001603 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001604 CI->getOperand(2), B);
1605 return CI; // Known to have no uses (see above).
1606 }
1607};
1608
1609//===---------------------------------------===//
1610// 'fprintf' Optimizations
1611
Chris Lattner3e8b6632009-09-02 06:11:42 +00001612struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001613 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001614 // Require two fixed paramters as pointers and integer result.
1615 const FunctionType *FT = Callee->getFunctionType();
1616 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1617 !isa<PointerType>(FT->getParamType(1)) ||
1618 !isa<IntegerType>(FT->getReturnType()))
1619 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001620
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001621 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001622 std::string FormatStr;
1623 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1624 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001625
1626 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1627 if (CI->getNumOperands() == 3) {
1628 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1629 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001630 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001631
1632 // These optimizations require TargetData.
1633 if (!TD) return 0;
1634
Owen Anderson1d0be152009-08-13 21:58:54 +00001635 EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(*Context),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001636 FormatStr.size()),
1637 CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001638 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001639 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001640
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001641 // The remaining optimizations require the format string to be "%s" or "%c"
1642 // and have an extra operand.
1643 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1644 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001645
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001646 // Decode the second character of the format string.
1647 if (FormatStr[1] == 'c') {
1648 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1649 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1650 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
Owen Andersoneed707b2009-07-24 23:12:02 +00001651 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001652 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001653
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001654 if (FormatStr[1] == 's') {
1655 // fprintf(F, "%s", str) -> fputs(str, F)
1656 if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
1657 return 0;
1658 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1659 return CI;
1660 }
1661 return 0;
1662 }
1663};
1664
Bill Wendlingac178222008-05-05 21:37:59 +00001665} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001666
1667//===----------------------------------------------------------------------===//
1668// SimplifyLibCalls Pass Implementation
1669//===----------------------------------------------------------------------===//
1670
1671namespace {
1672 /// This pass optimizes well known library functions from libc and libm.
1673 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001674 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001675 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001676 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001677 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1678 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
1679 StrToOpt StrTo; MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove;
1680 MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001681 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001682 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001683 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001684 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1685 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001686 // Formatting and IO Optimizations
1687 SPrintFOpt SPrintF; PrintFOpt PrintF;
1688 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher3bcaa8a2009-11-20 19:57:37 +00001689
1690 // Object Size Checking
Eric Christopher7b5e6172009-10-27 00:52:25 +00001691 SizeOpt ObjectSize;
Eric Christopher3bcaa8a2009-11-20 19:57:37 +00001692 MemCpyChkOpt MemCpyChk; MemSetChkOpt MemSetChk; MemMoveChkOpt MemMoveChk;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001693
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001694 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001695 public:
1696 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001697 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001698
1699 void InitOptimizations();
1700 bool runOnFunction(Function &F);
1701
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001702 void setDoesNotAccessMemory(Function &F);
1703 void setOnlyReadsMemory(Function &F);
1704 void setDoesNotThrow(Function &F);
1705 void setDoesNotCapture(Function &F, unsigned n);
1706 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001707 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001708
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001709 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001710 }
1711 };
1712 char SimplifyLibCalls::ID = 0;
1713} // end anonymous namespace.
1714
1715static RegisterPass<SimplifyLibCalls>
1716X("simplify-libcalls", "Simplify well-known library calls");
1717
1718// Public interface to the Simplify LibCalls pass.
1719FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001720 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001721}
1722
1723/// Optimizations - Populate the Optimizations map with all the optimizations
1724/// we know.
1725void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001726 // String and Memory LibCall Optimizations
1727 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001728 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001729 Optimizations["strchr"] = &StrChr;
1730 Optimizations["strcmp"] = &StrCmp;
1731 Optimizations["strncmp"] = &StrNCmp;
1732 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001733 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001734 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001735 Optimizations["strtol"] = &StrTo;
1736 Optimizations["strtod"] = &StrTo;
1737 Optimizations["strtof"] = &StrTo;
1738 Optimizations["strtoul"] = &StrTo;
1739 Optimizations["strtoll"] = &StrTo;
1740 Optimizations["strtold"] = &StrTo;
1741 Optimizations["strtoull"] = &StrTo;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001742 Optimizations["memcmp"] = &MemCmp;
1743 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001744 Optimizations["memmove"] = &MemMove;
1745 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001746
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001747 // Math Library Optimizations
1748 Optimizations["powf"] = &Pow;
1749 Optimizations["pow"] = &Pow;
1750 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001751 Optimizations["llvm.pow.f32"] = &Pow;
1752 Optimizations["llvm.pow.f64"] = &Pow;
1753 Optimizations["llvm.pow.f80"] = &Pow;
1754 Optimizations["llvm.pow.f128"] = &Pow;
1755 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001756 Optimizations["exp2l"] = &Exp2;
1757 Optimizations["exp2"] = &Exp2;
1758 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001759 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1760 Optimizations["llvm.exp2.f128"] = &Exp2;
1761 Optimizations["llvm.exp2.f80"] = &Exp2;
1762 Optimizations["llvm.exp2.f64"] = &Exp2;
1763 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001764
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001765#ifdef HAVE_FLOORF
1766 Optimizations["floor"] = &UnaryDoubleFP;
1767#endif
1768#ifdef HAVE_CEILF
1769 Optimizations["ceil"] = &UnaryDoubleFP;
1770#endif
1771#ifdef HAVE_ROUNDF
1772 Optimizations["round"] = &UnaryDoubleFP;
1773#endif
1774#ifdef HAVE_RINTF
1775 Optimizations["rint"] = &UnaryDoubleFP;
1776#endif
1777#ifdef HAVE_NEARBYINTF
1778 Optimizations["nearbyint"] = &UnaryDoubleFP;
1779#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001780
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001781 // Integer Optimizations
1782 Optimizations["ffs"] = &FFS;
1783 Optimizations["ffsl"] = &FFS;
1784 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001785 Optimizations["abs"] = &Abs;
1786 Optimizations["labs"] = &Abs;
1787 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001788 Optimizations["isdigit"] = &IsDigit;
1789 Optimizations["isascii"] = &IsAscii;
1790 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001791
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001792 // Formatting and IO Optimizations
1793 Optimizations["sprintf"] = &SPrintF;
1794 Optimizations["printf"] = &PrintF;
1795 Optimizations["fwrite"] = &FWrite;
1796 Optimizations["fputs"] = &FPuts;
1797 Optimizations["fprintf"] = &FPrintF;
Eric Christopher7b5e6172009-10-27 00:52:25 +00001798
Eric Christopher3bcaa8a2009-11-20 19:57:37 +00001799 // Object Size Checking
1800 Optimizations["llvm.objectsize.i32"] = &ObjectSize;
1801 Optimizations["llvm.objectsize.i64"] = &ObjectSize;
1802 Optimizations["__memcpy_chk"] = &MemCpyChk;
1803 Optimizations["__memset_chk"] = &MemSetChk;
1804 Optimizations["__memmove_chk"] = &MemMoveChk;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001805}
1806
1807
1808/// runOnFunction - Top level algorithm.
1809///
1810bool SimplifyLibCalls::runOnFunction(Function &F) {
1811 if (Optimizations.empty())
1812 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001813
Dan Gohmanf14d9192009-08-18 00:48:13 +00001814 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001815
Owen Andersone922c022009-07-22 00:24:57 +00001816 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001817
1818 bool Changed = false;
1819 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1820 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1821 // Ignore non-calls.
1822 CallInst *CI = dyn_cast<CallInst>(I++);
1823 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001824
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001825 // Ignore indirect calls and calls to non-external functions.
1826 Function *Callee = CI->getCalledFunction();
1827 if (Callee == 0 || !Callee->isDeclaration() ||
1828 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1829 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001830
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001831 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001832 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1833 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001834
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001835 // Set the builder to the instruction after the call.
1836 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001837
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001838 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001839 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001840 if (Result == 0) continue;
1841
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001842 DEBUG(errs() << "SimplifyLibCalls simplified: " << *CI;
1843 errs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001844
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001845 // Something changed!
1846 Changed = true;
1847 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001848
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001849 // Inspect the instruction after the call (which was potentially just
1850 // added) next.
1851 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001852
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001853 if (CI != Result && !CI->use_empty()) {
1854 CI->replaceAllUsesWith(Result);
1855 if (!Result->hasName())
1856 Result->takeName(CI);
1857 }
1858 CI->eraseFromParent();
1859 }
1860 }
1861 return Changed;
1862}
1863
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001864// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001865
1866void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1867 if (!F.doesNotAccessMemory()) {
1868 F.setDoesNotAccessMemory();
1869 ++NumAnnotated;
1870 Modified = true;
1871 }
1872}
1873void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1874 if (!F.onlyReadsMemory()) {
1875 F.setOnlyReadsMemory();
1876 ++NumAnnotated;
1877 Modified = true;
1878 }
1879}
1880void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1881 if (!F.doesNotThrow()) {
1882 F.setDoesNotThrow();
1883 ++NumAnnotated;
1884 Modified = true;
1885 }
1886}
1887void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1888 if (!F.doesNotCapture(n)) {
1889 F.setDoesNotCapture(n);
1890 ++NumAnnotated;
1891 Modified = true;
1892 }
1893}
1894void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1895 if (!F.doesNotAlias(n)) {
1896 F.setDoesNotAlias(n);
1897 ++NumAnnotated;
1898 Modified = true;
1899 }
1900}
1901
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001902/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001903///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001904bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001905 Modified = false;
1906 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1907 Function &F = *I;
1908 if (!F.isDeclaration())
1909 continue;
1910
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001911 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001912 continue;
1913
1914 const FunctionType *FTy = F.getFunctionType();
1915
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001916 StringRef Name = F.getName();
1917 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001918 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001919 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001920 if (FTy->getNumParams() != 1 ||
1921 !isa<PointerType>(FTy->getParamType(0)))
1922 continue;
1923 setOnlyReadsMemory(F);
1924 setDoesNotThrow(F);
1925 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001926 } else if (Name == "strcpy" ||
1927 Name == "stpcpy" ||
1928 Name == "strcat" ||
1929 Name == "strtol" ||
1930 Name == "strtod" ||
1931 Name == "strtof" ||
1932 Name == "strtoul" ||
1933 Name == "strtoll" ||
1934 Name == "strtold" ||
1935 Name == "strncat" ||
1936 Name == "strncpy" ||
1937 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001938 if (FTy->getNumParams() < 2 ||
1939 !isa<PointerType>(FTy->getParamType(1)))
1940 continue;
1941 setDoesNotThrow(F);
1942 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001943 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001944 if (FTy->getNumParams() != 3 ||
1945 !isa<PointerType>(FTy->getParamType(0)) ||
1946 !isa<PointerType>(FTy->getParamType(1)))
1947 continue;
1948 setDoesNotThrow(F);
1949 setDoesNotCapture(F, 1);
1950 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001951 } else if (Name == "strcmp" ||
1952 Name == "strspn" ||
1953 Name == "strncmp" ||
1954 Name ==" strcspn" ||
1955 Name == "strcoll" ||
1956 Name == "strcasecmp" ||
1957 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001958 if (FTy->getNumParams() < 2 ||
1959 !isa<PointerType>(FTy->getParamType(0)) ||
1960 !isa<PointerType>(FTy->getParamType(1)))
1961 continue;
1962 setOnlyReadsMemory(F);
1963 setDoesNotThrow(F);
1964 setDoesNotCapture(F, 1);
1965 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001966 } else if (Name == "strstr" ||
1967 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001968 if (FTy->getNumParams() != 2 ||
1969 !isa<PointerType>(FTy->getParamType(1)))
1970 continue;
1971 setOnlyReadsMemory(F);
1972 setDoesNotThrow(F);
1973 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001974 } else if (Name == "strtok" ||
1975 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001976 if (FTy->getNumParams() < 2 ||
1977 !isa<PointerType>(FTy->getParamType(1)))
1978 continue;
1979 setDoesNotThrow(F);
1980 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001981 } else if (Name == "scanf" ||
1982 Name == "setbuf" ||
1983 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001984 if (FTy->getNumParams() < 1 ||
1985 !isa<PointerType>(FTy->getParamType(0)))
1986 continue;
1987 setDoesNotThrow(F);
1988 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001989 } else if (Name == "strdup" ||
1990 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001991 if (FTy->getNumParams() < 1 ||
1992 !isa<PointerType>(FTy->getReturnType()) ||
1993 !isa<PointerType>(FTy->getParamType(0)))
1994 continue;
1995 setDoesNotThrow(F);
1996 setDoesNotAlias(F, 0);
1997 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001998 } else if (Name == "stat" ||
1999 Name == "sscanf" ||
2000 Name == "sprintf" ||
2001 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002002 if (FTy->getNumParams() < 2 ||
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002003 !isa<PointerType>(FTy->getParamType(0)) ||
2004 !isa<PointerType>(FTy->getParamType(1)))
2005 continue;
2006 setDoesNotThrow(F);
2007 setDoesNotCapture(F, 1);
2008 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002009 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002010 if (FTy->getNumParams() != 3 ||
2011 !isa<PointerType>(FTy->getParamType(0)) ||
2012 !isa<PointerType>(FTy->getParamType(2)))
2013 continue;
2014 setDoesNotThrow(F);
2015 setDoesNotCapture(F, 1);
2016 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002017 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002018 if (FTy->getNumParams() != 3 ||
2019 !isa<PointerType>(FTy->getParamType(1)) ||
2020 !isa<PointerType>(FTy->getParamType(2)))
2021 continue;
2022 setDoesNotThrow(F);
2023 setDoesNotCapture(F, 2);
2024 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002025 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002026 if (FTy->getNumParams() != 1 ||
2027 !isa<PointerType>(FTy->getParamType(0)))
2028 continue;
2029 // May throw; "system" is a valid pthread cancellation point.
2030 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002031 }
2032 break;
2033 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00002034 if (Name == "malloc") {
2035 if (FTy->getNumParams() != 1 ||
2036 !isa<PointerType>(FTy->getReturnType()))
2037 continue;
2038 setDoesNotThrow(F);
2039 setDoesNotAlias(F, 0);
2040 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002041 if (FTy->getNumParams() != 3 ||
2042 !isa<PointerType>(FTy->getParamType(0)) ||
2043 !isa<PointerType>(FTy->getParamType(1)))
2044 continue;
2045 setOnlyReadsMemory(F);
2046 setDoesNotThrow(F);
2047 setDoesNotCapture(F, 1);
2048 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002049 } else if (Name == "memchr" ||
2050 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002051 if (FTy->getNumParams() != 3)
2052 continue;
2053 setOnlyReadsMemory(F);
2054 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002055 } else if (Name == "modf" ||
2056 Name == "modff" ||
2057 Name == "modfl" ||
2058 Name == "memcpy" ||
2059 Name == "memccpy" ||
2060 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002061 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002062 !isa<PointerType>(FTy->getParamType(1)))
2063 continue;
2064 setDoesNotThrow(F);
2065 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002066 } else if (Name == "memalign") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002067 if (!isa<PointerType>(FTy->getReturnType()))
2068 continue;
2069 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002070 } else if (Name == "mkdir" ||
2071 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002072 if (FTy->getNumParams() == 0 ||
2073 !isa<PointerType>(FTy->getParamType(0)))
2074 continue;
2075 setDoesNotThrow(F);
2076 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002077 }
2078 break;
2079 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002080 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002081 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002082 !isa<PointerType>(FTy->getParamType(0)) ||
2083 !isa<PointerType>(FTy->getReturnType()))
2084 continue;
2085 setDoesNotThrow(F);
2086 setDoesNotAlias(F, 0);
2087 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002088 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002089 if (FTy->getNumParams() != 3 ||
2090 !isa<PointerType>(FTy->getParamType(1)))
2091 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002092 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002093 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002094 } else if (Name == "rmdir" ||
2095 Name == "rewind" ||
2096 Name == "remove" ||
2097 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002098 if (FTy->getNumParams() < 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002099 !isa<PointerType>(FTy->getParamType(0)))
2100 continue;
2101 setDoesNotThrow(F);
2102 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002103 } else if (Name == "rename" ||
2104 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002105 if (FTy->getNumParams() < 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002106 !isa<PointerType>(FTy->getParamType(0)) ||
2107 !isa<PointerType>(FTy->getParamType(1)))
2108 continue;
2109 setDoesNotThrow(F);
2110 setDoesNotCapture(F, 1);
2111 setDoesNotCapture(F, 2);
2112 }
2113 break;
2114 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002115 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002116 if (FTy->getNumParams() != 3 ||
2117 !isa<PointerType>(FTy->getParamType(1)))
2118 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002119 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002120 setDoesNotCapture(F, 2);
2121 }
2122 break;
2123 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002124 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002125 if (FTy->getNumParams() != 3 ||
2126 !isa<PointerType>(FTy->getParamType(0)) ||
2127 !isa<PointerType>(FTy->getParamType(1)))
2128 continue;
2129 setDoesNotThrow(F);
2130 setDoesNotCapture(F, 1);
2131 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002132 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002133 if (FTy->getNumParams() != 3 ||
2134 !isa<PointerType>(FTy->getParamType(0)) ||
2135 !isa<PointerType>(FTy->getParamType(1)))
2136 continue;
2137 setDoesNotThrow(F);
2138 setOnlyReadsMemory(F);
2139 setDoesNotCapture(F, 1);
2140 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002141 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002142 if (FTy->getNumParams() != 2 ||
2143 !isa<PointerType>(FTy->getParamType(0)))
2144 continue;
2145 setDoesNotThrow(F);
2146 setDoesNotCapture(F, 1);
2147 }
2148 break;
2149 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002150 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002151 if (FTy->getNumParams() != 2 ||
2152 !isa<PointerType>(FTy->getReturnType()))
2153 continue;
2154 setDoesNotThrow(F);
2155 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002156 } else if (Name == "chmod" ||
2157 Name == "chown" ||
2158 Name == "ctermid" ||
2159 Name == "clearerr" ||
2160 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002161 if (FTy->getNumParams() == 0 ||
2162 !isa<PointerType>(FTy->getParamType(0)))
2163 continue;
2164 setDoesNotThrow(F);
2165 setDoesNotCapture(F, 1);
2166 }
2167 break;
2168 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002169 if (Name == "atoi" ||
2170 Name == "atol" ||
2171 Name == "atof" ||
2172 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002173 if (FTy->getNumParams() != 1 ||
2174 !isa<PointerType>(FTy->getParamType(0)))
2175 continue;
2176 setDoesNotThrow(F);
2177 setOnlyReadsMemory(F);
2178 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002179 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002180 if (FTy->getNumParams() != 2 ||
2181 !isa<PointerType>(FTy->getParamType(0)))
2182 continue;
2183 setDoesNotThrow(F);
2184 setDoesNotCapture(F, 1);
2185 }
2186 break;
2187 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002188 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002189 if (FTy->getNumParams() != 2 ||
2190 !isa<PointerType>(FTy->getReturnType()) ||
2191 !isa<PointerType>(FTy->getParamType(0)) ||
2192 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002193 continue;
2194 setDoesNotThrow(F);
2195 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002196 setDoesNotCapture(F, 1);
2197 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002198 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002199 if (FTy->getNumParams() != 2 ||
2200 !isa<PointerType>(FTy->getReturnType()) ||
2201 !isa<PointerType>(FTy->getParamType(1)))
2202 continue;
2203 setDoesNotThrow(F);
2204 setDoesNotAlias(F, 0);
2205 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002206 } else if (Name == "feof" ||
2207 Name == "free" ||
2208 Name == "fseek" ||
2209 Name == "ftell" ||
2210 Name == "fgetc" ||
2211 Name == "fseeko" ||
2212 Name == "ftello" ||
2213 Name == "fileno" ||
2214 Name == "fflush" ||
2215 Name == "fclose" ||
2216 Name == "fsetpos" ||
2217 Name == "flockfile" ||
2218 Name == "funlockfile" ||
2219 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002220 if (FTy->getNumParams() == 0 ||
2221 !isa<PointerType>(FTy->getParamType(0)))
2222 continue;
2223 setDoesNotThrow(F);
2224 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002225 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002226 if (FTy->getNumParams() != 1 ||
2227 !isa<PointerType>(FTy->getParamType(0)))
2228 continue;
2229 setDoesNotThrow(F);
2230 setDoesNotCapture(F, 1);
2231 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002232 } else if (Name == "fputc" ||
2233 Name == "fstat" ||
2234 Name == "frexp" ||
2235 Name == "frexpf" ||
2236 Name == "frexpl" ||
2237 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002238 if (FTy->getNumParams() != 2 ||
2239 !isa<PointerType>(FTy->getParamType(1)))
2240 continue;
2241 setDoesNotThrow(F);
2242 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002243 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002244 if (FTy->getNumParams() != 3 ||
2245 !isa<PointerType>(FTy->getParamType(0)) ||
2246 !isa<PointerType>(FTy->getParamType(2)))
2247 continue;
2248 setDoesNotThrow(F);
2249 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002250 } else if (Name == "fread" ||
2251 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002252 if (FTy->getNumParams() != 4 ||
2253 !isa<PointerType>(FTy->getParamType(0)) ||
2254 !isa<PointerType>(FTy->getParamType(3)))
2255 continue;
2256 setDoesNotThrow(F);
2257 setDoesNotCapture(F, 1);
2258 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002259 } else if (Name == "fputs" ||
2260 Name == "fscanf" ||
2261 Name == "fprintf" ||
2262 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002263 if (FTy->getNumParams() < 2 ||
2264 !isa<PointerType>(FTy->getParamType(0)) ||
2265 !isa<PointerType>(FTy->getParamType(1)))
2266 continue;
2267 setDoesNotThrow(F);
2268 setDoesNotCapture(F, 1);
2269 setDoesNotCapture(F, 2);
2270 }
2271 break;
2272 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002273 if (Name == "getc" ||
2274 Name == "getlogin_r" ||
2275 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002276 if (FTy->getNumParams() == 0 ||
2277 !isa<PointerType>(FTy->getParamType(0)))
2278 continue;
2279 setDoesNotThrow(F);
2280 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002281 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002282 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002283 !isa<PointerType>(FTy->getParamType(0)))
2284 continue;
2285 setDoesNotThrow(F);
2286 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002287 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002288 } else if (Name == "gets" ||
2289 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002290 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002291 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002292 if (FTy->getNumParams() != 2 ||
2293 !isa<PointerType>(FTy->getParamType(1)))
2294 continue;
2295 setDoesNotThrow(F);
2296 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002297 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002298 if (FTy->getNumParams() != 1 ||
2299 !isa<PointerType>(FTy->getParamType(0)))
2300 continue;
2301 setDoesNotThrow(F);
2302 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002303 }
2304 break;
2305 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002306 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002307 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002308 !isa<PointerType>(FTy->getParamType(1)))
2309 continue;
2310 setDoesNotThrow(F);
2311 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002312 } else if (Name == "uname" ||
2313 Name == "unlink" ||
2314 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002315 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002316 !isa<PointerType>(FTy->getParamType(0)))
2317 continue;
2318 setDoesNotThrow(F);
2319 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002320 } else if (Name == "utime" ||
2321 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002322 if (FTy->getNumParams() != 2 ||
2323 !isa<PointerType>(FTy->getParamType(0)) ||
2324 !isa<PointerType>(FTy->getParamType(1)))
2325 continue;
2326 setDoesNotThrow(F);
2327 setDoesNotCapture(F, 1);
2328 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002329 }
2330 break;
2331 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002332 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002333 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002334 !isa<PointerType>(FTy->getParamType(1)))
2335 continue;
2336 setDoesNotThrow(F);
2337 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002338 } else if (Name == "puts" ||
2339 Name == "printf" ||
2340 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002341 if (FTy->getNumParams() != 1 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002342 !isa<PointerType>(FTy->getParamType(0)))
2343 continue;
2344 setDoesNotThrow(F);
2345 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002346 } else if (Name == "pread" ||
2347 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002348 if (FTy->getNumParams() != 4 ||
2349 !isa<PointerType>(FTy->getParamType(1)))
2350 continue;
2351 // May throw; these are valid pthread cancellation points.
2352 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002353 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002354 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002355 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002356 if (FTy->getNumParams() != 2 ||
2357 !isa<PointerType>(FTy->getReturnType()) ||
2358 !isa<PointerType>(FTy->getParamType(0)) ||
2359 !isa<PointerType>(FTy->getParamType(1)))
2360 continue;
2361 setDoesNotThrow(F);
2362 setDoesNotAlias(F, 0);
2363 setDoesNotCapture(F, 1);
2364 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002365 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002366 if (FTy->getNumParams() != 1 ||
2367 !isa<PointerType>(FTy->getParamType(0)))
2368 continue;
2369 setDoesNotThrow(F);
2370 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002371 }
2372 break;
2373 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002374 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002375 if (FTy->getNumParams() != 2 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002376 !isa<PointerType>(FTy->getParamType(1)))
2377 continue;
2378 setDoesNotThrow(F);
2379 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002380 } else if (Name == "vsscanf" ||
2381 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002382 if (FTy->getNumParams() != 3 ||
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002383 !isa<PointerType>(FTy->getParamType(1)) ||
2384 !isa<PointerType>(FTy->getParamType(2)))
2385 continue;
2386 setDoesNotThrow(F);
2387 setDoesNotCapture(F, 1);
2388 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002389 } else if (Name == "valloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002390 if (!isa<PointerType>(FTy->getReturnType()))
2391 continue;
2392 setDoesNotThrow(F);
2393 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002394 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002395 if (FTy->getNumParams() != 2 ||
2396 !isa<PointerType>(FTy->getParamType(0)))
2397 continue;
2398 setDoesNotThrow(F);
2399 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002400 } else if (Name == "vfprintf" ||
2401 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002402 if (FTy->getNumParams() != 3 ||
2403 !isa<PointerType>(FTy->getParamType(0)) ||
2404 !isa<PointerType>(FTy->getParamType(1)))
2405 continue;
2406 setDoesNotThrow(F);
2407 setDoesNotCapture(F, 1);
2408 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002409 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002410 if (FTy->getNumParams() != 4 ||
2411 !isa<PointerType>(FTy->getParamType(0)) ||
2412 !isa<PointerType>(FTy->getParamType(2)))
2413 continue;
2414 setDoesNotThrow(F);
2415 setDoesNotCapture(F, 1);
2416 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002417 }
2418 break;
2419 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002420 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002421 if (FTy->getNumParams() < 2 ||
2422 !isa<PointerType>(FTy->getParamType(0)))
2423 continue;
2424 // May throw; "open" is a valid pthread cancellation point.
2425 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002426 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002427 if (FTy->getNumParams() != 1 ||
2428 !isa<PointerType>(FTy->getReturnType()) ||
2429 !isa<PointerType>(FTy->getParamType(0)))
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002430 continue;
2431 setDoesNotThrow(F);
2432 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002433 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002434 }
2435 break;
2436 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002437 if (Name == "tmpfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002438 if (!isa<PointerType>(FTy->getReturnType()))
2439 continue;
2440 setDoesNotThrow(F);
2441 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002442 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002443 if (FTy->getNumParams() != 1 ||
2444 !isa<PointerType>(FTy->getParamType(0)))
2445 continue;
2446 setDoesNotThrow(F);
2447 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002448 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002449 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002450 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002451 if (Name == "htonl" ||
2452 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002453 setDoesNotThrow(F);
2454 setDoesNotAccessMemory(F);
2455 }
2456 break;
2457 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002458 if (Name == "ntohl" ||
2459 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002460 setDoesNotThrow(F);
2461 setDoesNotAccessMemory(F);
2462 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002463 break;
2464 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002465 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002466 if (FTy->getNumParams() != 2 ||
2467 !isa<PointerType>(FTy->getParamType(0)) ||
2468 !isa<PointerType>(FTy->getParamType(1)))
2469 continue;
2470 setDoesNotThrow(F);
2471 setDoesNotCapture(F, 1);
2472 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002473 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002474 if (FTy->getNumParams() != 3 ||
2475 !isa<PointerType>(FTy->getParamType(0)))
2476 continue;
2477 setDoesNotThrow(F);
2478 setDoesNotCapture(F, 1);
2479 }
2480 break;
2481 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002482 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002483 if (FTy->getNumParams() != 4 ||
2484 !isa<PointerType>(FTy->getParamType(3)))
2485 continue;
2486 // May throw; places call through function pointer.
2487 setDoesNotCapture(F, 4);
2488 }
2489 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002490 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002491 if (Name == "__strdup" ||
2492 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002493 if (FTy->getNumParams() < 1 ||
2494 !isa<PointerType>(FTy->getReturnType()) ||
2495 !isa<PointerType>(FTy->getParamType(0)))
2496 continue;
2497 setDoesNotThrow(F);
2498 setDoesNotAlias(F, 0);
2499 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002500 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002501 if (FTy->getNumParams() != 3 ||
2502 !isa<PointerType>(FTy->getParamType(1)))
2503 continue;
2504 setDoesNotThrow(F);
2505 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002506 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002507 if (FTy->getNumParams() != 1 ||
2508 !isa<PointerType>(FTy->getParamType(0)))
2509 continue;
2510 setDoesNotThrow(F);
2511 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002512 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002513 if (FTy->getNumParams() != 2 ||
2514 !isa<PointerType>(FTy->getParamType(1)))
2515 continue;
2516 setDoesNotThrow(F);
2517 setDoesNotCapture(F, 2);
2518 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002519 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002520 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002521 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002522 if (FTy->getNumParams() < 1 ||
2523 !isa<PointerType>(FTy->getParamType(0)))
2524 continue;
2525 setDoesNotThrow(F);
2526 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002527 } else if (Name == "\1stat64" ||
2528 Name == "\1lstat64" ||
2529 Name == "\1statvfs64" ||
2530 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002531 if (FTy->getNumParams() < 1 ||
Nick Lewycky225f7472009-02-15 22:47:25 +00002532 !isa<PointerType>(FTy->getParamType(0)) ||
2533 !isa<PointerType>(FTy->getParamType(1)))
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002534 continue;
2535 setDoesNotThrow(F);
2536 setDoesNotCapture(F, 1);
2537 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002538 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002539 if (FTy->getNumParams() != 2 ||
2540 !isa<PointerType>(FTy->getReturnType()) ||
2541 !isa<PointerType>(FTy->getParamType(0)) ||
2542 !isa<PointerType>(FTy->getParamType(1)))
2543 continue;
2544 setDoesNotThrow(F);
2545 setDoesNotAlias(F, 0);
2546 setDoesNotCapture(F, 1);
2547 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002548 } else if (Name == "\1fseeko64" ||
2549 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002550 if (FTy->getNumParams() == 0 ||
2551 !isa<PointerType>(FTy->getParamType(0)))
2552 continue;
2553 setDoesNotThrow(F);
2554 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002555 } else if (Name == "\1tmpfile64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002556 if (!isa<PointerType>(FTy->getReturnType()))
2557 continue;
2558 setDoesNotThrow(F);
2559 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002560 } else if (Name == "\1fstat64" ||
2561 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002562 if (FTy->getNumParams() != 2 ||
2563 !isa<PointerType>(FTy->getParamType(1)))
2564 continue;
2565 setDoesNotThrow(F);
2566 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002567 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002568 if (FTy->getNumParams() < 2 ||
2569 !isa<PointerType>(FTy->getParamType(0)))
2570 continue;
2571 // May throw; "open" is a valid pthread cancellation point.
2572 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002573 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002574 break;
2575 }
2576 }
2577 return Modified;
2578}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002579
2580// TODO:
2581// Additional cases that we need to add to this file:
2582//
2583// cbrt:
2584// * cbrt(expN(X)) -> expN(x/3)
2585// * cbrt(sqrt(x)) -> pow(x,1/6)
2586// * cbrt(sqrt(x)) -> pow(x,1/9)
2587//
2588// cos, cosf, cosl:
2589// * cos(-x) -> cos(x)
2590//
2591// exp, expf, expl:
2592// * exp(log(x)) -> x
2593//
2594// log, logf, logl:
2595// * log(exp(x)) -> x
2596// * log(x**y) -> y*log(x)
2597// * log(exp(y)) -> y*log(e)
2598// * log(exp2(y)) -> y*log(2)
2599// * log(exp10(y)) -> y*log(10)
2600// * log(sqrt(x)) -> 0.5*log(x)
2601// * log(pow(x,y)) -> y*log(x)
2602//
2603// lround, lroundf, lroundl:
2604// * lround(cnst) -> cnst'
2605//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002606// pow, powf, powl:
2607// * pow(exp(x),y) -> exp(x*y)
2608// * pow(sqrt(x),y) -> pow(x,y*0.5)
2609// * pow(pow(x,y),z)-> pow(x,y*z)
2610//
2611// puts:
2612// * puts("") -> putchar("\n")
2613//
2614// round, roundf, roundl:
2615// * round(cnst) -> cnst'
2616//
2617// signbit:
2618// * signbit(cnst) -> cnst'
2619// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2620//
2621// sqrt, sqrtf, sqrtl:
2622// * sqrt(expN(x)) -> expN(x*0.5)
2623// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2624// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2625//
2626// stpcpy:
2627// * stpcpy(str, "literal") ->
2628// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2629// strrchr:
2630// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2631// (if c is a constant integer and s is a constant string)
2632// * strrchr(s1,0) -> strchr(s1,0)
2633//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002634// strpbrk:
2635// * strpbrk(s,a) -> offset_in_for(s,a)
2636// (if s and a are both constant strings)
2637// * strpbrk(s,"") -> 0
2638// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2639//
2640// strspn, strcspn:
2641// * strspn(s,a) -> const_int (if both args are constant)
2642// * strspn("",a) -> 0
2643// * strspn(s,"") -> 0
2644// * strcspn(s,a) -> const_int (if both args are constant)
2645// * strcspn("",a) -> 0
2646// * strcspn(s,"") -> strlen(a)
2647//
2648// strstr:
2649// * strstr(x,x) -> x
2650// * strstr(s1,s2) -> offset_of_s2_in(s1)
2651// (if s1 and s2 are constant strings)
2652//
2653// tan, tanf, tanl:
2654// * tan(atan(x)) -> x
2655//
2656// trunc, truncf, truncl:
2657// * trunc(cnst) -> cnst'
2658//
2659//