blob: 3edc447cec172ce947116ae90c73a45abc8c0c84 [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) ||
Benjamin Kramer4c756792010-09-30 11:21:59 +0000226 FT->getParamType(0) != FT->getReturnType() ||
227 !FT->getParamType(1)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000228 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000229
Gabor Greifaee5dc12010-06-24 10:42:46 +0000230 Value *SrcStr = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000231
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000232 // If the second operand is non-constant, see if we can compute the length
233 // of the input string and turn this into memchr.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000234 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000235 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000236 // These optimizations require TargetData.
237 if (!TD) return 0;
238
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000239 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000240 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000241 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000242
Gabor Greifaee5dc12010-06-24 10:42:46 +0000243 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000244 ConstantInt::get(TD->getIntPtrType(*Context), Len),
245 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000246 }
247
248 // Otherwise, the character is a constant, see if the first argument is
249 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000250 std::string Str;
251 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000252 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000253
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000254 // strchr can find the nul character.
255 Str += '\0';
Eric Christopher37c8b862009-10-07 21:14:25 +0000256
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000257 // Compute the offset.
Benjamin Kramere2609902010-09-29 22:29:12 +0000258 size_t I = Str.find(CharC->getSExtValue());
259 if (I == std::string::npos) // Didn't find the char. strchr returns null.
260 return Constant::getNullValue(CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000261
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000262 // strchr(s+n,c) -> gep(s+n+i,c)
Benjamin Kramere2609902010-09-29 22:29:12 +0000263 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000264 return B.CreateGEP(SrcStr, Idx, "strchr");
265 }
266};
267
268//===---------------------------------------===//
Benjamin Kramer06f25cf2010-09-29 21:50:51 +0000269// 'strrchr' Optimizations
270
271struct StrRChrOpt : public LibCallOptimization {
272 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
273 // Verify the "strrchr" function prototype.
274 const FunctionType *FT = Callee->getFunctionType();
275 if (FT->getNumParams() != 2 ||
276 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Benjamin Kramer4c756792010-09-30 11:21:59 +0000277 FT->getParamType(0) != FT->getReturnType() ||
278 !FT->getParamType(1)->isIntegerTy(32))
Benjamin Kramer06f25cf2010-09-29 21:50:51 +0000279 return 0;
280
281 Value *SrcStr = CI->getArgOperand(0);
282 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
283
284 // Cannot fold anything if we're not looking for a constant.
285 if (!CharC)
286 return 0;
287
288 std::string Str;
289 if (!GetConstantStringInfo(SrcStr, Str)) {
290 // strrchr(s, 0) -> strchr(s, 0)
291 if (TD && CharC->isZero())
292 return EmitStrChr(SrcStr, '\0', B, TD);
293 return 0;
294 }
295
296 // strrchr can find the nul character.
297 Str += '\0';
298
299 // Compute the offset.
300 size_t I = Str.rfind(CharC->getSExtValue());
301 if (I == std::string::npos) // Didn't find the char. Return null.
302 return Constant::getNullValue(CI->getType());
303
304 // strrchr(s+n,c) -> gep(s+n+i,c)
305 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
306 return B.CreateGEP(SrcStr, Idx, "strrchr");
307 }
308};
309
310//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000311// 'strcmp' Optimizations
312
Chris Lattner3e8b6632009-09-02 06:11:42 +0000313struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000314 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000315 // Verify the "strcmp" function prototype.
316 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000317 if (FT->getNumParams() != 2 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000318 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000319 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000320 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000321 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000322
Gabor Greifaee5dc12010-06-24 10:42:46 +0000323 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000324 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000325 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000326
Bill Wendling0582ae92009-03-13 04:39:26 +0000327 std::string Str1, Str2;
328 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
329 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000330
Bill Wendling0582ae92009-03-13 04:39:26 +0000331 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000332 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000333
Bill Wendling0582ae92009-03-13 04:39:26 +0000334 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000335 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000336
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000337 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000338 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000339 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000340 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000341
342 // strcmp(P, "x") -> memcmp(P, "x", 2)
343 uint64_t Len1 = GetStringLength(Str1P);
344 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000345 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000346 // These optimizations require TargetData.
347 if (!TD) return 0;
348
Nick Lewycky13a09e22008-12-21 00:19:21 +0000349 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000350 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000351 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000352 }
353
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000354 return 0;
355 }
356};
357
358//===---------------------------------------===//
359// 'strncmp' Optimizations
360
Chris Lattner3e8b6632009-09-02 06:11:42 +0000361struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000362 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000363 // Verify the "strncmp" function prototype.
364 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000365 if (FT->getNumParams() != 3 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000366 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000367 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000368 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000369 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000370 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000371
Gabor Greifaee5dc12010-06-24 10:42:46 +0000372 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000373 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000374 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000375
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000376 // Get the length argument if it is constant.
377 uint64_t Length;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000378 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000379 Length = LengthArg->getZExtValue();
380 else
381 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000382
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000383 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000384 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000385
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000386 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000387 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD);
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000388
Bill Wendling0582ae92009-03-13 04:39:26 +0000389 std::string Str1, Str2;
390 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
391 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000392
Bill Wendling0582ae92009-03-13 04:39:26 +0000393 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000394 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000395
Bill Wendling0582ae92009-03-13 04:39:26 +0000396 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000397 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000398
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000399 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000400 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000401 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000402 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000403 return 0;
404 }
405};
406
407
408//===---------------------------------------===//
409// 'strcpy' Optimizations
410
Chris Lattner3e8b6632009-09-02 06:11:42 +0000411struct StrCpyOpt : public LibCallOptimization {
Evan Chengeb8c6452010-03-24 20:19:04 +0000412 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
413
414 StrCpyOpt(bool c) : OptChkCall(c) {}
415
Eric Christopher7a61d702008-08-08 19:39:37 +0000416 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000417 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000418 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000419 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000420 if (FT->getNumParams() != NumParams ||
421 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000422 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000423 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000424 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000425
Gabor Greifaee5dc12010-06-24 10:42:46 +0000426 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000427 if (Dst == Src) // strcpy(x,x) -> x
428 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000429
Dan Gohmanf14d9192009-08-18 00:48:13 +0000430 // These optimizations require TargetData.
431 if (!TD) return 0;
432
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000433 // See if we can get the length of the input string.
434 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000435 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000436
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000437 // We have enough information to now generate the memcpy call to do the
438 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000439 if (OptChkCall)
440 EmitMemCpyChk(Dst, Src,
441 ConstantInt::get(TD->getIntPtrType(*Context), Len),
Gabor Greifaee5dc12010-06-24 10:42:46 +0000442 CI->getArgOperand(2), B, TD);
Evan Cheng0289b412010-03-23 15:48:04 +0000443 else
444 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000445 ConstantInt::get(TD->getIntPtrType(*Context), Len),
446 1, false, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000447 return Dst;
448 }
449};
450
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000451//===---------------------------------------===//
452// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000453
Chris Lattner3e8b6632009-09-02 06:11:42 +0000454struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000455 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
456 const FunctionType *FT = Callee->getFunctionType();
457 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
458 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000459 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000460 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000461 return 0;
462
Gabor Greifaee5dc12010-06-24 10:42:46 +0000463 Value *Dst = CI->getArgOperand(0);
464 Value *Src = CI->getArgOperand(1);
465 Value *LenOp = CI->getArgOperand(2);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000466
467 // See if we can get the length of the input string.
468 uint64_t SrcLen = GetStringLength(Src);
469 if (SrcLen == 0) return 0;
470 --SrcLen;
471
472 if (SrcLen == 0) {
473 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Mon P Wang20adc9d2010-04-04 03:10:48 +0000474 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'),
475 LenOp, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000476 return Dst;
477 }
478
479 uint64_t Len;
480 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
481 Len = LengthArg->getZExtValue();
482 else
483 return 0;
484
485 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
486
Dan Gohmanf14d9192009-08-18 00:48:13 +0000487 // These optimizations require TargetData.
488 if (!TD) return 0;
489
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000490 // Let strncpy handle the zero padding
491 if (Len > SrcLen+1) return 0;
492
493 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000494 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000495 ConstantInt::get(TD->getIntPtrType(*Context), Len),
496 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000497
498 return Dst;
499 }
500};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000501
502//===---------------------------------------===//
503// 'strlen' Optimizations
504
Chris Lattner3e8b6632009-09-02 06:11:42 +0000505struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000506 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000507 const FunctionType *FT = Callee->getFunctionType();
508 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000509 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000510 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000511 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000512
Gabor Greifaee5dc12010-06-24 10:42:46 +0000513 Value *Src = CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000514
515 // Constant folding: strlen("xyz") -> 3
516 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000517 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000518
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000519 // strlen(x) != 0 --> *x != 0
520 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000521 if (IsOnlyUsedInZeroEqualityComparison(CI))
522 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
523 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000524 }
525};
526
Benjamin Kramer05f585e2010-09-29 23:52:12 +0000527
528//===---------------------------------------===//
529// 'strpbrk' Optimizations
530
531struct StrPBrkOpt : public LibCallOptimization {
532 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
533 const FunctionType *FT = Callee->getFunctionType();
534 if (FT->getNumParams() != 2 ||
535 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
536 FT->getParamType(1) != FT->getParamType(0) ||
537 FT->getReturnType() != FT->getParamType(0))
538 return 0;
539
540 std::string S1, S2;
541 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
542 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
543
544 // strpbrk(s, "") -> NULL
545 // strpbrk("", s) -> NULL
546 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
547 return Constant::getNullValue(CI->getType());
548
549 // Constant folding.
550 if (HasS1 && HasS2) {
551 size_t I = S1.find_first_of(S2);
552 if (I == std::string::npos) // No match.
553 return Constant::getNullValue(CI->getType());
554
555 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
556 return B.CreateGEP(CI->getArgOperand(0), Idx, "strpbrk");
557 }
558
559 // strpbrk(s, "a") -> strchr(s, 'a')
560 if (TD && HasS2 && S2.size() == 1)
561 return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD);
562
563 return 0;
564 }
565};
566
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000567//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000568// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000569
Chris Lattner3e8b6632009-09-02 06:11:42 +0000570struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000571 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
572 const FunctionType *FT = Callee->getFunctionType();
573 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000574 !FT->getParamType(0)->isPointerTy() ||
575 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000576 return 0;
577
Gabor Greifaee5dc12010-06-24 10:42:46 +0000578 Value *EndPtr = CI->getArgOperand(1);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000579 if (isa<ConstantPointerNull>(EndPtr)) {
Dan Gohmanc32046e2010-12-17 01:09:43 +0000580 // With a null EndPtr, this function won't capture the main argument.
581 // It would be readonly too, except that it still may write to errno.
Nick Lewycky4c498412009-02-13 15:31:46 +0000582 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000583 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000584
585 return 0;
586 }
587};
588
Chris Lattner24604112009-12-16 09:32:05 +0000589//===---------------------------------------===//
Benjamin Kramer9510a252010-09-30 00:58:35 +0000590// 'strspn' Optimizations
591
592struct StrSpnOpt : public LibCallOptimization {
593 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
594 const FunctionType *FT = Callee->getFunctionType();
595 if (FT->getNumParams() != 2 ||
596 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
597 FT->getParamType(1) != FT->getParamType(0) ||
598 !FT->getReturnType()->isIntegerTy())
599 return 0;
600
601 std::string S1, S2;
602 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
603 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
604
605 // strspn(s, "") -> 0
606 // strspn("", s) -> 0
607 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
608 return Constant::getNullValue(CI->getType());
609
610 // Constant folding.
611 if (HasS1 && HasS2)
612 return ConstantInt::get(CI->getType(), strspn(S1.c_str(), S2.c_str()));
613
614 return 0;
615 }
616};
617
618//===---------------------------------------===//
619// 'strcspn' Optimizations
620
621struct StrCSpnOpt : public LibCallOptimization {
622 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
623 const FunctionType *FT = Callee->getFunctionType();
624 if (FT->getNumParams() != 2 ||
625 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
626 FT->getParamType(1) != FT->getParamType(0) ||
627 !FT->getReturnType()->isIntegerTy())
628 return 0;
629
630 std::string S1, S2;
631 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
632 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
633
634 // strcspn("", s) -> 0
635 if (HasS1 && S1.empty())
636 return Constant::getNullValue(CI->getType());
637
638 // Constant folding.
639 if (HasS1 && HasS2)
640 return ConstantInt::get(CI->getType(), strcspn(S1.c_str(), S2.c_str()));
641
642 // strcspn(s, "") -> strlen(s)
643 if (TD && HasS2 && S2.empty())
644 return EmitStrLen(CI->getArgOperand(0), B, TD);
645
646 return 0;
647 }
648};
649
650//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000651// 'strstr' Optimizations
652
653struct StrStrOpt : public LibCallOptimization {
654 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
655 const FunctionType *FT = Callee->getFunctionType();
656 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000657 !FT->getParamType(0)->isPointerTy() ||
658 !FT->getParamType(1)->isPointerTy() ||
659 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000660 return 0;
661
662 // fold strstr(x, x) -> x.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000663 if (CI->getArgOperand(0) == CI->getArgOperand(1))
664 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000665
Benjamin Kramer386e9182010-06-15 21:34:25 +0000666 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000667 if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
668 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD);
669 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000670 StrLen, B, TD);
671 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
672 UI != UE; ) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000673 ICmpInst *Old = cast<ICmpInst>(*UI++);
Benjamin Kramer386e9182010-06-15 21:34:25 +0000674 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
675 ConstantInt::getNullValue(StrNCmp->getType()),
676 "cmp");
677 Old->replaceAllUsesWith(Cmp);
678 Old->eraseFromParent();
679 }
680 return CI;
681 }
682
Chris Lattner24604112009-12-16 09:32:05 +0000683 // See if either input string is a constant string.
684 std::string SearchStr, ToFindStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000685 bool HasStr1 = GetConstantStringInfo(CI->getArgOperand(0), SearchStr);
686 bool HasStr2 = GetConstantStringInfo(CI->getArgOperand(1), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000687
Chris Lattner24604112009-12-16 09:32:05 +0000688 // fold strstr(x, "") -> x.
689 if (HasStr2 && ToFindStr.empty())
Gabor Greifaee5dc12010-06-24 10:42:46 +0000690 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000691
Chris Lattner24604112009-12-16 09:32:05 +0000692 // If both strings are known, constant fold it.
693 if (HasStr1 && HasStr2) {
694 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000695
Chris Lattner24604112009-12-16 09:32:05 +0000696 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
697 return Constant::getNullValue(CI->getType());
698
699 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000700 Value *Result = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner24604112009-12-16 09:32:05 +0000701 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
702 return B.CreateBitCast(Result, CI->getType());
703 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000704
Chris Lattner24604112009-12-16 09:32:05 +0000705 // fold strstr(x, "y") -> strchr(x, 'y').
706 if (HasStr2 && ToFindStr.size() == 1)
Gabor Greifa3997812010-07-22 10:37:47 +0000707 return B.CreateBitCast(EmitStrChr(CI->getArgOperand(0),
708 ToFindStr[0], B, TD), CI->getType());
Chris Lattner24604112009-12-16 09:32:05 +0000709 return 0;
710 }
711};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000712
Nick Lewycky4c498412009-02-13 15:31:46 +0000713
714//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000715// 'memcmp' Optimizations
716
Chris Lattner3e8b6632009-09-02 06:11:42 +0000717struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000718 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000719 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000720 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
721 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000722 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000723 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000724
Gabor Greifaee5dc12010-06-24 10:42:46 +0000725 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000726
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000727 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000728 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000729
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000730 // Make sure we have a constant length.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000731 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000732 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000733 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000734
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000735 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000736 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000737
Benjamin Kramer48aefe12010-05-25 22:53:43 +0000738 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
739 if (Len == 1) {
740 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
741 CI->getType(), "lhsv");
742 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
743 CI->getType(), "rhsv");
Benjamin Kramer1464c1d2010-05-26 09:45:04 +0000744 return B.CreateSub(LHSV, RHSV, "chardiff");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000745 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000746
Benjamin Kramer992a6372009-11-05 17:44:22 +0000747 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
748 std::string LHSStr, RHSStr;
749 if (GetConstantStringInfo(LHS, LHSStr) &&
750 GetConstantStringInfo(RHS, RHSStr)) {
751 // Make sure we're not reading out-of-bounds memory.
752 if (Len > LHSStr.length() || Len > RHSStr.length())
753 return 0;
754 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
755 return ConstantInt::get(CI->getType(), Ret);
756 }
757
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000758 return 0;
759 }
760};
761
762//===---------------------------------------===//
763// 'memcpy' Optimizations
764
Chris Lattner3e8b6632009-09-02 06:11:42 +0000765struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000766 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000767 // These optimizations require TargetData.
768 if (!TD) return 0;
769
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000770 const FunctionType *FT = Callee->getFunctionType();
771 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000772 !FT->getParamType(0)->isPointerTy() ||
773 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000774 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000775 return 0;
776
777 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000778 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
779 CI->getArgOperand(2), 1, false, B, TD);
780 return CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000781 }
782};
783
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000784//===---------------------------------------===//
785// 'memmove' Optimizations
786
Chris Lattner3e8b6632009-09-02 06:11:42 +0000787struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000788 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000789 // These optimizations require TargetData.
790 if (!TD) return 0;
791
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000792 const FunctionType *FT = Callee->getFunctionType();
793 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000794 !FT->getParamType(0)->isPointerTy() ||
795 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000796 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000797 return 0;
798
799 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000800 EmitMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
801 CI->getArgOperand(2), 1, false, B, TD);
802 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000803 }
804};
805
806//===---------------------------------------===//
807// 'memset' Optimizations
808
Chris Lattner3e8b6632009-09-02 06:11:42 +0000809struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000810 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000811 // These optimizations require TargetData.
812 if (!TD) return 0;
813
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000814 const FunctionType *FT = Callee->getFunctionType();
815 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000816 !FT->getParamType(0)->isPointerTy() ||
817 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000818 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000819 return 0;
820
821 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Gabor Greifa3997812010-07-22 10:37:47 +0000822 Value *Val = B.CreateIntCast(CI->getArgOperand(1),
823 Type::getInt8Ty(*Context), false);
Gabor Greifaee5dc12010-06-24 10:42:46 +0000824 EmitMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), false, B, TD);
825 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000826 }
827};
828
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000829//===----------------------------------------------------------------------===//
830// Math Library Optimizations
831//===----------------------------------------------------------------------===//
832
833//===---------------------------------------===//
834// 'pow*' Optimizations
835
Chris Lattner3e8b6632009-09-02 06:11:42 +0000836struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000837 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000838 const FunctionType *FT = Callee->getFunctionType();
839 // Just make sure this has 2 arguments of the same FP type, which match the
840 // result type.
841 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
842 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000843 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000844 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000845
Gabor Greifaee5dc12010-06-24 10:42:46 +0000846 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000847 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
848 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
849 return Op1C;
850 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000851 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000852 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000853
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000854 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
855 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000856
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000857 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000858 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000859
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000860 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000861 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
862 // This is faster than calling pow, and still handles negative zero
863 // and negative infinite correctly.
864 // TODO: In fast-math mode, this could be just sqrt(x).
865 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000866 Value *Inf = ConstantFP::getInfinity(CI->getType());
867 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000868 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
869 Callee->getAttributes());
870 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
871 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000872 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
873 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
874 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000875 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000876
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000877 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
878 return Op1;
879 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000880 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000881 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000882 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000883 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000884 return 0;
885 }
886};
887
888//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000889// 'exp2' Optimizations
890
Chris Lattner3e8b6632009-09-02 06:11:42 +0000891struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000892 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000893 const FunctionType *FT = Callee->getFunctionType();
894 // Just make sure this has 1 argument of FP type, which matches the
895 // result type.
896 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000897 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000898 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000899
Gabor Greifaee5dc12010-06-24 10:42:46 +0000900 Value *Op = CI->getArgOperand(0);
Chris Lattnere818f772008-05-02 18:43:35 +0000901 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
902 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
903 Value *LdExpArg = 0;
904 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
905 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000906 LdExpArg = B.CreateSExt(OpC->getOperand(0),
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000907 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000908 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
909 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000910 LdExpArg = B.CreateZExt(OpC->getOperand(0),
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000911 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000912 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000913
Chris Lattnere818f772008-05-02 18:43:35 +0000914 if (LdExpArg) {
915 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000916 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000917 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000918 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000919 Name = "ldexp";
920 else
921 Name = "ldexpl";
922
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000923 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000924 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000925 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000926
927 Module *M = Caller->getParent();
928 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000929 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000930 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000931 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
932 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
933 CI->setCallingConv(F->getCallingConv());
934
935 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000936 }
937 return 0;
938 }
939};
Chris Lattnere818f772008-05-02 18:43:35 +0000940
941//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000942// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
943
Chris Lattner3e8b6632009-09-02 06:11:42 +0000944struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000945 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000946 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000947 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
948 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000949 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000950
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000951 // If this is something like 'floor((double)floatval)', convert to floorf.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000952 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000953 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000954 return 0;
955
956 // floor((double)floatval) -> (double)floorf(floatval)
957 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000958 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
959 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000960 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000961 }
962};
963
964//===----------------------------------------------------------------------===//
965// Integer Optimizations
966//===----------------------------------------------------------------------===//
967
968//===---------------------------------------===//
969// 'ffs*' Optimizations
970
Chris Lattner3e8b6632009-09-02 06:11:42 +0000971struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000972 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000973 const FunctionType *FT = Callee->getFunctionType();
974 // Just make sure this has 2 arguments of the same FP type, which match the
975 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000976 if (FT->getNumParams() != 1 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000977 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000978 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000979 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000980
Gabor Greifaee5dc12010-06-24 10:42:46 +0000981 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000982
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000983 // Constant fold.
984 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
985 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000986 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000987 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000988 CI->getValue().countTrailingZeros()+1);
989 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000990
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000991 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
992 const Type *ArgType = Op->getType();
993 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
994 Intrinsic::cttz, &ArgType, 1);
995 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000996 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000997 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000998
Owen Andersona7235ea2009-07-31 20:28:14 +0000999 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001000 return B.CreateSelect(Cond, V,
Nick Lewycky10d2f4d2010-07-06 03:53:43 +00001001 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001002 }
1003};
1004
1005//===---------------------------------------===//
1006// 'isdigit' Optimizations
1007
Chris Lattner3e8b6632009-09-02 06:11:42 +00001008struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001009 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001010 const FunctionType *FT = Callee->getFunctionType();
1011 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +00001012 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001013 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001014 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001015
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001016 // isdigit(c) -> (c-'0') <u 10
Gabor Greifaee5dc12010-06-24 10:42:46 +00001017 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001018 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001019 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +00001020 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001021 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001022 return B.CreateZExt(Op, CI->getType());
1023 }
1024};
1025
1026//===---------------------------------------===//
1027// 'isascii' Optimizations
1028
Chris Lattner3e8b6632009-09-02 06:11:42 +00001029struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001030 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001031 const FunctionType *FT = Callee->getFunctionType();
1032 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +00001033 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001034 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001035 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001036
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001037 // isascii(c) -> c <u 128
Gabor Greifaee5dc12010-06-24 10:42:46 +00001038 Value *Op = CI->getArgOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +00001039 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001040 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001041 return B.CreateZExt(Op, CI->getType());
1042 }
1043};
Eric Christopher37c8b862009-10-07 21:14:25 +00001044
Chris Lattner313f0e62008-06-09 08:26:51 +00001045//===---------------------------------------===//
1046// 'abs', 'labs', 'llabs' Optimizations
1047
Chris Lattner3e8b6632009-09-02 06:11:42 +00001048struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001049 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001050 const FunctionType *FT = Callee->getFunctionType();
1051 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +00001052 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +00001053 FT->getParamType(0) != FT->getReturnType())
1054 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001055
Chris Lattner313f0e62008-06-09 08:26:51 +00001056 // abs(x) -> x >s -1 ? x : -x
Gabor Greifaee5dc12010-06-24 10:42:46 +00001057 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001058 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +00001059 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001060 "ispos");
1061 Value *Neg = B.CreateNeg(Op, "neg");
1062 return B.CreateSelect(Pos, Op, Neg);
1063 }
1064};
Eric Christopher37c8b862009-10-07 21:14:25 +00001065
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001066
1067//===---------------------------------------===//
1068// 'toascii' Optimizations
1069
Chris Lattner3e8b6632009-09-02 06:11:42 +00001070struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001071 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001072 const FunctionType *FT = Callee->getFunctionType();
1073 // We require i32(i32)
1074 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001075 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001076 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001077
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001078 // isascii(c) -> c & 0x7f
Gabor Greifaee5dc12010-06-24 10:42:46 +00001079 return B.CreateAnd(CI->getArgOperand(0),
Owen Andersoneed707b2009-07-24 23:12:02 +00001080 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001081 }
1082};
1083
1084//===----------------------------------------------------------------------===//
1085// Formatting and IO Optimizations
1086//===----------------------------------------------------------------------===//
1087
1088//===---------------------------------------===//
1089// 'printf' Optimizations
1090
Chris Lattner3e8b6632009-09-02 06:11:42 +00001091struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001092 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001093 // Require one fixed pointer argument and an integer/void result.
1094 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001095 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1096 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001097 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001098 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001099
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001100 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001101 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001102 if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001103 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001104
1105 // Empty format string -> noop.
1106 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001107 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001108 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001109
Chris Lattner74965f22009-11-09 04:57:04 +00001110 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1111 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001112 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001113 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +00001114 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +00001115 if (CI->use_empty()) return CI;
1116 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001117 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001118
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001119 // printf("foo\n") --> puts("foo")
1120 if (FormatStr[FormatStr.size()-1] == '\n' &&
1121 FormatStr.find('%') == std::string::npos) { // no format characters.
1122 // Create a string literal with no \n on it. We expect the constant merge
1123 // pass to be run after this pass, to merge duplicate strings.
1124 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001125 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001126 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1127 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +00001128 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001129 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001130 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001131 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001132
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001133 // Optimize specific format strings.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001134 // printf("%c", chr) --> putchar(chr)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001135 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001136 CI->getArgOperand(1)->getType()->isIntegerTy()) {
1137 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001138
Chris Lattner74965f22009-11-09 04:57:04 +00001139 if (CI->use_empty()) return CI;
1140 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001141 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001142
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001143 // printf("%s\n", str) --> puts(str)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001144 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001145 CI->getArgOperand(1)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001146 CI->use_empty()) {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001147 EmitPutS(CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001148 return CI;
1149 }
1150 return 0;
1151 }
1152};
1153
1154//===---------------------------------------===//
1155// 'sprintf' Optimizations
1156
Chris Lattner3e8b6632009-09-02 06:11:42 +00001157struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001158 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001159 // Require two fixed pointer arguments and an integer result.
1160 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001161 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1162 !FT->getParamType(1)->isPointerTy() ||
1163 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001164 return 0;
1165
1166 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001167 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001168 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001169 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001170
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001171 // If we just have a format string (nothing else crazy) transform it.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001172 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001173 // Make sure there's no % in the constant array. We could try to handle
1174 // %% -> % in the future if we cared.
1175 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1176 if (FormatStr[i] == '%')
1177 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001178
1179 // These optimizations require TargetData.
1180 if (!TD) return 0;
1181
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001182 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
Gabor Greifa3997812010-07-22 10:37:47 +00001183 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), // Copy the
1184 ConstantInt::get(TD->getIntPtrType(*Context), // nul byte.
1185 FormatStr.size() + 1), 1, false, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001186 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001187 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001188
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001189 // The remaining optimizations require the format string to be "%s" or "%c"
1190 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001191 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1192 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001193 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001194
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001195 // Decode the second character of the format string.
1196 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001197 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Gabor Greifaee5dc12010-06-24 10:42:46 +00001198 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1199 Value *V = B.CreateTrunc(CI->getArgOperand(2),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001200 Type::getInt8Ty(*Context), "char");
Gabor Greifaee5dc12010-06-24 10:42:46 +00001201 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001202 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001203 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001204 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001205 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001206
Owen Andersoneed707b2009-07-24 23:12:02 +00001207 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001208 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001209
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001210 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001211 // These optimizations require TargetData.
1212 if (!TD) return 0;
1213
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001214 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001215 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001216
Gabor Greifaee5dc12010-06-24 10:42:46 +00001217 Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001218 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001219 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001220 "leninc");
Gabor Greifa3997812010-07-22 10:37:47 +00001221 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(2),
1222 IncLen, 1, false, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001223
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001224 // The sprintf result is the unincremented number of bytes in the string.
1225 return B.CreateIntCast(Len, CI->getType(), false);
1226 }
1227 return 0;
1228 }
1229};
1230
1231//===---------------------------------------===//
1232// 'fwrite' Optimizations
1233
Chris Lattner3e8b6632009-09-02 06:11:42 +00001234struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001235 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001236 // Require a pointer, an integer, an integer, a pointer, returning integer.
1237 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001238 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1239 !FT->getParamType(1)->isIntegerTy() ||
1240 !FT->getParamType(2)->isIntegerTy() ||
1241 !FT->getParamType(3)->isPointerTy() ||
1242 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001243 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001244
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001245 // Get the element size and count.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001246 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1247 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001248 if (!SizeC || !CountC) return 0;
1249 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001250
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001251 // If this is writing zero records, remove the call (it's a noop).
1252 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001253 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001254
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001255 // If this is writing one byte, turn it into fputc.
1256 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001257 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1258 EmitFPutC(Char, CI->getArgOperand(3), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001259 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001260 }
1261
1262 return 0;
1263 }
1264};
1265
1266//===---------------------------------------===//
1267// 'fputs' Optimizations
1268
Chris Lattner3e8b6632009-09-02 06:11:42 +00001269struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001270 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001271 // These optimizations require TargetData.
1272 if (!TD) return 0;
1273
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001274 // Require two pointers. Also, we can't optimize if return value is used.
1275 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001276 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1277 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001278 !CI->use_empty())
1279 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001280
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001281 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001282 uint64_t Len = GetStringLength(CI->getArgOperand(0));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001283 if (!Len) return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001284 EmitFWrite(CI->getArgOperand(0),
Owen Anderson1d0be152009-08-13 21:58:54 +00001285 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001286 CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001287 return CI; // Known to have no uses (see above).
1288 }
1289};
1290
1291//===---------------------------------------===//
1292// 'fprintf' Optimizations
1293
Chris Lattner3e8b6632009-09-02 06:11:42 +00001294struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001295 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001296 // Require two fixed paramters as pointers and integer result.
1297 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001298 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1299 !FT->getParamType(1)->isPointerTy() ||
1300 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001301 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001302
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001303 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001304 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001305 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001306 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001307
1308 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001309 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001310 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1311 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001312 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001313
1314 // These optimizations require TargetData.
1315 if (!TD) return 0;
1316
Gabor Greifaee5dc12010-06-24 10:42:46 +00001317 EmitFWrite(CI->getArgOperand(1),
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001318 ConstantInt::get(TD->getIntPtrType(*Context),
1319 FormatStr.size()),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001320 CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001321 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001322 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001323
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001324 // The remaining optimizations require the format string to be "%s" or "%c"
1325 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001326 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1327 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001328 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001329
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001330 // Decode the second character of the format string.
1331 if (FormatStr[1] == 'c') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001332 // fprintf(F, "%c", chr) --> fputc(chr, F)
1333 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1334 EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001335 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001336 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001337
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001338 if (FormatStr[1] == 's') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001339 // fprintf(F, "%s", str) --> fputs(str, F)
1340 if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001341 return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001342 EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001343 return CI;
1344 }
1345 return 0;
1346 }
1347};
1348
Anders Carlsson303023d2010-11-30 06:19:18 +00001349//===---------------------------------------===//
1350// 'puts' Optimizations
1351
1352struct PutsOpt : public LibCallOptimization {
1353 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1354 // Require one fixed pointer argument and an integer/void result.
1355 const FunctionType *FT = Callee->getFunctionType();
1356 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1357 !(FT->getReturnType()->isIntegerTy() ||
1358 FT->getReturnType()->isVoidTy()))
1359 return 0;
1360
1361 // Check for a constant string.
1362 std::string Str;
1363 if (!GetConstantStringInfo(CI->getArgOperand(0), Str))
1364 return 0;
1365
1366 if (Str.empty()) {
1367 // puts("") -> putchar('\n')
1368 Value *Res = EmitPutChar(B.getInt32('\n'), B, TD);
1369 if (CI->use_empty()) return CI;
1370 return B.CreateIntCast(Res, CI->getType(), true);
1371 }
1372
1373 return 0;
1374 }
1375};
1376
Bill Wendlingac178222008-05-05 21:37:59 +00001377} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001378
1379//===----------------------------------------------------------------------===//
1380// SimplifyLibCalls Pass Implementation
1381//===----------------------------------------------------------------------===//
1382
1383namespace {
1384 /// This pass optimizes well known library functions from libc and libm.
1385 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001386 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001387 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001388 // String and Memory LibCall Optimizations
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001389 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrRChrOpt StrRChr;
1390 StrCmpOpt StrCmp; StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
Benjamin Kramer05f585e2010-09-29 23:52:12 +00001391 StrNCpyOpt StrNCpy; StrLenOpt StrLen; StrPBrkOpt StrPBrk;
Benjamin Kramer9510a252010-09-30 00:58:35 +00001392 StrToOpt StrTo; StrSpnOpt StrSpn; StrCSpnOpt StrCSpn; StrStrOpt StrStr;
Chris Lattner24604112009-12-16 09:32:05 +00001393 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001394 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001395 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001396 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001397 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1398 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001399 // Formatting and IO Optimizations
1400 SPrintFOpt SPrintF; PrintFOpt PrintF;
1401 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Anders Carlsson303023d2010-11-30 06:19:18 +00001402 PutsOpt Puts;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001403
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001404 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001405 public:
1406 static char ID; // Pass identification
Owen Anderson081c34b2010-10-19 17:21:58 +00001407 SimplifyLibCalls() : FunctionPass(ID), StrCpy(false), StrCpyChk(true) {
1408 initializeSimplifyLibCallsPass(*PassRegistry::getPassRegistry());
1409 }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001410 void InitOptimizations();
1411 bool runOnFunction(Function &F);
1412
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001413 void setDoesNotAccessMemory(Function &F);
1414 void setOnlyReadsMemory(Function &F);
1415 void setDoesNotThrow(Function &F);
1416 void setDoesNotCapture(Function &F, unsigned n);
1417 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001418 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001419
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001420 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001421 }
1422 };
1423 char SimplifyLibCalls::ID = 0;
1424} // end anonymous namespace.
1425
Owen Andersond13db2c2010-07-21 22:09:45 +00001426INITIALIZE_PASS(SimplifyLibCalls, "simplify-libcalls",
Owen Andersonce665bd2010-10-07 22:25:06 +00001427 "Simplify well-known library calls", false, false)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001428
1429// Public interface to the Simplify LibCalls pass.
1430FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001431 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001432}
1433
1434/// Optimizations - Populate the Optimizations map with all the optimizations
1435/// we know.
1436void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001437 // String and Memory LibCall Optimizations
1438 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001439 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001440 Optimizations["strchr"] = &StrChr;
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001441 Optimizations["strrchr"] = &StrRChr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001442 Optimizations["strcmp"] = &StrCmp;
1443 Optimizations["strncmp"] = &StrNCmp;
1444 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001445 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001446 Optimizations["strlen"] = &StrLen;
Benjamin Kramer05f585e2010-09-29 23:52:12 +00001447 Optimizations["strpbrk"] = &StrPBrk;
Nick Lewycky4c498412009-02-13 15:31:46 +00001448 Optimizations["strtol"] = &StrTo;
1449 Optimizations["strtod"] = &StrTo;
1450 Optimizations["strtof"] = &StrTo;
1451 Optimizations["strtoul"] = &StrTo;
1452 Optimizations["strtoll"] = &StrTo;
1453 Optimizations["strtold"] = &StrTo;
1454 Optimizations["strtoull"] = &StrTo;
Benjamin Kramer9510a252010-09-30 00:58:35 +00001455 Optimizations["strspn"] = &StrSpn;
1456 Optimizations["strcspn"] = &StrCSpn;
Chris Lattner24604112009-12-16 09:32:05 +00001457 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001458 Optimizations["memcmp"] = &MemCmp;
1459 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001460 Optimizations["memmove"] = &MemMove;
1461 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001462
Evan Cheng0289b412010-03-23 15:48:04 +00001463 // _chk variants of String and Memory LibCall Optimizations.
Evan Cheng0289b412010-03-23 15:48:04 +00001464 Optimizations["__strcpy_chk"] = &StrCpyChk;
1465
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001466 // Math Library Optimizations
1467 Optimizations["powf"] = &Pow;
1468 Optimizations["pow"] = &Pow;
1469 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001470 Optimizations["llvm.pow.f32"] = &Pow;
1471 Optimizations["llvm.pow.f64"] = &Pow;
1472 Optimizations["llvm.pow.f80"] = &Pow;
1473 Optimizations["llvm.pow.f128"] = &Pow;
1474 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001475 Optimizations["exp2l"] = &Exp2;
1476 Optimizations["exp2"] = &Exp2;
1477 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001478 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1479 Optimizations["llvm.exp2.f128"] = &Exp2;
1480 Optimizations["llvm.exp2.f80"] = &Exp2;
1481 Optimizations["llvm.exp2.f64"] = &Exp2;
1482 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001483
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001484#ifdef HAVE_FLOORF
1485 Optimizations["floor"] = &UnaryDoubleFP;
1486#endif
1487#ifdef HAVE_CEILF
1488 Optimizations["ceil"] = &UnaryDoubleFP;
1489#endif
1490#ifdef HAVE_ROUNDF
1491 Optimizations["round"] = &UnaryDoubleFP;
1492#endif
1493#ifdef HAVE_RINTF
1494 Optimizations["rint"] = &UnaryDoubleFP;
1495#endif
1496#ifdef HAVE_NEARBYINTF
1497 Optimizations["nearbyint"] = &UnaryDoubleFP;
1498#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001499
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001500 // Integer Optimizations
1501 Optimizations["ffs"] = &FFS;
1502 Optimizations["ffsl"] = &FFS;
1503 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001504 Optimizations["abs"] = &Abs;
1505 Optimizations["labs"] = &Abs;
1506 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001507 Optimizations["isdigit"] = &IsDigit;
1508 Optimizations["isascii"] = &IsAscii;
1509 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001510
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001511 // Formatting and IO Optimizations
1512 Optimizations["sprintf"] = &SPrintF;
1513 Optimizations["printf"] = &PrintF;
1514 Optimizations["fwrite"] = &FWrite;
1515 Optimizations["fputs"] = &FPuts;
1516 Optimizations["fprintf"] = &FPrintF;
Anders Carlsson303023d2010-11-30 06:19:18 +00001517 Optimizations["puts"] = &Puts;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001518}
1519
1520
1521/// runOnFunction - Top level algorithm.
1522///
1523bool SimplifyLibCalls::runOnFunction(Function &F) {
1524 if (Optimizations.empty())
1525 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001526
Dan Gohmanf14d9192009-08-18 00:48:13 +00001527 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001528
Owen Andersone922c022009-07-22 00:24:57 +00001529 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001530
1531 bool Changed = false;
1532 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1533 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1534 // Ignore non-calls.
1535 CallInst *CI = dyn_cast<CallInst>(I++);
1536 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001537
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001538 // Ignore indirect calls and calls to non-external functions.
1539 Function *Callee = CI->getCalledFunction();
1540 if (Callee == 0 || !Callee->isDeclaration() ||
1541 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1542 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001543
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001544 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001545 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1546 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001547
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001548 // Set the builder to the instruction after the call.
1549 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001550
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001551 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001552 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001553 if (Result == 0) continue;
1554
David Greene6a6b90e2010-01-05 01:27:21 +00001555 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1556 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001557
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001558 // Something changed!
1559 Changed = true;
1560 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001561
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001562 // Inspect the instruction after the call (which was potentially just
1563 // added) next.
1564 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001565
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001566 if (CI != Result && !CI->use_empty()) {
1567 CI->replaceAllUsesWith(Result);
1568 if (!Result->hasName())
1569 Result->takeName(CI);
1570 }
1571 CI->eraseFromParent();
1572 }
1573 }
1574 return Changed;
1575}
1576
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001577// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001578
1579void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1580 if (!F.doesNotAccessMemory()) {
1581 F.setDoesNotAccessMemory();
1582 ++NumAnnotated;
1583 Modified = true;
1584 }
1585}
1586void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1587 if (!F.onlyReadsMemory()) {
1588 F.setOnlyReadsMemory();
1589 ++NumAnnotated;
1590 Modified = true;
1591 }
1592}
1593void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1594 if (!F.doesNotThrow()) {
1595 F.setDoesNotThrow();
1596 ++NumAnnotated;
1597 Modified = true;
1598 }
1599}
1600void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1601 if (!F.doesNotCapture(n)) {
1602 F.setDoesNotCapture(n);
1603 ++NumAnnotated;
1604 Modified = true;
1605 }
1606}
1607void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1608 if (!F.doesNotAlias(n)) {
1609 F.setDoesNotAlias(n);
1610 ++NumAnnotated;
1611 Modified = true;
1612 }
1613}
1614
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001615/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001616///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001617bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001618 Modified = false;
1619 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1620 Function &F = *I;
1621 if (!F.isDeclaration())
1622 continue;
1623
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001624 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001625 continue;
1626
1627 const FunctionType *FTy = F.getFunctionType();
1628
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001629 StringRef Name = F.getName();
1630 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001631 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001632 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001633 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001634 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001635 continue;
1636 setOnlyReadsMemory(F);
1637 setDoesNotThrow(F);
1638 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001639 } else if (Name == "strchr" ||
1640 Name == "strrchr") {
1641 if (FTy->getNumParams() != 2 ||
1642 !FTy->getParamType(0)->isPointerTy() ||
1643 !FTy->getParamType(1)->isIntegerTy())
1644 continue;
1645 setOnlyReadsMemory(F);
1646 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001647 } else if (Name == "strcpy" ||
1648 Name == "stpcpy" ||
1649 Name == "strcat" ||
1650 Name == "strtol" ||
1651 Name == "strtod" ||
1652 Name == "strtof" ||
1653 Name == "strtoul" ||
1654 Name == "strtoll" ||
1655 Name == "strtold" ||
1656 Name == "strncat" ||
1657 Name == "strncpy" ||
1658 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001659 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001660 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001661 continue;
1662 setDoesNotThrow(F);
1663 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001664 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001665 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001666 !FTy->getParamType(0)->isPointerTy() ||
1667 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001668 continue;
1669 setDoesNotThrow(F);
1670 setDoesNotCapture(F, 1);
1671 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001672 } else if (Name == "strcmp" ||
1673 Name == "strspn" ||
1674 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001675 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001676 Name == "strcoll" ||
1677 Name == "strcasecmp" ||
1678 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001679 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001680 !FTy->getParamType(0)->isPointerTy() ||
1681 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001682 continue;
1683 setOnlyReadsMemory(F);
1684 setDoesNotThrow(F);
1685 setDoesNotCapture(F, 1);
1686 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001687 } else if (Name == "strstr" ||
1688 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001689 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001690 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001691 continue;
1692 setOnlyReadsMemory(F);
1693 setDoesNotThrow(F);
1694 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001695 } else if (Name == "strtok" ||
1696 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001697 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001698 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001699 continue;
1700 setDoesNotThrow(F);
1701 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001702 } else if (Name == "scanf" ||
1703 Name == "setbuf" ||
1704 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001705 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001706 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001707 continue;
1708 setDoesNotThrow(F);
1709 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001710 } else if (Name == "strdup" ||
1711 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001712 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001713 !FTy->getReturnType()->isPointerTy() ||
1714 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001715 continue;
1716 setDoesNotThrow(F);
1717 setDoesNotAlias(F, 0);
1718 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001719 } else if (Name == "stat" ||
1720 Name == "sscanf" ||
1721 Name == "sprintf" ||
1722 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001723 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001724 !FTy->getParamType(0)->isPointerTy() ||
1725 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001726 continue;
1727 setDoesNotThrow(F);
1728 setDoesNotCapture(F, 1);
1729 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001730 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001731 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001732 !FTy->getParamType(0)->isPointerTy() ||
1733 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001734 continue;
1735 setDoesNotThrow(F);
1736 setDoesNotCapture(F, 1);
1737 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001738 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001739 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001740 !FTy->getParamType(1)->isPointerTy() ||
1741 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001742 continue;
1743 setDoesNotThrow(F);
1744 setDoesNotCapture(F, 2);
1745 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001746 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001747 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001748 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001749 continue;
1750 // May throw; "system" is a valid pthread cancellation point.
1751 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001752 }
1753 break;
1754 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001755 if (Name == "malloc") {
1756 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001757 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001758 continue;
1759 setDoesNotThrow(F);
1760 setDoesNotAlias(F, 0);
1761 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001762 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001763 !FTy->getParamType(0)->isPointerTy() ||
1764 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001765 continue;
1766 setOnlyReadsMemory(F);
1767 setDoesNotThrow(F);
1768 setDoesNotCapture(F, 1);
1769 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001770 } else if (Name == "memchr" ||
1771 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001772 if (FTy->getNumParams() != 3)
1773 continue;
1774 setOnlyReadsMemory(F);
1775 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001776 } else if (Name == "modf" ||
1777 Name == "modff" ||
1778 Name == "modfl" ||
1779 Name == "memcpy" ||
1780 Name == "memccpy" ||
1781 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001782 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001783 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001784 continue;
1785 setDoesNotThrow(F);
1786 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001787 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001788 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001789 continue;
1790 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001791 } else if (Name == "mkdir" ||
1792 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001793 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001794 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001795 continue;
1796 setDoesNotThrow(F);
1797 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001798 }
1799 break;
1800 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001801 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001802 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001803 !FTy->getParamType(0)->isPointerTy() ||
1804 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001805 continue;
1806 setDoesNotThrow(F);
1807 setDoesNotAlias(F, 0);
1808 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001809 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001810 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001811 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001812 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001813 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001814 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001815 } else if (Name == "rmdir" ||
1816 Name == "rewind" ||
1817 Name == "remove" ||
1818 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001819 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001820 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001821 continue;
1822 setDoesNotThrow(F);
1823 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001824 } else if (Name == "rename" ||
1825 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001826 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001827 !FTy->getParamType(0)->isPointerTy() ||
1828 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001829 continue;
1830 setDoesNotThrow(F);
1831 setDoesNotCapture(F, 1);
1832 setDoesNotCapture(F, 2);
1833 }
1834 break;
1835 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001836 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001837 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001838 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001839 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001840 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001841 setDoesNotCapture(F, 2);
1842 }
1843 break;
1844 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001845 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001846 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001847 !FTy->getParamType(0)->isPointerTy() ||
1848 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001849 continue;
1850 setDoesNotThrow(F);
1851 setDoesNotCapture(F, 1);
1852 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001853 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001854 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001855 !FTy->getParamType(0)->isPointerTy() ||
1856 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001857 continue;
1858 setDoesNotThrow(F);
1859 setOnlyReadsMemory(F);
1860 setDoesNotCapture(F, 1);
1861 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001862 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001863 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001864 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001865 continue;
1866 setDoesNotThrow(F);
1867 setDoesNotCapture(F, 1);
1868 }
1869 break;
1870 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001871 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001872 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001873 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001874 continue;
1875 setDoesNotThrow(F);
1876 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001877 } else if (Name == "chmod" ||
1878 Name == "chown" ||
1879 Name == "ctermid" ||
1880 Name == "clearerr" ||
1881 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001882 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001883 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001884 continue;
1885 setDoesNotThrow(F);
1886 setDoesNotCapture(F, 1);
1887 }
1888 break;
1889 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001890 if (Name == "atoi" ||
1891 Name == "atol" ||
1892 Name == "atof" ||
1893 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001894 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001895 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001896 continue;
1897 setDoesNotThrow(F);
1898 setOnlyReadsMemory(F);
1899 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001900 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001901 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001902 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001903 continue;
1904 setDoesNotThrow(F);
1905 setDoesNotCapture(F, 1);
1906 }
1907 break;
1908 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001909 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001910 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001911 !FTy->getReturnType()->isPointerTy() ||
1912 !FTy->getParamType(0)->isPointerTy() ||
1913 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001914 continue;
1915 setDoesNotThrow(F);
1916 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001917 setDoesNotCapture(F, 1);
1918 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001919 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001920 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001921 !FTy->getReturnType()->isPointerTy() ||
1922 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001923 continue;
1924 setDoesNotThrow(F);
1925 setDoesNotAlias(F, 0);
1926 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001927 } else if (Name == "feof" ||
1928 Name == "free" ||
1929 Name == "fseek" ||
1930 Name == "ftell" ||
1931 Name == "fgetc" ||
1932 Name == "fseeko" ||
1933 Name == "ftello" ||
1934 Name == "fileno" ||
1935 Name == "fflush" ||
1936 Name == "fclose" ||
1937 Name == "fsetpos" ||
1938 Name == "flockfile" ||
1939 Name == "funlockfile" ||
1940 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001941 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001942 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001943 continue;
1944 setDoesNotThrow(F);
1945 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001946 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001947 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001948 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001949 continue;
1950 setDoesNotThrow(F);
1951 setDoesNotCapture(F, 1);
1952 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001953 } else if (Name == "fputc" ||
1954 Name == "fstat" ||
1955 Name == "frexp" ||
1956 Name == "frexpf" ||
1957 Name == "frexpl" ||
1958 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001959 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001960 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001961 continue;
1962 setDoesNotThrow(F);
1963 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001964 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001965 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001966 !FTy->getParamType(0)->isPointerTy() ||
1967 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001968 continue;
1969 setDoesNotThrow(F);
1970 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001971 } else if (Name == "fread" ||
1972 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001973 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001974 !FTy->getParamType(0)->isPointerTy() ||
1975 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001976 continue;
1977 setDoesNotThrow(F);
1978 setDoesNotCapture(F, 1);
1979 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001980 } else if (Name == "fputs" ||
1981 Name == "fscanf" ||
1982 Name == "fprintf" ||
1983 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001984 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001985 !FTy->getParamType(0)->isPointerTy() ||
1986 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001987 continue;
1988 setDoesNotThrow(F);
1989 setDoesNotCapture(F, 1);
1990 setDoesNotCapture(F, 2);
1991 }
1992 break;
1993 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001994 if (Name == "getc" ||
1995 Name == "getlogin_r" ||
1996 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001997 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001998 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001999 continue;
2000 setDoesNotThrow(F);
2001 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002002 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002003 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002004 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002005 continue;
2006 setDoesNotThrow(F);
2007 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002008 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002009 } else if (Name == "gets" ||
2010 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002011 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002012 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002013 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002014 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002015 continue;
2016 setDoesNotThrow(F);
2017 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002018 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002019 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002020 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002021 continue;
2022 setDoesNotThrow(F);
2023 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002024 }
2025 break;
2026 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002027 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002028 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002029 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002030 continue;
2031 setDoesNotThrow(F);
2032 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002033 } else if (Name == "uname" ||
2034 Name == "unlink" ||
2035 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002036 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002037 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002038 continue;
2039 setDoesNotThrow(F);
2040 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002041 } else if (Name == "utime" ||
2042 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002043 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002044 !FTy->getParamType(0)->isPointerTy() ||
2045 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002046 continue;
2047 setDoesNotThrow(F);
2048 setDoesNotCapture(F, 1);
2049 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002050 }
2051 break;
2052 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002053 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002054 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002055 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002056 continue;
2057 setDoesNotThrow(F);
2058 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002059 } else if (Name == "puts" ||
2060 Name == "printf" ||
2061 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002062 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002063 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002064 continue;
2065 setDoesNotThrow(F);
2066 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002067 } else if (Name == "pread" ||
2068 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002069 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002070 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002071 continue;
2072 // May throw; these are valid pthread cancellation points.
2073 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002074 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002075 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002076 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002077 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002078 !FTy->getReturnType()->isPointerTy() ||
2079 !FTy->getParamType(0)->isPointerTy() ||
2080 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002081 continue;
2082 setDoesNotThrow(F);
2083 setDoesNotAlias(F, 0);
2084 setDoesNotCapture(F, 1);
2085 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002086 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002087 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002088 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002089 continue;
2090 setDoesNotThrow(F);
2091 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002092 }
2093 break;
2094 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002095 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002096 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002097 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002098 continue;
2099 setDoesNotThrow(F);
2100 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002101 } else if (Name == "vsscanf" ||
2102 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002103 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002104 !FTy->getParamType(1)->isPointerTy() ||
2105 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002106 continue;
2107 setDoesNotThrow(F);
2108 setDoesNotCapture(F, 1);
2109 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002110 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00002111 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002112 continue;
2113 setDoesNotThrow(F);
2114 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002115 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002116 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002117 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002118 continue;
2119 setDoesNotThrow(F);
2120 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002121 } else if (Name == "vfprintf" ||
2122 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002123 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002124 !FTy->getParamType(0)->isPointerTy() ||
2125 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002126 continue;
2127 setDoesNotThrow(F);
2128 setDoesNotCapture(F, 1);
2129 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002130 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002131 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002132 !FTy->getParamType(0)->isPointerTy() ||
2133 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002134 continue;
2135 setDoesNotThrow(F);
2136 setDoesNotCapture(F, 1);
2137 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002138 }
2139 break;
2140 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002141 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002142 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002143 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002144 continue;
2145 // May throw; "open" is a valid pthread cancellation point.
2146 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002147 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002148 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002149 !FTy->getReturnType()->isPointerTy() ||
2150 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002151 continue;
2152 setDoesNotThrow(F);
2153 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002154 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002155 }
2156 break;
2157 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002158 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00002159 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002160 continue;
2161 setDoesNotThrow(F);
2162 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002163 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002164 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002165 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002166 continue;
2167 setDoesNotThrow(F);
2168 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002169 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002170 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002171 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002172 if (Name == "htonl" ||
2173 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002174 setDoesNotThrow(F);
2175 setDoesNotAccessMemory(F);
2176 }
2177 break;
2178 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002179 if (Name == "ntohl" ||
2180 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002181 setDoesNotThrow(F);
2182 setDoesNotAccessMemory(F);
2183 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002184 break;
2185 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002186 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002187 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002188 !FTy->getParamType(0)->isPointerTy() ||
2189 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002190 continue;
2191 setDoesNotThrow(F);
2192 setDoesNotCapture(F, 1);
2193 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002194 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002195 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002196 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002197 continue;
2198 setDoesNotThrow(F);
2199 setDoesNotCapture(F, 1);
2200 }
2201 break;
2202 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002203 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002204 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002205 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002206 continue;
2207 // May throw; places call through function pointer.
2208 setDoesNotCapture(F, 4);
2209 }
2210 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002211 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002212 if (Name == "__strdup" ||
2213 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002214 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002215 !FTy->getReturnType()->isPointerTy() ||
2216 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002217 continue;
2218 setDoesNotThrow(F);
2219 setDoesNotAlias(F, 0);
2220 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002221 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002222 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002223 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002224 continue;
2225 setDoesNotThrow(F);
2226 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002227 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002228 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002229 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002230 continue;
2231 setDoesNotThrow(F);
2232 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002233 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002234 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002235 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002236 continue;
2237 setDoesNotThrow(F);
2238 setDoesNotCapture(F, 2);
2239 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002240 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002241 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002242 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002243 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002244 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002245 continue;
2246 setDoesNotThrow(F);
2247 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002248 } else if (Name == "\1stat64" ||
2249 Name == "\1lstat64" ||
2250 Name == "\1statvfs64" ||
2251 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002252 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002253 !FTy->getParamType(0)->isPointerTy() ||
2254 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002255 continue;
2256 setDoesNotThrow(F);
2257 setDoesNotCapture(F, 1);
2258 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002259 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002260 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002261 !FTy->getReturnType()->isPointerTy() ||
2262 !FTy->getParamType(0)->isPointerTy() ||
2263 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002264 continue;
2265 setDoesNotThrow(F);
2266 setDoesNotAlias(F, 0);
2267 setDoesNotCapture(F, 1);
2268 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002269 } else if (Name == "\1fseeko64" ||
2270 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002271 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002272 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002273 continue;
2274 setDoesNotThrow(F);
2275 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002276 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002277 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002278 continue;
2279 setDoesNotThrow(F);
2280 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002281 } else if (Name == "\1fstat64" ||
2282 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002283 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002284 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002285 continue;
2286 setDoesNotThrow(F);
2287 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002288 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002289 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002290 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002291 continue;
2292 // May throw; "open" is a valid pthread cancellation point.
2293 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002294 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002295 break;
2296 }
2297 }
2298 return Modified;
2299}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002300
2301// TODO:
2302// Additional cases that we need to add to this file:
2303//
2304// cbrt:
2305// * cbrt(expN(X)) -> expN(x/3)
2306// * cbrt(sqrt(x)) -> pow(x,1/6)
2307// * cbrt(sqrt(x)) -> pow(x,1/9)
2308//
2309// cos, cosf, cosl:
2310// * cos(-x) -> cos(x)
2311//
2312// exp, expf, expl:
2313// * exp(log(x)) -> x
2314//
2315// log, logf, logl:
2316// * log(exp(x)) -> x
2317// * log(x**y) -> y*log(x)
2318// * log(exp(y)) -> y*log(e)
2319// * log(exp2(y)) -> y*log(2)
2320// * log(exp10(y)) -> y*log(10)
2321// * log(sqrt(x)) -> 0.5*log(x)
2322// * log(pow(x,y)) -> y*log(x)
2323//
2324// lround, lroundf, lroundl:
2325// * lround(cnst) -> cnst'
2326//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002327// pow, powf, powl:
2328// * pow(exp(x),y) -> exp(x*y)
2329// * pow(sqrt(x),y) -> pow(x,y*0.5)
2330// * pow(pow(x,y),z)-> pow(x,y*z)
2331//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002332// round, roundf, roundl:
2333// * round(cnst) -> cnst'
2334//
2335// signbit:
2336// * signbit(cnst) -> cnst'
2337// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2338//
2339// sqrt, sqrtf, sqrtl:
2340// * sqrt(expN(x)) -> expN(x*0.5)
2341// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2342// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2343//
2344// stpcpy:
2345// * stpcpy(str, "literal") ->
2346// llvm.memcpy(str,"literal",strlen("literal")+1,1)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002347//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002348// tan, tanf, tanl:
2349// * tan(atan(x)) -> x
2350//
2351// trunc, truncf, truncl:
2352// * trunc(cnst) -> cnst'
2353//
2354//