blob: 81460bcf4de704eacf2c8b38dbc986e383ebffbe [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"
Eric Christopherb6174e32010-03-05 22:25:30 +000020#include "llvm/Transforms/Utils/BuildLibCalls.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000021#include "llvm/Intrinsics.h"
Owen Andersonfa5cbd62009-07-03 19:42:02 +000022#include "llvm/LLVMContext.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000023#include "llvm/Module.h"
24#include "llvm/Pass.h"
25#include "llvm/Support/IRBuilder.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000026#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000027#include "llvm/Target/TargetData.h"
28#include "llvm/ADT/SmallPtrSet.h"
29#include "llvm/ADT/StringMap.h"
30#include "llvm/ADT/Statistic.h"
Daniel Dunbar473955f2009-07-29 22:00:43 +000031#include "llvm/ADT/STLExtras.h"
Chris Lattner56b4f2b2008-05-01 06:39:12 +000032#include "llvm/Support/Debug.h"
Daniel Dunbarf0443c12009-07-26 08:34:35 +000033#include "llvm/Support/raw_ostream.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000034#include "llvm/Config/config.h"
35using namespace llvm;
36
37STATISTIC(NumSimplified, "Number of library calls simplified");
Nick Lewycky0f8df9a2009-01-04 20:27:34 +000038STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000039
40//===----------------------------------------------------------------------===//
41// Optimizer Base Class
42//===----------------------------------------------------------------------===//
43
44/// This class is the abstract base class for the set of optimizations that
45/// corresponds to one library call.
46namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +000047class LibCallOptimization {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000048protected:
49 Function *Caller;
50 const TargetData *TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000051 LLVMContext* Context;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000052public:
Evan Chengeb8c6452010-03-24 20:19:04 +000053 LibCallOptimization() { }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000054 virtual ~LibCallOptimization() {}
55
56 /// CallOptimizer - This pure virtual method is implemented by base classes to
57 /// do various optimizations. If this returns null then no transformation was
58 /// performed. If it returns CI, then it transformed the call and CI is to be
59 /// deleted. If it returns something else, replace CI with the new value and
60 /// delete CI.
Eric Christopher37c8b862009-10-07 21:14:25 +000061 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
Eric Christopher7a61d702008-08-08 19:39:37 +000062 =0;
Eric Christopher37c8b862009-10-07 21:14:25 +000063
Dan Gohmanf14d9192009-08-18 00:48:13 +000064 Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000065 Caller = CI->getParent()->getParent();
Dan Gohmanf14d9192009-08-18 00:48:13 +000066 this->TD = TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000067 if (CI->getCalledFunction())
Owen Andersone922c022009-07-22 00:24:57 +000068 Context = &CI->getCalledFunction()->getContext();
Rafael Espindolae96af562010-06-16 19:34:01 +000069
70 // We never change the calling convention.
71 if (CI->getCallingConv() != llvm::CallingConv::C)
72 return NULL;
73
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000074 return CallOptimizer(CI->getCalledFunction(), CI, B);
75 }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000076};
77} // End anonymous namespace.
78
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000079
80//===----------------------------------------------------------------------===//
81// Helper Functions
82//===----------------------------------------------------------------------===//
83
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000084/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +000085/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000086static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
87 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
88 UI != E; ++UI) {
89 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
90 if (IC->isEquality())
91 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
92 if (C->isNullValue())
93 continue;
94 // Unknown instruction.
95 return false;
96 }
97 return true;
98}
99
Benjamin Kramer386e9182010-06-15 21:34:25 +0000100/// IsOnlyUsedInEqualityComparison - Return true if it is only used in equality
101/// comparisons with With.
102static bool IsOnlyUsedInEqualityComparison(Value *V, Value *With) {
103 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
104 UI != E; ++UI) {
105 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
106 if (IC->isEquality() && IC->getOperand(1) == With)
107 continue;
108 // Unknown instruction.
109 return false;
110 }
111 return true;
112}
113
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000114//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000115// String and Memory LibCall Optimizations
116//===----------------------------------------------------------------------===//
117
118//===---------------------------------------===//
119// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000120namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000121struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000122 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000123 // Verify the "strcat" function prototype.
124 const FunctionType *FT = Callee->getFunctionType();
125 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000126 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000127 FT->getParamType(0) != FT->getReturnType() ||
128 FT->getParamType(1) != FT->getReturnType())
129 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000130
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000131 // Extract some information from the instruction
Gabor Greifaee5dc12010-06-24 10:42:46 +0000132 Value *Dst = CI->getArgOperand(0);
133 Value *Src = CI->getArgOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000134
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000135 // See if we can get the length of the input string.
136 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000137 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000138 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000139
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000140 // Handle the simple, do-nothing case: strcat(x, "") -> x
141 if (Len == 0)
142 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000143
144 // These optimizations require TargetData.
145 if (!TD) return 0;
146
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000147 EmitStrLenMemCpy(Src, Dst, Len, B);
148 return Dst;
149 }
150
151 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000152 // We need to find the end of the destination string. That's where the
153 // memory is to be moved to. We just generate a call to strlen.
Eric Christopherb6174e32010-03-05 22:25:30 +0000154 Value *DstLen = EmitStrLen(Dst, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000155
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000156 // Now that we have the destination's length, we must index into the
157 // destination's pointer to get the actual memcpy destination (end of
158 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000159 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000160
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000161 // We have enough information to now generate the memcpy call to do the
162 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000163 EmitMemCpy(CpyDst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000164 ConstantInt::get(TD->getIntPtrType(*Context), Len+1),
165 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000166 }
167};
168
169//===---------------------------------------===//
170// 'strncat' Optimizations
171
Chris Lattner3e8b6632009-09-02 06:11:42 +0000172struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000173 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
174 // Verify the "strncat" function prototype.
175 const FunctionType *FT = Callee->getFunctionType();
176 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000177 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000178 FT->getParamType(0) != FT->getReturnType() ||
179 FT->getParamType(1) != FT->getReturnType() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000180 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000181 return 0;
182
183 // Extract some information from the instruction
Gabor Greifaee5dc12010-06-24 10:42:46 +0000184 Value *Dst = CI->getArgOperand(0);
185 Value *Src = CI->getArgOperand(1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000186 uint64_t Len;
187
188 // We don't do anything if length is not constant
Gabor Greifaee5dc12010-06-24 10:42:46 +0000189 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000190 Len = LengthArg->getZExtValue();
191 else
192 return 0;
193
194 // See if we can get the length of the input string.
195 uint64_t SrcLen = GetStringLength(Src);
196 if (SrcLen == 0) return 0;
197 --SrcLen; // Unbias length.
198
199 // Handle the simple, do-nothing cases:
200 // strncat(x, "", c) -> x
201 // strncat(x, c, 0) -> x
202 if (SrcLen == 0 || Len == 0) return Dst;
203
Dan Gohmanf14d9192009-08-18 00:48:13 +0000204 // These optimizations require TargetData.
205 if (!TD) return 0;
206
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000207 // We don't optimize this case
208 if (Len < SrcLen) return 0;
209
210 // strncat(x, s, c) -> strcat(x, s)
211 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000212 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000213 return Dst;
214 }
215};
216
217//===---------------------------------------===//
218// 'strchr' Optimizations
219
Chris Lattner3e8b6632009-09-02 06:11:42 +0000220struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000221 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000222 // Verify the "strchr" function prototype.
223 const FunctionType *FT = Callee->getFunctionType();
224 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000225 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000226 FT->getParamType(0) != FT->getReturnType())
227 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000228
Gabor Greifaee5dc12010-06-24 10:42:46 +0000229 Value *SrcStr = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000230
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000231 // If the second operand is non-constant, see if we can compute the length
232 // of the input string and turn this into memchr.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000233 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000234 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000235 // These optimizations require TargetData.
236 if (!TD) return 0;
237
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000238 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000239 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000240 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000241
Gabor Greifaee5dc12010-06-24 10:42:46 +0000242 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000243 ConstantInt::get(TD->getIntPtrType(*Context), Len),
244 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000245 }
246
247 // Otherwise, the character is a constant, see if the first argument is
248 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000249 std::string Str;
250 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000251 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000252
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000253 // strchr can find the nul character.
254 Str += '\0';
Eric Christopher37c8b862009-10-07 21:14:25 +0000255
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000256 // Compute the offset.
Benjamin Kramere2609902010-09-29 22:29:12 +0000257 size_t I = Str.find(CharC->getSExtValue());
258 if (I == std::string::npos) // Didn't find the char. strchr returns null.
259 return Constant::getNullValue(CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000260
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000261 // strchr(s+n,c) -> gep(s+n+i,c)
Benjamin Kramere2609902010-09-29 22:29:12 +0000262 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000263 return B.CreateGEP(SrcStr, Idx, "strchr");
264 }
265};
266
267//===---------------------------------------===//
Benjamin Kramer06f25cf2010-09-29 21:50:51 +0000268// 'strrchr' Optimizations
269
270struct StrRChrOpt : public LibCallOptimization {
271 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
272 // Verify the "strrchr" function prototype.
273 const FunctionType *FT = Callee->getFunctionType();
274 if (FT->getNumParams() != 2 ||
275 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
276 FT->getParamType(0) != FT->getReturnType())
277 return 0;
278
279 Value *SrcStr = CI->getArgOperand(0);
280 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
281
282 // Cannot fold anything if we're not looking for a constant.
283 if (!CharC)
284 return 0;
285
286 std::string Str;
287 if (!GetConstantStringInfo(SrcStr, Str)) {
288 // strrchr(s, 0) -> strchr(s, 0)
289 if (TD && CharC->isZero())
290 return EmitStrChr(SrcStr, '\0', B, TD);
291 return 0;
292 }
293
294 // strrchr can find the nul character.
295 Str += '\0';
296
297 // Compute the offset.
298 size_t I = Str.rfind(CharC->getSExtValue());
299 if (I == std::string::npos) // Didn't find the char. Return null.
300 return Constant::getNullValue(CI->getType());
301
302 // strrchr(s+n,c) -> gep(s+n+i,c)
303 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
304 return B.CreateGEP(SrcStr, Idx, "strrchr");
305 }
306};
307
308//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000309// 'strcmp' Optimizations
310
Chris Lattner3e8b6632009-09-02 06:11:42 +0000311struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000312 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000313 // Verify the "strcmp" function prototype.
314 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000315 if (FT->getNumParams() != 2 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000316 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000317 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000318 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000319 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000320
Gabor Greifaee5dc12010-06-24 10:42:46 +0000321 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000322 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000323 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000324
Bill Wendling0582ae92009-03-13 04:39:26 +0000325 std::string Str1, Str2;
326 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
327 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000328
Bill Wendling0582ae92009-03-13 04:39:26 +0000329 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000330 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000331
Bill Wendling0582ae92009-03-13 04:39:26 +0000332 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000333 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000334
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000335 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000336 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000337 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000338 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000339
340 // strcmp(P, "x") -> memcmp(P, "x", 2)
341 uint64_t Len1 = GetStringLength(Str1P);
342 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000343 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000344 // These optimizations require TargetData.
345 if (!TD) return 0;
346
Nick Lewycky13a09e22008-12-21 00:19:21 +0000347 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000348 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000349 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000350 }
351
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000352 return 0;
353 }
354};
355
356//===---------------------------------------===//
357// 'strncmp' Optimizations
358
Chris Lattner3e8b6632009-09-02 06:11:42 +0000359struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000360 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000361 // Verify the "strncmp" function prototype.
362 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000363 if (FT->getNumParams() != 3 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000364 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000365 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000366 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000367 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000368 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000369
Gabor Greifaee5dc12010-06-24 10:42:46 +0000370 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000371 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000372 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000373
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000374 // Get the length argument if it is constant.
375 uint64_t Length;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000376 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000377 Length = LengthArg->getZExtValue();
378 else
379 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000380
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000381 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000382 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000383
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000384 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000385 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD);
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000386
Bill Wendling0582ae92009-03-13 04:39:26 +0000387 std::string Str1, Str2;
388 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
389 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000390
Bill Wendling0582ae92009-03-13 04:39:26 +0000391 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000392 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000393
Bill Wendling0582ae92009-03-13 04:39:26 +0000394 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000395 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000396
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000397 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000398 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000399 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000400 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000401 return 0;
402 }
403};
404
405
406//===---------------------------------------===//
407// 'strcpy' Optimizations
408
Chris Lattner3e8b6632009-09-02 06:11:42 +0000409struct StrCpyOpt : public LibCallOptimization {
Evan Chengeb8c6452010-03-24 20:19:04 +0000410 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
411
412 StrCpyOpt(bool c) : OptChkCall(c) {}
413
Eric Christopher7a61d702008-08-08 19:39:37 +0000414 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000415 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000416 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000417 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000418 if (FT->getNumParams() != NumParams ||
419 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000420 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000421 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000422 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000423
Gabor Greifaee5dc12010-06-24 10:42:46 +0000424 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000425 if (Dst == Src) // strcpy(x,x) -> x
426 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000427
Dan Gohmanf14d9192009-08-18 00:48:13 +0000428 // These optimizations require TargetData.
429 if (!TD) return 0;
430
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000431 // See if we can get the length of the input string.
432 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000433 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000434
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000435 // We have enough information to now generate the memcpy call to do the
436 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000437 if (OptChkCall)
438 EmitMemCpyChk(Dst, Src,
439 ConstantInt::get(TD->getIntPtrType(*Context), Len),
Gabor Greifaee5dc12010-06-24 10:42:46 +0000440 CI->getArgOperand(2), B, TD);
Evan Cheng0289b412010-03-23 15:48:04 +0000441 else
442 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000443 ConstantInt::get(TD->getIntPtrType(*Context), Len),
444 1, false, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000445 return Dst;
446 }
447};
448
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000449//===---------------------------------------===//
450// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000451
Chris Lattner3e8b6632009-09-02 06:11:42 +0000452struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000453 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
454 const FunctionType *FT = Callee->getFunctionType();
455 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
456 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000457 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000458 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000459 return 0;
460
Gabor Greifaee5dc12010-06-24 10:42:46 +0000461 Value *Dst = CI->getArgOperand(0);
462 Value *Src = CI->getArgOperand(1);
463 Value *LenOp = CI->getArgOperand(2);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000464
465 // See if we can get the length of the input string.
466 uint64_t SrcLen = GetStringLength(Src);
467 if (SrcLen == 0) return 0;
468 --SrcLen;
469
470 if (SrcLen == 0) {
471 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Mon P Wang20adc9d2010-04-04 03:10:48 +0000472 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'),
473 LenOp, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000474 return Dst;
475 }
476
477 uint64_t Len;
478 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
479 Len = LengthArg->getZExtValue();
480 else
481 return 0;
482
483 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
484
Dan Gohmanf14d9192009-08-18 00:48:13 +0000485 // These optimizations require TargetData.
486 if (!TD) return 0;
487
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000488 // Let strncpy handle the zero padding
489 if (Len > SrcLen+1) return 0;
490
491 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000492 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000493 ConstantInt::get(TD->getIntPtrType(*Context), Len),
494 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000495
496 return Dst;
497 }
498};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000499
500//===---------------------------------------===//
501// 'strlen' Optimizations
502
Chris Lattner3e8b6632009-09-02 06:11:42 +0000503struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000504 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000505 const FunctionType *FT = Callee->getFunctionType();
506 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000507 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000508 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000509 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000510
Gabor Greifaee5dc12010-06-24 10:42:46 +0000511 Value *Src = CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000512
513 // Constant folding: strlen("xyz") -> 3
514 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000515 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000516
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000517 // strlen(x) != 0 --> *x != 0
518 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000519 if (IsOnlyUsedInZeroEqualityComparison(CI))
520 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
521 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000522 }
523};
524
Benjamin Kramer05f585e2010-09-29 23:52:12 +0000525
526//===---------------------------------------===//
527// 'strpbrk' Optimizations
528
529struct StrPBrkOpt : public LibCallOptimization {
530 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
531 const FunctionType *FT = Callee->getFunctionType();
532 if (FT->getNumParams() != 2 ||
533 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
534 FT->getParamType(1) != FT->getParamType(0) ||
535 FT->getReturnType() != FT->getParamType(0))
536 return 0;
537
538 std::string S1, S2;
539 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
540 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
541
542 // strpbrk(s, "") -> NULL
543 // strpbrk("", s) -> NULL
544 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
545 return Constant::getNullValue(CI->getType());
546
547 // Constant folding.
548 if (HasS1 && HasS2) {
549 size_t I = S1.find_first_of(S2);
550 if (I == std::string::npos) // No match.
551 return Constant::getNullValue(CI->getType());
552
553 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
554 return B.CreateGEP(CI->getArgOperand(0), Idx, "strpbrk");
555 }
556
557 // strpbrk(s, "a") -> strchr(s, 'a')
558 if (TD && HasS2 && S2.size() == 1)
559 return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD);
560
561 return 0;
562 }
563};
564
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000565//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000566// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000567
Chris Lattner3e8b6632009-09-02 06:11:42 +0000568struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000569 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
570 const FunctionType *FT = Callee->getFunctionType();
571 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000572 !FT->getParamType(0)->isPointerTy() ||
573 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000574 return 0;
575
Gabor Greifaee5dc12010-06-24 10:42:46 +0000576 Value *EndPtr = CI->getArgOperand(1);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000577 if (isa<ConstantPointerNull>(EndPtr)) {
578 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000579 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000580 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000581
582 return 0;
583 }
584};
585
Chris Lattner24604112009-12-16 09:32:05 +0000586//===---------------------------------------===//
Benjamin Kramer9510a252010-09-30 00:58:35 +0000587// 'strspn' Optimizations
588
589struct StrSpnOpt : public LibCallOptimization {
590 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
591 const FunctionType *FT = Callee->getFunctionType();
592 if (FT->getNumParams() != 2 ||
593 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
594 FT->getParamType(1) != FT->getParamType(0) ||
595 !FT->getReturnType()->isIntegerTy())
596 return 0;
597
598 std::string S1, S2;
599 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
600 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
601
602 // strspn(s, "") -> 0
603 // strspn("", s) -> 0
604 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
605 return Constant::getNullValue(CI->getType());
606
607 // Constant folding.
608 if (HasS1 && HasS2)
609 return ConstantInt::get(CI->getType(), strspn(S1.c_str(), S2.c_str()));
610
611 return 0;
612 }
613};
614
615//===---------------------------------------===//
616// 'strcspn' Optimizations
617
618struct StrCSpnOpt : public LibCallOptimization {
619 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
620 const FunctionType *FT = Callee->getFunctionType();
621 if (FT->getNumParams() != 2 ||
622 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
623 FT->getParamType(1) != FT->getParamType(0) ||
624 !FT->getReturnType()->isIntegerTy())
625 return 0;
626
627 std::string S1, S2;
628 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
629 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
630
631 // strcspn("", s) -> 0
632 if (HasS1 && S1.empty())
633 return Constant::getNullValue(CI->getType());
634
635 // Constant folding.
636 if (HasS1 && HasS2)
637 return ConstantInt::get(CI->getType(), strcspn(S1.c_str(), S2.c_str()));
638
639 // strcspn(s, "") -> strlen(s)
640 if (TD && HasS2 && S2.empty())
641 return EmitStrLen(CI->getArgOperand(0), B, TD);
642
643 return 0;
644 }
645};
646
647//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000648// 'strstr' Optimizations
649
650struct StrStrOpt : public LibCallOptimization {
651 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
652 const FunctionType *FT = Callee->getFunctionType();
653 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000654 !FT->getParamType(0)->isPointerTy() ||
655 !FT->getParamType(1)->isPointerTy() ||
656 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000657 return 0;
658
659 // fold strstr(x, x) -> x.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000660 if (CI->getArgOperand(0) == CI->getArgOperand(1))
661 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000662
Benjamin Kramer386e9182010-06-15 21:34:25 +0000663 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000664 if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
665 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD);
666 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000667 StrLen, B, TD);
668 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
669 UI != UE; ) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000670 ICmpInst *Old = cast<ICmpInst>(*UI++);
Benjamin Kramer386e9182010-06-15 21:34:25 +0000671 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
672 ConstantInt::getNullValue(StrNCmp->getType()),
673 "cmp");
674 Old->replaceAllUsesWith(Cmp);
675 Old->eraseFromParent();
676 }
677 return CI;
678 }
679
Chris Lattner24604112009-12-16 09:32:05 +0000680 // See if either input string is a constant string.
681 std::string SearchStr, ToFindStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000682 bool HasStr1 = GetConstantStringInfo(CI->getArgOperand(0), SearchStr);
683 bool HasStr2 = GetConstantStringInfo(CI->getArgOperand(1), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000684
Chris Lattner24604112009-12-16 09:32:05 +0000685 // fold strstr(x, "") -> x.
686 if (HasStr2 && ToFindStr.empty())
Gabor Greifaee5dc12010-06-24 10:42:46 +0000687 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000688
Chris Lattner24604112009-12-16 09:32:05 +0000689 // If both strings are known, constant fold it.
690 if (HasStr1 && HasStr2) {
691 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000692
Chris Lattner24604112009-12-16 09:32:05 +0000693 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
694 return Constant::getNullValue(CI->getType());
695
696 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000697 Value *Result = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner24604112009-12-16 09:32:05 +0000698 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
699 return B.CreateBitCast(Result, CI->getType());
700 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000701
Chris Lattner24604112009-12-16 09:32:05 +0000702 // fold strstr(x, "y") -> strchr(x, 'y').
703 if (HasStr2 && ToFindStr.size() == 1)
Gabor Greifa3997812010-07-22 10:37:47 +0000704 return B.CreateBitCast(EmitStrChr(CI->getArgOperand(0),
705 ToFindStr[0], B, TD), CI->getType());
Chris Lattner24604112009-12-16 09:32:05 +0000706 return 0;
707 }
708};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000709
Nick Lewycky4c498412009-02-13 15:31:46 +0000710
711//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000712// 'memcmp' Optimizations
713
Chris Lattner3e8b6632009-09-02 06:11:42 +0000714struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000715 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000716 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000717 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
718 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000719 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000720 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000721
Gabor Greifaee5dc12010-06-24 10:42:46 +0000722 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000723
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000724 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000725 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000726
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000727 // Make sure we have a constant length.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000728 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000729 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000730 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000731
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000732 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000733 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000734
Benjamin Kramer48aefe12010-05-25 22:53:43 +0000735 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
736 if (Len == 1) {
737 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
738 CI->getType(), "lhsv");
739 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
740 CI->getType(), "rhsv");
Benjamin Kramer1464c1d2010-05-26 09:45:04 +0000741 return B.CreateSub(LHSV, RHSV, "chardiff");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000742 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000743
Benjamin Kramer992a6372009-11-05 17:44:22 +0000744 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
745 std::string LHSStr, RHSStr;
746 if (GetConstantStringInfo(LHS, LHSStr) &&
747 GetConstantStringInfo(RHS, RHSStr)) {
748 // Make sure we're not reading out-of-bounds memory.
749 if (Len > LHSStr.length() || Len > RHSStr.length())
750 return 0;
751 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
752 return ConstantInt::get(CI->getType(), Ret);
753 }
754
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000755 return 0;
756 }
757};
758
759//===---------------------------------------===//
760// 'memcpy' Optimizations
761
Chris Lattner3e8b6632009-09-02 06:11:42 +0000762struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000763 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000764 // These optimizations require TargetData.
765 if (!TD) return 0;
766
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000767 const FunctionType *FT = Callee->getFunctionType();
768 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000769 !FT->getParamType(0)->isPointerTy() ||
770 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000771 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000772 return 0;
773
774 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000775 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
776 CI->getArgOperand(2), 1, false, B, TD);
777 return CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000778 }
779};
780
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000781//===---------------------------------------===//
782// 'memmove' Optimizations
783
Chris Lattner3e8b6632009-09-02 06:11:42 +0000784struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000785 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000786 // These optimizations require TargetData.
787 if (!TD) return 0;
788
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000789 const FunctionType *FT = Callee->getFunctionType();
790 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000791 !FT->getParamType(0)->isPointerTy() ||
792 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000793 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000794 return 0;
795
796 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000797 EmitMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
798 CI->getArgOperand(2), 1, false, B, TD);
799 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000800 }
801};
802
803//===---------------------------------------===//
804// 'memset' Optimizations
805
Chris Lattner3e8b6632009-09-02 06:11:42 +0000806struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000807 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000808 // These optimizations require TargetData.
809 if (!TD) return 0;
810
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000811 const FunctionType *FT = Callee->getFunctionType();
812 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000813 !FT->getParamType(0)->isPointerTy() ||
814 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000815 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000816 return 0;
817
818 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Gabor Greifa3997812010-07-22 10:37:47 +0000819 Value *Val = B.CreateIntCast(CI->getArgOperand(1),
820 Type::getInt8Ty(*Context), false);
Gabor Greifaee5dc12010-06-24 10:42:46 +0000821 EmitMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), false, B, TD);
822 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000823 }
824};
825
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000826//===----------------------------------------------------------------------===//
827// Math Library Optimizations
828//===----------------------------------------------------------------------===//
829
830//===---------------------------------------===//
831// 'pow*' Optimizations
832
Chris Lattner3e8b6632009-09-02 06:11:42 +0000833struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000834 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000835 const FunctionType *FT = Callee->getFunctionType();
836 // Just make sure this has 2 arguments of the same FP type, which match the
837 // result type.
838 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
839 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000840 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000841 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000842
Gabor Greifaee5dc12010-06-24 10:42:46 +0000843 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000844 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
845 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
846 return Op1C;
847 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000848 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000849 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000850
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000851 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
852 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000853
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000854 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000855 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000856
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000857 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000858 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
859 // This is faster than calling pow, and still handles negative zero
860 // and negative infinite correctly.
861 // TODO: In fast-math mode, this could be just sqrt(x).
862 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000863 Value *Inf = ConstantFP::getInfinity(CI->getType());
864 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000865 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
866 Callee->getAttributes());
867 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
868 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000869 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
870 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
871 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000872 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000873
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000874 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
875 return Op1;
876 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000877 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000878 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000879 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000880 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000881 return 0;
882 }
883};
884
885//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000886// 'exp2' Optimizations
887
Chris Lattner3e8b6632009-09-02 06:11:42 +0000888struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000889 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000890 const FunctionType *FT = Callee->getFunctionType();
891 // Just make sure this has 1 argument of FP type, which matches the
892 // result type.
893 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000894 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000895 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000896
Gabor Greifaee5dc12010-06-24 10:42:46 +0000897 Value *Op = CI->getArgOperand(0);
Chris Lattnere818f772008-05-02 18:43:35 +0000898 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
899 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
900 Value *LdExpArg = 0;
901 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
902 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000903 LdExpArg = B.CreateSExt(OpC->getOperand(0),
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000904 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000905 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
906 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000907 LdExpArg = B.CreateZExt(OpC->getOperand(0),
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000908 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000909 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000910
Chris Lattnere818f772008-05-02 18:43:35 +0000911 if (LdExpArg) {
912 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000913 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000914 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000915 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000916 Name = "ldexp";
917 else
918 Name = "ldexpl";
919
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000920 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000921 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000922 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000923
924 Module *M = Caller->getParent();
925 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000926 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000927 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000928 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
929 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
930 CI->setCallingConv(F->getCallingConv());
931
932 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000933 }
934 return 0;
935 }
936};
Chris Lattnere818f772008-05-02 18:43:35 +0000937
938//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000939// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
940
Chris Lattner3e8b6632009-09-02 06:11:42 +0000941struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000942 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000943 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000944 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
945 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000946 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000947
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000948 // If this is something like 'floor((double)floatval)', convert to floorf.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000949 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000950 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000951 return 0;
952
953 // floor((double)floatval) -> (double)floorf(floatval)
954 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000955 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
956 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000957 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000958 }
959};
960
961//===----------------------------------------------------------------------===//
962// Integer Optimizations
963//===----------------------------------------------------------------------===//
964
965//===---------------------------------------===//
966// 'ffs*' Optimizations
967
Chris Lattner3e8b6632009-09-02 06:11:42 +0000968struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000969 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000970 const FunctionType *FT = Callee->getFunctionType();
971 // Just make sure this has 2 arguments of the same FP type, which match the
972 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000973 if (FT->getNumParams() != 1 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000974 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000975 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000976 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000977
Gabor Greifaee5dc12010-06-24 10:42:46 +0000978 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000979
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000980 // Constant fold.
981 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
982 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000983 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000984 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000985 CI->getValue().countTrailingZeros()+1);
986 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000987
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000988 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
989 const Type *ArgType = Op->getType();
990 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
991 Intrinsic::cttz, &ArgType, 1);
992 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000993 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000994 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000995
Owen Andersona7235ea2009-07-31 20:28:14 +0000996 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000997 return B.CreateSelect(Cond, V,
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000998 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000999 }
1000};
1001
1002//===---------------------------------------===//
1003// 'isdigit' Optimizations
1004
Chris Lattner3e8b6632009-09-02 06:11:42 +00001005struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001006 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001007 const FunctionType *FT = Callee->getFunctionType();
1008 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +00001009 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001010 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001011 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001012
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001013 // isdigit(c) -> (c-'0') <u 10
Gabor Greifaee5dc12010-06-24 10:42:46 +00001014 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001015 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001016 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001017 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001018 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001019 return B.CreateZExt(Op, CI->getType());
1020 }
1021};
1022
1023//===---------------------------------------===//
1024// 'isascii' Optimizations
1025
Chris Lattner3e8b6632009-09-02 06:11:42 +00001026struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001027 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001028 const FunctionType *FT = Callee->getFunctionType();
1029 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +00001030 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001031 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001032 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001033
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001034 // isascii(c) -> c <u 128
Gabor Greifaee5dc12010-06-24 10:42:46 +00001035 Value *Op = CI->getArgOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +00001036 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001037 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001038 return B.CreateZExt(Op, CI->getType());
1039 }
1040};
Eric Christopher37c8b862009-10-07 21:14:25 +00001041
Chris Lattner313f0e62008-06-09 08:26:51 +00001042//===---------------------------------------===//
1043// 'abs', 'labs', 'llabs' Optimizations
1044
Chris Lattner3e8b6632009-09-02 06:11:42 +00001045struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001046 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001047 const FunctionType *FT = Callee->getFunctionType();
1048 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +00001049 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +00001050 FT->getParamType(0) != FT->getReturnType())
1051 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001052
Chris Lattner313f0e62008-06-09 08:26:51 +00001053 // abs(x) -> x >s -1 ? x : -x
Gabor Greifaee5dc12010-06-24 10:42:46 +00001054 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001055 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001056 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001057 "ispos");
1058 Value *Neg = B.CreateNeg(Op, "neg");
1059 return B.CreateSelect(Pos, Op, Neg);
1060 }
1061};
Eric Christopher37c8b862009-10-07 21:14:25 +00001062
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001063
1064//===---------------------------------------===//
1065// 'toascii' Optimizations
1066
Chris Lattner3e8b6632009-09-02 06:11:42 +00001067struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001068 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001069 const FunctionType *FT = Callee->getFunctionType();
1070 // We require i32(i32)
1071 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001072 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001073 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001074
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001075 // isascii(c) -> c & 0x7f
Gabor Greifaee5dc12010-06-24 10:42:46 +00001076 return B.CreateAnd(CI->getArgOperand(0),
Owen Andersoneed707b2009-07-24 23:12:02 +00001077 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001078 }
1079};
1080
1081//===----------------------------------------------------------------------===//
1082// Formatting and IO Optimizations
1083//===----------------------------------------------------------------------===//
1084
1085//===---------------------------------------===//
1086// 'printf' Optimizations
1087
Chris Lattner3e8b6632009-09-02 06:11:42 +00001088struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001089 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001090 // Require one fixed pointer argument and an integer/void result.
1091 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001092 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1093 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001094 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001095 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001096
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001097 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001098 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001099 if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001100 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001101
1102 // Empty format string -> noop.
1103 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001104 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001105 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001106
Chris Lattner74965f22009-11-09 04:57:04 +00001107 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1108 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001109 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001110 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +00001111 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +00001112 if (CI->use_empty()) return CI;
1113 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001114 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001115
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001116 // printf("foo\n") --> puts("foo")
1117 if (FormatStr[FormatStr.size()-1] == '\n' &&
1118 FormatStr.find('%') == std::string::npos) { // no format characters.
1119 // Create a string literal with no \n on it. We expect the constant merge
1120 // pass to be run after this pass, to merge duplicate strings.
1121 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001122 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001123 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1124 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +00001125 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001126 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001127 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001128 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001129
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001130 // Optimize specific format strings.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001131 // printf("%c", chr) --> putchar(chr)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001132 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001133 CI->getArgOperand(1)->getType()->isIntegerTy()) {
1134 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001135
Chris Lattner74965f22009-11-09 04:57:04 +00001136 if (CI->use_empty()) return CI;
1137 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001138 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001139
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001140 // printf("%s\n", str) --> puts(str)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001141 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001142 CI->getArgOperand(1)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001143 CI->use_empty()) {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001144 EmitPutS(CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001145 return CI;
1146 }
1147 return 0;
1148 }
1149};
1150
1151//===---------------------------------------===//
1152// 'sprintf' Optimizations
1153
Chris Lattner3e8b6632009-09-02 06:11:42 +00001154struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001155 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001156 // Require two fixed pointer arguments and an integer result.
1157 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001158 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1159 !FT->getParamType(1)->isPointerTy() ||
1160 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001161 return 0;
1162
1163 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001164 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001165 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001166 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001167
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001168 // If we just have a format string (nothing else crazy) transform it.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001169 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001170 // Make sure there's no % in the constant array. We could try to handle
1171 // %% -> % in the future if we cared.
1172 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1173 if (FormatStr[i] == '%')
1174 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001175
1176 // These optimizations require TargetData.
1177 if (!TD) return 0;
1178
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001179 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
Gabor Greifa3997812010-07-22 10:37:47 +00001180 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), // Copy the
1181 ConstantInt::get(TD->getIntPtrType(*Context), // nul byte.
1182 FormatStr.size() + 1), 1, false, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001183 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001184 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001185
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001186 // The remaining optimizations require the format string to be "%s" or "%c"
1187 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001188 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1189 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001190 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001191
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001192 // Decode the second character of the format string.
1193 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001194 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Gabor Greifaee5dc12010-06-24 10:42:46 +00001195 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1196 Value *V = B.CreateTrunc(CI->getArgOperand(2),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001197 Type::getInt8Ty(*Context), "char");
Gabor Greifaee5dc12010-06-24 10:42:46 +00001198 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001199 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001200 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001201 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001202 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001203
Owen Andersoneed707b2009-07-24 23:12:02 +00001204 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001205 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001206
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001207 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001208 // These optimizations require TargetData.
1209 if (!TD) return 0;
1210
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001211 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001212 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001213
Gabor Greifaee5dc12010-06-24 10:42:46 +00001214 Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001215 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001216 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001217 "leninc");
Gabor Greifa3997812010-07-22 10:37:47 +00001218 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(2),
1219 IncLen, 1, false, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001220
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001221 // The sprintf result is the unincremented number of bytes in the string.
1222 return B.CreateIntCast(Len, CI->getType(), false);
1223 }
1224 return 0;
1225 }
1226};
1227
1228//===---------------------------------------===//
1229// 'fwrite' Optimizations
1230
Chris Lattner3e8b6632009-09-02 06:11:42 +00001231struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001232 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001233 // Require a pointer, an integer, an integer, a pointer, returning integer.
1234 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001235 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1236 !FT->getParamType(1)->isIntegerTy() ||
1237 !FT->getParamType(2)->isIntegerTy() ||
1238 !FT->getParamType(3)->isPointerTy() ||
1239 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001240 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001241
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001242 // Get the element size and count.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001243 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1244 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001245 if (!SizeC || !CountC) return 0;
1246 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001247
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001248 // If this is writing zero records, remove the call (it's a noop).
1249 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001250 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001251
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001252 // If this is writing one byte, turn it into fputc.
1253 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001254 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1255 EmitFPutC(Char, CI->getArgOperand(3), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001256 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001257 }
1258
1259 return 0;
1260 }
1261};
1262
1263//===---------------------------------------===//
1264// 'fputs' Optimizations
1265
Chris Lattner3e8b6632009-09-02 06:11:42 +00001266struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001267 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001268 // These optimizations require TargetData.
1269 if (!TD) return 0;
1270
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001271 // Require two pointers. Also, we can't optimize if return value is used.
1272 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001273 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1274 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001275 !CI->use_empty())
1276 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001277
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001278 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001279 uint64_t Len = GetStringLength(CI->getArgOperand(0));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001280 if (!Len) return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001281 EmitFWrite(CI->getArgOperand(0),
Owen Anderson1d0be152009-08-13 21:58:54 +00001282 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001283 CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001284 return CI; // Known to have no uses (see above).
1285 }
1286};
1287
1288//===---------------------------------------===//
1289// 'fprintf' Optimizations
1290
Chris Lattner3e8b6632009-09-02 06:11:42 +00001291struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001292 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001293 // Require two fixed paramters as pointers and integer result.
1294 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001295 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1296 !FT->getParamType(1)->isPointerTy() ||
1297 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001298 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001299
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001300 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001301 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001302 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001303 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001304
1305 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001306 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001307 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1308 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001309 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001310
1311 // These optimizations require TargetData.
1312 if (!TD) return 0;
1313
Gabor Greifaee5dc12010-06-24 10:42:46 +00001314 EmitFWrite(CI->getArgOperand(1),
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001315 ConstantInt::get(TD->getIntPtrType(*Context),
1316 FormatStr.size()),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001317 CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001318 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001319 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001320
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001321 // The remaining optimizations require the format string to be "%s" or "%c"
1322 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001323 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1324 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001325 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001326
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001327 // Decode the second character of the format string.
1328 if (FormatStr[1] == 'c') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001329 // fprintf(F, "%c", chr) --> fputc(chr, F)
1330 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1331 EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001332 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001333 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001334
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001335 if (FormatStr[1] == 's') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001336 // fprintf(F, "%s", str) --> fputs(str, F)
1337 if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001338 return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001339 EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001340 return CI;
1341 }
1342 return 0;
1343 }
1344};
1345
Bill Wendlingac178222008-05-05 21:37:59 +00001346} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001347
1348//===----------------------------------------------------------------------===//
1349// SimplifyLibCalls Pass Implementation
1350//===----------------------------------------------------------------------===//
1351
1352namespace {
1353 /// This pass optimizes well known library functions from libc and libm.
1354 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001355 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001356 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001357 // String and Memory LibCall Optimizations
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001358 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrRChrOpt StrRChr;
1359 StrCmpOpt StrCmp; StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
Benjamin Kramer05f585e2010-09-29 23:52:12 +00001360 StrNCpyOpt StrNCpy; StrLenOpt StrLen; StrPBrkOpt StrPBrk;
Benjamin Kramer9510a252010-09-30 00:58:35 +00001361 StrToOpt StrTo; StrSpnOpt StrSpn; StrCSpnOpt StrCSpn; StrStrOpt StrStr;
Chris Lattner24604112009-12-16 09:32:05 +00001362 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001363 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001364 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001365 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001366 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1367 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001368 // Formatting and IO Optimizations
1369 SPrintFOpt SPrintF; PrintFOpt PrintF;
1370 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001371
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001372 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001373 public:
1374 static char ID; // Pass identification
Owen Anderson90c579d2010-08-06 18:33:48 +00001375 SimplifyLibCalls() : FunctionPass(ID), StrCpy(false), StrCpyChk(true) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001376 void InitOptimizations();
1377 bool runOnFunction(Function &F);
1378
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001379 void setDoesNotAccessMemory(Function &F);
1380 void setOnlyReadsMemory(Function &F);
1381 void setDoesNotThrow(Function &F);
1382 void setDoesNotCapture(Function &F, unsigned n);
1383 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001384 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001385
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001386 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001387 }
1388 };
1389 char SimplifyLibCalls::ID = 0;
1390} // end anonymous namespace.
1391
Owen Andersond13db2c2010-07-21 22:09:45 +00001392INITIALIZE_PASS(SimplifyLibCalls, "simplify-libcalls",
1393 "Simplify well-known library calls", false, false);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001394
1395// Public interface to the Simplify LibCalls pass.
1396FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001397 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001398}
1399
1400/// Optimizations - Populate the Optimizations map with all the optimizations
1401/// we know.
1402void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001403 // String and Memory LibCall Optimizations
1404 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001405 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001406 Optimizations["strchr"] = &StrChr;
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001407 Optimizations["strrchr"] = &StrRChr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001408 Optimizations["strcmp"] = &StrCmp;
1409 Optimizations["strncmp"] = &StrNCmp;
1410 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001411 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001412 Optimizations["strlen"] = &StrLen;
Benjamin Kramer05f585e2010-09-29 23:52:12 +00001413 Optimizations["strpbrk"] = &StrPBrk;
Nick Lewycky4c498412009-02-13 15:31:46 +00001414 Optimizations["strtol"] = &StrTo;
1415 Optimizations["strtod"] = &StrTo;
1416 Optimizations["strtof"] = &StrTo;
1417 Optimizations["strtoul"] = &StrTo;
1418 Optimizations["strtoll"] = &StrTo;
1419 Optimizations["strtold"] = &StrTo;
1420 Optimizations["strtoull"] = &StrTo;
Benjamin Kramer9510a252010-09-30 00:58:35 +00001421 Optimizations["strspn"] = &StrSpn;
1422 Optimizations["strcspn"] = &StrCSpn;
Chris Lattner24604112009-12-16 09:32:05 +00001423 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001424 Optimizations["memcmp"] = &MemCmp;
1425 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001426 Optimizations["memmove"] = &MemMove;
1427 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001428
Evan Cheng0289b412010-03-23 15:48:04 +00001429 // _chk variants of String and Memory LibCall Optimizations.
Evan Cheng0289b412010-03-23 15:48:04 +00001430 Optimizations["__strcpy_chk"] = &StrCpyChk;
1431
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001432 // Math Library Optimizations
1433 Optimizations["powf"] = &Pow;
1434 Optimizations["pow"] = &Pow;
1435 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001436 Optimizations["llvm.pow.f32"] = &Pow;
1437 Optimizations["llvm.pow.f64"] = &Pow;
1438 Optimizations["llvm.pow.f80"] = &Pow;
1439 Optimizations["llvm.pow.f128"] = &Pow;
1440 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001441 Optimizations["exp2l"] = &Exp2;
1442 Optimizations["exp2"] = &Exp2;
1443 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001444 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1445 Optimizations["llvm.exp2.f128"] = &Exp2;
1446 Optimizations["llvm.exp2.f80"] = &Exp2;
1447 Optimizations["llvm.exp2.f64"] = &Exp2;
1448 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001449
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001450#ifdef HAVE_FLOORF
1451 Optimizations["floor"] = &UnaryDoubleFP;
1452#endif
1453#ifdef HAVE_CEILF
1454 Optimizations["ceil"] = &UnaryDoubleFP;
1455#endif
1456#ifdef HAVE_ROUNDF
1457 Optimizations["round"] = &UnaryDoubleFP;
1458#endif
1459#ifdef HAVE_RINTF
1460 Optimizations["rint"] = &UnaryDoubleFP;
1461#endif
1462#ifdef HAVE_NEARBYINTF
1463 Optimizations["nearbyint"] = &UnaryDoubleFP;
1464#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001465
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001466 // Integer Optimizations
1467 Optimizations["ffs"] = &FFS;
1468 Optimizations["ffsl"] = &FFS;
1469 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001470 Optimizations["abs"] = &Abs;
1471 Optimizations["labs"] = &Abs;
1472 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001473 Optimizations["isdigit"] = &IsDigit;
1474 Optimizations["isascii"] = &IsAscii;
1475 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001476
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001477 // Formatting and IO Optimizations
1478 Optimizations["sprintf"] = &SPrintF;
1479 Optimizations["printf"] = &PrintF;
1480 Optimizations["fwrite"] = &FWrite;
1481 Optimizations["fputs"] = &FPuts;
1482 Optimizations["fprintf"] = &FPrintF;
1483}
1484
1485
1486/// runOnFunction - Top level algorithm.
1487///
1488bool SimplifyLibCalls::runOnFunction(Function &F) {
1489 if (Optimizations.empty())
1490 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001491
Dan Gohmanf14d9192009-08-18 00:48:13 +00001492 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001493
Owen Andersone922c022009-07-22 00:24:57 +00001494 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001495
1496 bool Changed = false;
1497 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1498 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1499 // Ignore non-calls.
1500 CallInst *CI = dyn_cast<CallInst>(I++);
1501 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001502
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001503 // Ignore indirect calls and calls to non-external functions.
1504 Function *Callee = CI->getCalledFunction();
1505 if (Callee == 0 || !Callee->isDeclaration() ||
1506 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1507 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001508
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001509 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001510 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1511 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001512
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001513 // Set the builder to the instruction after the call.
1514 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001515
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001516 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001517 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001518 if (Result == 0) continue;
1519
David Greene6a6b90e2010-01-05 01:27:21 +00001520 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1521 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001522
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001523 // Something changed!
1524 Changed = true;
1525 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001526
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001527 // Inspect the instruction after the call (which was potentially just
1528 // added) next.
1529 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001530
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001531 if (CI != Result && !CI->use_empty()) {
1532 CI->replaceAllUsesWith(Result);
1533 if (!Result->hasName())
1534 Result->takeName(CI);
1535 }
1536 CI->eraseFromParent();
1537 }
1538 }
1539 return Changed;
1540}
1541
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001542// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001543
1544void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1545 if (!F.doesNotAccessMemory()) {
1546 F.setDoesNotAccessMemory();
1547 ++NumAnnotated;
1548 Modified = true;
1549 }
1550}
1551void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1552 if (!F.onlyReadsMemory()) {
1553 F.setOnlyReadsMemory();
1554 ++NumAnnotated;
1555 Modified = true;
1556 }
1557}
1558void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1559 if (!F.doesNotThrow()) {
1560 F.setDoesNotThrow();
1561 ++NumAnnotated;
1562 Modified = true;
1563 }
1564}
1565void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1566 if (!F.doesNotCapture(n)) {
1567 F.setDoesNotCapture(n);
1568 ++NumAnnotated;
1569 Modified = true;
1570 }
1571}
1572void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1573 if (!F.doesNotAlias(n)) {
1574 F.setDoesNotAlias(n);
1575 ++NumAnnotated;
1576 Modified = true;
1577 }
1578}
1579
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001580/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001581///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001582bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001583 Modified = false;
1584 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1585 Function &F = *I;
1586 if (!F.isDeclaration())
1587 continue;
1588
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001589 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001590 continue;
1591
1592 const FunctionType *FTy = F.getFunctionType();
1593
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001594 StringRef Name = F.getName();
1595 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001596 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001597 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001598 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001599 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001600 continue;
1601 setOnlyReadsMemory(F);
1602 setDoesNotThrow(F);
1603 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001604 } else if (Name == "strchr" ||
1605 Name == "strrchr") {
1606 if (FTy->getNumParams() != 2 ||
1607 !FTy->getParamType(0)->isPointerTy() ||
1608 !FTy->getParamType(1)->isIntegerTy())
1609 continue;
1610 setOnlyReadsMemory(F);
1611 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001612 } else if (Name == "strcpy" ||
1613 Name == "stpcpy" ||
1614 Name == "strcat" ||
1615 Name == "strtol" ||
1616 Name == "strtod" ||
1617 Name == "strtof" ||
1618 Name == "strtoul" ||
1619 Name == "strtoll" ||
1620 Name == "strtold" ||
1621 Name == "strncat" ||
1622 Name == "strncpy" ||
1623 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001624 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001625 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001626 continue;
1627 setDoesNotThrow(F);
1628 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001629 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001630 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001631 !FTy->getParamType(0)->isPointerTy() ||
1632 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001633 continue;
1634 setDoesNotThrow(F);
1635 setDoesNotCapture(F, 1);
1636 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001637 } else if (Name == "strcmp" ||
1638 Name == "strspn" ||
1639 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001640 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001641 Name == "strcoll" ||
1642 Name == "strcasecmp" ||
1643 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001644 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001645 !FTy->getParamType(0)->isPointerTy() ||
1646 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001647 continue;
1648 setOnlyReadsMemory(F);
1649 setDoesNotThrow(F);
1650 setDoesNotCapture(F, 1);
1651 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001652 } else if (Name == "strstr" ||
1653 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001654 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001655 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001656 continue;
1657 setOnlyReadsMemory(F);
1658 setDoesNotThrow(F);
1659 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001660 } else if (Name == "strtok" ||
1661 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001662 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001663 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001664 continue;
1665 setDoesNotThrow(F);
1666 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001667 } else if (Name == "scanf" ||
1668 Name == "setbuf" ||
1669 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001670 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001671 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001672 continue;
1673 setDoesNotThrow(F);
1674 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001675 } else if (Name == "strdup" ||
1676 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001677 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001678 !FTy->getReturnType()->isPointerTy() ||
1679 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001680 continue;
1681 setDoesNotThrow(F);
1682 setDoesNotAlias(F, 0);
1683 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001684 } else if (Name == "stat" ||
1685 Name == "sscanf" ||
1686 Name == "sprintf" ||
1687 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001688 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001689 !FTy->getParamType(0)->isPointerTy() ||
1690 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001691 continue;
1692 setDoesNotThrow(F);
1693 setDoesNotCapture(F, 1);
1694 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001695 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001696 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001697 !FTy->getParamType(0)->isPointerTy() ||
1698 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001699 continue;
1700 setDoesNotThrow(F);
1701 setDoesNotCapture(F, 1);
1702 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001703 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001704 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001705 !FTy->getParamType(1)->isPointerTy() ||
1706 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001707 continue;
1708 setDoesNotThrow(F);
1709 setDoesNotCapture(F, 2);
1710 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001711 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001712 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001713 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001714 continue;
1715 // May throw; "system" is a valid pthread cancellation point.
1716 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001717 }
1718 break;
1719 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001720 if (Name == "malloc") {
1721 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001722 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001723 continue;
1724 setDoesNotThrow(F);
1725 setDoesNotAlias(F, 0);
1726 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001727 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001728 !FTy->getParamType(0)->isPointerTy() ||
1729 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001730 continue;
1731 setOnlyReadsMemory(F);
1732 setDoesNotThrow(F);
1733 setDoesNotCapture(F, 1);
1734 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001735 } else if (Name == "memchr" ||
1736 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001737 if (FTy->getNumParams() != 3)
1738 continue;
1739 setOnlyReadsMemory(F);
1740 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001741 } else if (Name == "modf" ||
1742 Name == "modff" ||
1743 Name == "modfl" ||
1744 Name == "memcpy" ||
1745 Name == "memccpy" ||
1746 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001747 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001748 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001749 continue;
1750 setDoesNotThrow(F);
1751 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001752 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001753 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001754 continue;
1755 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001756 } else if (Name == "mkdir" ||
1757 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001758 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001759 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001760 continue;
1761 setDoesNotThrow(F);
1762 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001763 }
1764 break;
1765 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001766 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001767 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001768 !FTy->getParamType(0)->isPointerTy() ||
1769 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001770 continue;
1771 setDoesNotThrow(F);
1772 setDoesNotAlias(F, 0);
1773 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001774 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001775 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001776 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001777 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001778 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001779 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001780 } else if (Name == "rmdir" ||
1781 Name == "rewind" ||
1782 Name == "remove" ||
1783 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001784 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001785 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001786 continue;
1787 setDoesNotThrow(F);
1788 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001789 } else if (Name == "rename" ||
1790 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001791 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001792 !FTy->getParamType(0)->isPointerTy() ||
1793 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001794 continue;
1795 setDoesNotThrow(F);
1796 setDoesNotCapture(F, 1);
1797 setDoesNotCapture(F, 2);
1798 }
1799 break;
1800 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001801 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001802 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001803 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001804 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001805 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001806 setDoesNotCapture(F, 2);
1807 }
1808 break;
1809 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001810 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001811 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001812 !FTy->getParamType(0)->isPointerTy() ||
1813 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001814 continue;
1815 setDoesNotThrow(F);
1816 setDoesNotCapture(F, 1);
1817 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001818 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001819 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001820 !FTy->getParamType(0)->isPointerTy() ||
1821 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001822 continue;
1823 setDoesNotThrow(F);
1824 setOnlyReadsMemory(F);
1825 setDoesNotCapture(F, 1);
1826 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001827 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001828 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001829 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001830 continue;
1831 setDoesNotThrow(F);
1832 setDoesNotCapture(F, 1);
1833 }
1834 break;
1835 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001836 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001837 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001838 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001839 continue;
1840 setDoesNotThrow(F);
1841 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001842 } else if (Name == "chmod" ||
1843 Name == "chown" ||
1844 Name == "ctermid" ||
1845 Name == "clearerr" ||
1846 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001847 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001848 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001849 continue;
1850 setDoesNotThrow(F);
1851 setDoesNotCapture(F, 1);
1852 }
1853 break;
1854 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001855 if (Name == "atoi" ||
1856 Name == "atol" ||
1857 Name == "atof" ||
1858 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001859 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001860 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001861 continue;
1862 setDoesNotThrow(F);
1863 setOnlyReadsMemory(F);
1864 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001865 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001866 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001867 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001868 continue;
1869 setDoesNotThrow(F);
1870 setDoesNotCapture(F, 1);
1871 }
1872 break;
1873 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001874 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001875 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001876 !FTy->getReturnType()->isPointerTy() ||
1877 !FTy->getParamType(0)->isPointerTy() ||
1878 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001879 continue;
1880 setDoesNotThrow(F);
1881 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001882 setDoesNotCapture(F, 1);
1883 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001884 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001885 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001886 !FTy->getReturnType()->isPointerTy() ||
1887 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001888 continue;
1889 setDoesNotThrow(F);
1890 setDoesNotAlias(F, 0);
1891 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001892 } else if (Name == "feof" ||
1893 Name == "free" ||
1894 Name == "fseek" ||
1895 Name == "ftell" ||
1896 Name == "fgetc" ||
1897 Name == "fseeko" ||
1898 Name == "ftello" ||
1899 Name == "fileno" ||
1900 Name == "fflush" ||
1901 Name == "fclose" ||
1902 Name == "fsetpos" ||
1903 Name == "flockfile" ||
1904 Name == "funlockfile" ||
1905 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001906 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001907 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001908 continue;
1909 setDoesNotThrow(F);
1910 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001911 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001912 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001913 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001914 continue;
1915 setDoesNotThrow(F);
1916 setDoesNotCapture(F, 1);
1917 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001918 } else if (Name == "fputc" ||
1919 Name == "fstat" ||
1920 Name == "frexp" ||
1921 Name == "frexpf" ||
1922 Name == "frexpl" ||
1923 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001924 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001925 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001926 continue;
1927 setDoesNotThrow(F);
1928 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001929 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001930 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001931 !FTy->getParamType(0)->isPointerTy() ||
1932 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001933 continue;
1934 setDoesNotThrow(F);
1935 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001936 } else if (Name == "fread" ||
1937 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001938 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001939 !FTy->getParamType(0)->isPointerTy() ||
1940 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001941 continue;
1942 setDoesNotThrow(F);
1943 setDoesNotCapture(F, 1);
1944 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001945 } else if (Name == "fputs" ||
1946 Name == "fscanf" ||
1947 Name == "fprintf" ||
1948 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001949 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001950 !FTy->getParamType(0)->isPointerTy() ||
1951 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001952 continue;
1953 setDoesNotThrow(F);
1954 setDoesNotCapture(F, 1);
1955 setDoesNotCapture(F, 2);
1956 }
1957 break;
1958 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001959 if (Name == "getc" ||
1960 Name == "getlogin_r" ||
1961 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001962 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001963 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001964 continue;
1965 setDoesNotThrow(F);
1966 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001967 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001968 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001969 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001970 continue;
1971 setDoesNotThrow(F);
1972 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001973 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001974 } else if (Name == "gets" ||
1975 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001976 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001977 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001978 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001979 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001980 continue;
1981 setDoesNotThrow(F);
1982 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001983 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001984 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001985 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001986 continue;
1987 setDoesNotThrow(F);
1988 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001989 }
1990 break;
1991 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001992 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001993 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001994 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001995 continue;
1996 setDoesNotThrow(F);
1997 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001998 } else if (Name == "uname" ||
1999 Name == "unlink" ||
2000 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002001 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002002 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002003 continue;
2004 setDoesNotThrow(F);
2005 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002006 } else if (Name == "utime" ||
2007 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002008 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002009 !FTy->getParamType(0)->isPointerTy() ||
2010 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002011 continue;
2012 setDoesNotThrow(F);
2013 setDoesNotCapture(F, 1);
2014 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002015 }
2016 break;
2017 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002018 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002019 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002020 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002021 continue;
2022 setDoesNotThrow(F);
2023 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002024 } else if (Name == "puts" ||
2025 Name == "printf" ||
2026 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002027 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002028 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002029 continue;
2030 setDoesNotThrow(F);
2031 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002032 } else if (Name == "pread" ||
2033 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002034 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002035 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002036 continue;
2037 // May throw; these are valid pthread cancellation points.
2038 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002039 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002040 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002041 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002042 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002043 !FTy->getReturnType()->isPointerTy() ||
2044 !FTy->getParamType(0)->isPointerTy() ||
2045 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002046 continue;
2047 setDoesNotThrow(F);
2048 setDoesNotAlias(F, 0);
2049 setDoesNotCapture(F, 1);
2050 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002051 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002052 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002053 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002054 continue;
2055 setDoesNotThrow(F);
2056 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002057 }
2058 break;
2059 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002060 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002061 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002062 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002063 continue;
2064 setDoesNotThrow(F);
2065 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002066 } else if (Name == "vsscanf" ||
2067 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002068 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002069 !FTy->getParamType(1)->isPointerTy() ||
2070 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002071 continue;
2072 setDoesNotThrow(F);
2073 setDoesNotCapture(F, 1);
2074 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002075 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00002076 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002077 continue;
2078 setDoesNotThrow(F);
2079 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002080 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002081 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002082 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002083 continue;
2084 setDoesNotThrow(F);
2085 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002086 } else if (Name == "vfprintf" ||
2087 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002088 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002089 !FTy->getParamType(0)->isPointerTy() ||
2090 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002091 continue;
2092 setDoesNotThrow(F);
2093 setDoesNotCapture(F, 1);
2094 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002095 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002096 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002097 !FTy->getParamType(0)->isPointerTy() ||
2098 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002099 continue;
2100 setDoesNotThrow(F);
2101 setDoesNotCapture(F, 1);
2102 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002103 }
2104 break;
2105 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002106 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002107 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002108 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002109 continue;
2110 // May throw; "open" is a valid pthread cancellation point.
2111 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002112 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002113 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002114 !FTy->getReturnType()->isPointerTy() ||
2115 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002116 continue;
2117 setDoesNotThrow(F);
2118 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002119 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002120 }
2121 break;
2122 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002123 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00002124 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002125 continue;
2126 setDoesNotThrow(F);
2127 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002128 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002129 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002130 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002131 continue;
2132 setDoesNotThrow(F);
2133 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002134 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002135 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002136 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002137 if (Name == "htonl" ||
2138 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002139 setDoesNotThrow(F);
2140 setDoesNotAccessMemory(F);
2141 }
2142 break;
2143 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002144 if (Name == "ntohl" ||
2145 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002146 setDoesNotThrow(F);
2147 setDoesNotAccessMemory(F);
2148 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002149 break;
2150 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002151 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002152 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002153 !FTy->getParamType(0)->isPointerTy() ||
2154 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002155 continue;
2156 setDoesNotThrow(F);
2157 setDoesNotCapture(F, 1);
2158 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002159 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002160 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002161 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002162 continue;
2163 setDoesNotThrow(F);
2164 setDoesNotCapture(F, 1);
2165 }
2166 break;
2167 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002168 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002169 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002170 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002171 continue;
2172 // May throw; places call through function pointer.
2173 setDoesNotCapture(F, 4);
2174 }
2175 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002176 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002177 if (Name == "__strdup" ||
2178 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002179 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002180 !FTy->getReturnType()->isPointerTy() ||
2181 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002182 continue;
2183 setDoesNotThrow(F);
2184 setDoesNotAlias(F, 0);
2185 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002186 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002187 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002188 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002189 continue;
2190 setDoesNotThrow(F);
2191 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002192 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002193 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002194 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002195 continue;
2196 setDoesNotThrow(F);
2197 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002198 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002199 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002200 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002201 continue;
2202 setDoesNotThrow(F);
2203 setDoesNotCapture(F, 2);
2204 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002205 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002206 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002207 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002208 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002209 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002210 continue;
2211 setDoesNotThrow(F);
2212 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002213 } else if (Name == "\1stat64" ||
2214 Name == "\1lstat64" ||
2215 Name == "\1statvfs64" ||
2216 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002217 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002218 !FTy->getParamType(0)->isPointerTy() ||
2219 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002220 continue;
2221 setDoesNotThrow(F);
2222 setDoesNotCapture(F, 1);
2223 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002224 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002225 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002226 !FTy->getReturnType()->isPointerTy() ||
2227 !FTy->getParamType(0)->isPointerTy() ||
2228 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002229 continue;
2230 setDoesNotThrow(F);
2231 setDoesNotAlias(F, 0);
2232 setDoesNotCapture(F, 1);
2233 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002234 } else if (Name == "\1fseeko64" ||
2235 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002236 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002237 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002238 continue;
2239 setDoesNotThrow(F);
2240 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002241 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002242 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002243 continue;
2244 setDoesNotThrow(F);
2245 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002246 } else if (Name == "\1fstat64" ||
2247 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002248 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002249 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002250 continue;
2251 setDoesNotThrow(F);
2252 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002253 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002254 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002255 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002256 continue;
2257 // May throw; "open" is a valid pthread cancellation point.
2258 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002259 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002260 break;
2261 }
2262 }
2263 return Modified;
2264}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002265
2266// TODO:
2267// Additional cases that we need to add to this file:
2268//
2269// cbrt:
2270// * cbrt(expN(X)) -> expN(x/3)
2271// * cbrt(sqrt(x)) -> pow(x,1/6)
2272// * cbrt(sqrt(x)) -> pow(x,1/9)
2273//
2274// cos, cosf, cosl:
2275// * cos(-x) -> cos(x)
2276//
2277// exp, expf, expl:
2278// * exp(log(x)) -> x
2279//
2280// log, logf, logl:
2281// * log(exp(x)) -> x
2282// * log(x**y) -> y*log(x)
2283// * log(exp(y)) -> y*log(e)
2284// * log(exp2(y)) -> y*log(2)
2285// * log(exp10(y)) -> y*log(10)
2286// * log(sqrt(x)) -> 0.5*log(x)
2287// * log(pow(x,y)) -> y*log(x)
2288//
2289// lround, lroundf, lroundl:
2290// * lround(cnst) -> cnst'
2291//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002292// pow, powf, powl:
2293// * pow(exp(x),y) -> exp(x*y)
2294// * pow(sqrt(x),y) -> pow(x,y*0.5)
2295// * pow(pow(x,y),z)-> pow(x,y*z)
2296//
2297// puts:
Dan Gohman2511bd02010-08-04 01:16:35 +00002298// * puts("") -> putchar('\n')
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002299//
2300// round, roundf, roundl:
2301// * round(cnst) -> cnst'
2302//
2303// signbit:
2304// * signbit(cnst) -> cnst'
2305// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2306//
2307// sqrt, sqrtf, sqrtl:
2308// * sqrt(expN(x)) -> expN(x*0.5)
2309// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2310// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2311//
2312// stpcpy:
2313// * stpcpy(str, "literal") ->
2314// llvm.memcpy(str,"literal",strlen("literal")+1,1)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002315//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002316// tan, tanf, tanl:
2317// * tan(atan(x)) -> x
2318//
2319// trunc, truncf, truncl:
2320// * trunc(cnst) -> cnst'
2321//
2322//