blob: c8c00987cc371dccea8031ce073022ec5acd2981 [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 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000126 FT->getReturnType() != B.getInt8PtrTy() ||
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.
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000163 B.CreateMemCpy(CpyDst, Src,
164 ConstantInt::get(TD->getIntPtrType(*Context), Len + 1), 1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000165 }
166};
167
168//===---------------------------------------===//
169// 'strncat' Optimizations
170
Chris Lattner3e8b6632009-09-02 06:11:42 +0000171struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000172 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
173 // Verify the "strncat" function prototype.
174 const FunctionType *FT = Callee->getFunctionType();
175 if (FT->getNumParams() != 3 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000176 FT->getReturnType() != B.getInt8PtrTy() ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000177 FT->getParamType(0) != FT->getReturnType() ||
178 FT->getParamType(1) != FT->getReturnType() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000179 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000180 return 0;
181
182 // Extract some information from the instruction
Gabor Greifaee5dc12010-06-24 10:42:46 +0000183 Value *Dst = CI->getArgOperand(0);
184 Value *Src = CI->getArgOperand(1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000185 uint64_t Len;
186
187 // We don't do anything if length is not constant
Gabor Greifaee5dc12010-06-24 10:42:46 +0000188 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000189 Len = LengthArg->getZExtValue();
190 else
191 return 0;
192
193 // See if we can get the length of the input string.
194 uint64_t SrcLen = GetStringLength(Src);
195 if (SrcLen == 0) return 0;
196 --SrcLen; // Unbias length.
197
198 // Handle the simple, do-nothing cases:
199 // strncat(x, "", c) -> x
200 // strncat(x, c, 0) -> x
201 if (SrcLen == 0 || Len == 0) return Dst;
202
Dan Gohmanf14d9192009-08-18 00:48:13 +0000203 // These optimizations require TargetData.
204 if (!TD) return 0;
205
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000206 // We don't optimize this case
207 if (Len < SrcLen) return 0;
208
209 // strncat(x, s, c) -> strcat(x, s)
210 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000211 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000212 return Dst;
213 }
214};
215
216//===---------------------------------------===//
217// 'strchr' Optimizations
218
Chris Lattner3e8b6632009-09-02 06:11:42 +0000219struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000220 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000221 // Verify the "strchr" function prototype.
222 const FunctionType *FT = Callee->getFunctionType();
223 if (FT->getNumParams() != 2 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000224 FT->getReturnType() != B.getInt8PtrTy() ||
Benjamin Kramer4c756792010-09-30 11:21:59 +0000225 FT->getParamType(0) != FT->getReturnType() ||
226 !FT->getParamType(1)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000227 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000228
Gabor Greifaee5dc12010-06-24 10:42:46 +0000229 Value *SrcStr = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000230
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000231 // If the second operand is non-constant, see if we can compute the length
232 // of the input string and turn this into memchr.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000233 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000234 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000235 // These optimizations require TargetData.
236 if (!TD) return 0;
237
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000238 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000239 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000240 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000241
Gabor Greifaee5dc12010-06-24 10:42:46 +0000242 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000243 ConstantInt::get(TD->getIntPtrType(*Context), Len),
244 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000245 }
246
247 // Otherwise, the character is a constant, see if the first argument is
248 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000249 std::string Str;
250 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000251 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000252
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000253 // strchr can find the nul character.
254 Str += '\0';
Eric Christopher37c8b862009-10-07 21:14:25 +0000255
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000256 // Compute the offset.
Benjamin Kramere2609902010-09-29 22:29:12 +0000257 size_t I = Str.find(CharC->getSExtValue());
258 if (I == std::string::npos) // Didn't find the char. strchr returns null.
259 return Constant::getNullValue(CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000260
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000261 // strchr(s+n,c) -> gep(s+n+i,c)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000262 return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000263 }
264};
265
266//===---------------------------------------===//
Benjamin Kramer06f25cf2010-09-29 21:50:51 +0000267// 'strrchr' Optimizations
268
269struct StrRChrOpt : public LibCallOptimization {
270 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
271 // Verify the "strrchr" function prototype.
272 const FunctionType *FT = Callee->getFunctionType();
273 if (FT->getNumParams() != 2 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000274 FT->getReturnType() != B.getInt8PtrTy() ||
Benjamin Kramer4c756792010-09-30 11:21:59 +0000275 FT->getParamType(0) != FT->getReturnType() ||
276 !FT->getParamType(1)->isIntegerTy(32))
Benjamin Kramer06f25cf2010-09-29 21:50:51 +0000277 return 0;
278
279 Value *SrcStr = CI->getArgOperand(0);
280 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
281
282 // Cannot fold anything if we're not looking for a constant.
283 if (!CharC)
284 return 0;
285
286 std::string Str;
287 if (!GetConstantStringInfo(SrcStr, Str)) {
288 // strrchr(s, 0) -> strchr(s, 0)
289 if (TD && CharC->isZero())
290 return EmitStrChr(SrcStr, '\0', B, TD);
291 return 0;
292 }
293
294 // strrchr can find the nul character.
295 Str += '\0';
296
297 // Compute the offset.
298 size_t I = Str.rfind(CharC->getSExtValue());
299 if (I == std::string::npos) // Didn't find the char. Return null.
300 return Constant::getNullValue(CI->getType());
301
302 // strrchr(s+n,c) -> gep(s+n+i,c)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000303 return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");
Benjamin Kramer06f25cf2010-09-29 21:50:51 +0000304 }
305};
306
307//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000308// 'strcmp' Optimizations
309
Chris Lattner3e8b6632009-09-02 06:11:42 +0000310struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000311 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000312 // Verify the "strcmp" function prototype.
313 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000314 if (FT->getNumParams() != 2 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000315 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000316 FT->getParamType(0) != FT->getParamType(1) ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000317 FT->getParamType(0) != B.getInt8PtrTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000318 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000319
Gabor Greifaee5dc12010-06-24 10:42:46 +0000320 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000321 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000322 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000323
Bill Wendling0582ae92009-03-13 04:39:26 +0000324 std::string Str1, Str2;
325 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
326 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000327
Bill Wendling0582ae92009-03-13 04:39:26 +0000328 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000329 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000330
Bill Wendling0582ae92009-03-13 04:39:26 +0000331 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000332 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000333
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000334 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000335 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000336 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000337 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000338
339 // strcmp(P, "x") -> memcmp(P, "x", 2)
340 uint64_t Len1 = GetStringLength(Str1P);
341 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000342 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000343 // These optimizations require TargetData.
344 if (!TD) return 0;
345
Nick Lewycky13a09e22008-12-21 00:19:21 +0000346 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000347 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000348 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000349 }
350
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000351 return 0;
352 }
353};
354
355//===---------------------------------------===//
356// 'strncmp' Optimizations
357
Chris Lattner3e8b6632009-09-02 06:11:42 +0000358struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000359 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000360 // Verify the "strncmp" function prototype.
361 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000362 if (FT->getNumParams() != 3 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000363 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000364 FT->getParamType(0) != FT->getParamType(1) ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000365 FT->getParamType(0) != B.getInt8PtrTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000366 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000367 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000368
Gabor Greifaee5dc12010-06-24 10:42:46 +0000369 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000370 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000371 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000372
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000373 // Get the length argument if it is constant.
374 uint64_t Length;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000375 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000376 Length = LengthArg->getZExtValue();
377 else
378 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000379
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000380 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000381 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000382
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000383 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000384 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD);
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000385
Bill Wendling0582ae92009-03-13 04:39:26 +0000386 std::string Str1, Str2;
387 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
388 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000389
Bill Wendling0582ae92009-03-13 04:39:26 +0000390 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000391 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000392
Bill Wendling0582ae92009-03-13 04:39:26 +0000393 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000394 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000395
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000396 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000397 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000398 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000399 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000400 return 0;
401 }
402};
403
404
405//===---------------------------------------===//
406// 'strcpy' Optimizations
407
Chris Lattner3e8b6632009-09-02 06:11:42 +0000408struct StrCpyOpt : public LibCallOptimization {
Evan Chengeb8c6452010-03-24 20:19:04 +0000409 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
410
411 StrCpyOpt(bool c) : OptChkCall(c) {}
412
Eric Christopher7a61d702008-08-08 19:39:37 +0000413 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000414 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000415 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000416 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000417 if (FT->getNumParams() != NumParams ||
418 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000419 FT->getParamType(0) != FT->getParamType(1) ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000420 FT->getParamType(0) != B.getInt8PtrTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000421 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000422
Gabor Greifaee5dc12010-06-24 10:42:46 +0000423 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000424 if (Dst == Src) // strcpy(x,x) -> x
425 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000426
Dan Gohmanf14d9192009-08-18 00:48:13 +0000427 // These optimizations require TargetData.
428 if (!TD) return 0;
429
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000430 // See if we can get the length of the input string.
431 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000432 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000433
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000434 // We have enough information to now generate the memcpy call to do the
435 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000436 if (OptChkCall)
437 EmitMemCpyChk(Dst, Src,
438 ConstantInt::get(TD->getIntPtrType(*Context), Len),
Gabor Greifaee5dc12010-06-24 10:42:46 +0000439 CI->getArgOperand(2), B, TD);
Evan Cheng0289b412010-03-23 15:48:04 +0000440 else
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000441 B.CreateMemCpy(Dst, Src,
442 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000443 return Dst;
444 }
445};
446
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000447//===---------------------------------------===//
448// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000449
Chris Lattner3e8b6632009-09-02 06:11:42 +0000450struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000451 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
452 const FunctionType *FT = Callee->getFunctionType();
453 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
454 FT->getParamType(0) != FT->getParamType(1) ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000455 FT->getParamType(0) != B.getInt8PtrTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000456 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000457 return 0;
458
Gabor Greifaee5dc12010-06-24 10:42:46 +0000459 Value *Dst = CI->getArgOperand(0);
460 Value *Src = CI->getArgOperand(1);
461 Value *LenOp = CI->getArgOperand(2);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000462
463 // See if we can get the length of the input string.
464 uint64_t SrcLen = GetStringLength(Src);
465 if (SrcLen == 0) return 0;
466 --SrcLen;
467
468 if (SrcLen == 0) {
469 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000470 B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000471 return Dst;
472 }
473
474 uint64_t Len;
475 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
476 Len = LengthArg->getZExtValue();
477 else
478 return 0;
479
480 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
481
Dan Gohmanf14d9192009-08-18 00:48:13 +0000482 // These optimizations require TargetData.
483 if (!TD) return 0;
484
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000485 // Let strncpy handle the zero padding
486 if (Len > SrcLen+1) return 0;
487
488 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000489 B.CreateMemCpy(Dst, Src,
490 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000491
492 return Dst;
493 }
494};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000495
496//===---------------------------------------===//
497// 'strlen' Optimizations
498
Chris Lattner3e8b6632009-09-02 06:11:42 +0000499struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000500 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000501 const FunctionType *FT = Callee->getFunctionType();
502 if (FT->getNumParams() != 1 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000503 FT->getParamType(0) != B.getInt8PtrTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000504 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000505 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000506
Gabor Greifaee5dc12010-06-24 10:42:46 +0000507 Value *Src = CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000508
509 // Constant folding: strlen("xyz") -> 3
510 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000511 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000512
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000513 // strlen(x) != 0 --> *x != 0
514 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000515 if (IsOnlyUsedInZeroEqualityComparison(CI))
516 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
517 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000518 }
519};
520
Benjamin Kramer05f585e2010-09-29 23:52:12 +0000521
522//===---------------------------------------===//
523// 'strpbrk' Optimizations
524
525struct StrPBrkOpt : public LibCallOptimization {
526 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
527 const FunctionType *FT = Callee->getFunctionType();
528 if (FT->getNumParams() != 2 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000529 FT->getParamType(0) != B.getInt8PtrTy() ||
Benjamin Kramer05f585e2010-09-29 23:52:12 +0000530 FT->getParamType(1) != FT->getParamType(0) ||
531 FT->getReturnType() != FT->getParamType(0))
532 return 0;
533
534 std::string S1, S2;
535 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
536 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
537
538 // strpbrk(s, "") -> NULL
539 // strpbrk("", s) -> NULL
540 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
541 return Constant::getNullValue(CI->getType());
542
543 // Constant folding.
544 if (HasS1 && HasS2) {
545 size_t I = S1.find_first_of(S2);
546 if (I == std::string::npos) // No match.
547 return Constant::getNullValue(CI->getType());
548
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000549 return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");
Benjamin Kramer05f585e2010-09-29 23:52:12 +0000550 }
551
552 // strpbrk(s, "a") -> strchr(s, 'a')
553 if (TD && HasS2 && S2.size() == 1)
554 return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD);
555
556 return 0;
557 }
558};
559
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000560//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000561// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000562
Chris Lattner3e8b6632009-09-02 06:11:42 +0000563struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000564 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
565 const FunctionType *FT = Callee->getFunctionType();
566 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000567 !FT->getParamType(0)->isPointerTy() ||
568 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000569 return 0;
570
Gabor Greifaee5dc12010-06-24 10:42:46 +0000571 Value *EndPtr = CI->getArgOperand(1);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000572 if (isa<ConstantPointerNull>(EndPtr)) {
Dan Gohmanc32046e2010-12-17 01:09:43 +0000573 // With a null EndPtr, this function won't capture the main argument.
574 // It would be readonly too, except that it still may write to errno.
Nick Lewycky4c498412009-02-13 15:31:46 +0000575 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000576 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000577
578 return 0;
579 }
580};
581
Chris Lattner24604112009-12-16 09:32:05 +0000582//===---------------------------------------===//
Benjamin Kramer9510a252010-09-30 00:58:35 +0000583// 'strspn' Optimizations
584
585struct StrSpnOpt : public LibCallOptimization {
586 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
587 const FunctionType *FT = Callee->getFunctionType();
588 if (FT->getNumParams() != 2 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000589 FT->getParamType(0) != B.getInt8PtrTy() ||
Benjamin Kramer9510a252010-09-30 00:58:35 +0000590 FT->getParamType(1) != FT->getParamType(0) ||
591 !FT->getReturnType()->isIntegerTy())
592 return 0;
593
594 std::string S1, S2;
595 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
596 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
597
598 // strspn(s, "") -> 0
599 // strspn("", s) -> 0
600 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
601 return Constant::getNullValue(CI->getType());
602
603 // Constant folding.
604 if (HasS1 && HasS2)
605 return ConstantInt::get(CI->getType(), strspn(S1.c_str(), S2.c_str()));
606
607 return 0;
608 }
609};
610
611//===---------------------------------------===//
612// 'strcspn' Optimizations
613
614struct StrCSpnOpt : public LibCallOptimization {
615 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
616 const FunctionType *FT = Callee->getFunctionType();
617 if (FT->getNumParams() != 2 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000618 FT->getParamType(0) != B.getInt8PtrTy() ||
Benjamin Kramer9510a252010-09-30 00:58:35 +0000619 FT->getParamType(1) != FT->getParamType(0) ||
620 !FT->getReturnType()->isIntegerTy())
621 return 0;
622
623 std::string S1, S2;
624 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
625 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
626
627 // strcspn("", s) -> 0
628 if (HasS1 && S1.empty())
629 return Constant::getNullValue(CI->getType());
630
631 // Constant folding.
632 if (HasS1 && HasS2)
633 return ConstantInt::get(CI->getType(), strcspn(S1.c_str(), S2.c_str()));
634
635 // strcspn(s, "") -> strlen(s)
636 if (TD && HasS2 && S2.empty())
637 return EmitStrLen(CI->getArgOperand(0), B, TD);
638
639 return 0;
640 }
641};
642
643//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000644// 'strstr' Optimizations
645
646struct StrStrOpt : public LibCallOptimization {
647 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
648 const FunctionType *FT = Callee->getFunctionType();
649 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000650 !FT->getParamType(0)->isPointerTy() ||
651 !FT->getParamType(1)->isPointerTy() ||
652 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000653 return 0;
654
655 // fold strstr(x, x) -> x.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000656 if (CI->getArgOperand(0) == CI->getArgOperand(1))
657 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000658
Benjamin Kramer386e9182010-06-15 21:34:25 +0000659 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000660 if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
661 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD);
662 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000663 StrLen, B, TD);
664 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
665 UI != UE; ) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000666 ICmpInst *Old = cast<ICmpInst>(*UI++);
Benjamin Kramer386e9182010-06-15 21:34:25 +0000667 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
668 ConstantInt::getNullValue(StrNCmp->getType()),
669 "cmp");
670 Old->replaceAllUsesWith(Cmp);
671 Old->eraseFromParent();
672 }
673 return CI;
674 }
675
Chris Lattner24604112009-12-16 09:32:05 +0000676 // See if either input string is a constant string.
677 std::string SearchStr, ToFindStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000678 bool HasStr1 = GetConstantStringInfo(CI->getArgOperand(0), SearchStr);
679 bool HasStr2 = GetConstantStringInfo(CI->getArgOperand(1), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000680
Chris Lattner24604112009-12-16 09:32:05 +0000681 // fold strstr(x, "") -> x.
682 if (HasStr2 && ToFindStr.empty())
Gabor Greifaee5dc12010-06-24 10:42:46 +0000683 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000684
Chris Lattner24604112009-12-16 09:32:05 +0000685 // If both strings are known, constant fold it.
686 if (HasStr1 && HasStr2) {
687 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000688
Chris Lattner24604112009-12-16 09:32:05 +0000689 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
690 return Constant::getNullValue(CI->getType());
691
692 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000693 Value *Result = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner24604112009-12-16 09:32:05 +0000694 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
695 return B.CreateBitCast(Result, CI->getType());
696 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000697
Chris Lattner24604112009-12-16 09:32:05 +0000698 // fold strstr(x, "y") -> strchr(x, 'y').
699 if (HasStr2 && ToFindStr.size() == 1)
Gabor Greifa3997812010-07-22 10:37:47 +0000700 return B.CreateBitCast(EmitStrChr(CI->getArgOperand(0),
701 ToFindStr[0], B, TD), CI->getType());
Chris Lattner24604112009-12-16 09:32:05 +0000702 return 0;
703 }
704};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000705
Nick Lewycky4c498412009-02-13 15:31:46 +0000706
707//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000708// 'memcmp' Optimizations
709
Chris Lattner3e8b6632009-09-02 06:11:42 +0000710struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000711 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000712 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000713 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
714 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000715 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000716 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000717
Gabor Greifaee5dc12010-06-24 10:42:46 +0000718 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000719
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000720 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000721 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000722
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000723 // Make sure we have a constant length.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000724 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000725 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000726 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000727
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000728 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000729 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000730
Benjamin Kramer48aefe12010-05-25 22:53:43 +0000731 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
732 if (Len == 1) {
733 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
734 CI->getType(), "lhsv");
735 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
736 CI->getType(), "rhsv");
Benjamin Kramer1464c1d2010-05-26 09:45:04 +0000737 return B.CreateSub(LHSV, RHSV, "chardiff");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000738 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000739
Benjamin Kramer992a6372009-11-05 17:44:22 +0000740 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
741 std::string LHSStr, RHSStr;
742 if (GetConstantStringInfo(LHS, LHSStr) &&
743 GetConstantStringInfo(RHS, RHSStr)) {
744 // Make sure we're not reading out-of-bounds memory.
745 if (Len > LHSStr.length() || Len > RHSStr.length())
746 return 0;
747 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
748 return ConstantInt::get(CI->getType(), Ret);
749 }
750
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000751 return 0;
752 }
753};
754
755//===---------------------------------------===//
756// 'memcpy' Optimizations
757
Chris Lattner3e8b6632009-09-02 06:11:42 +0000758struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000759 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000760 // These optimizations require TargetData.
761 if (!TD) return 0;
762
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000763 const FunctionType *FT = Callee->getFunctionType();
764 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000765 !FT->getParamType(0)->isPointerTy() ||
766 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000767 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000768 return 0;
769
770 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000771 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
772 CI->getArgOperand(2), 1);
Gabor Greifaee5dc12010-06-24 10:42:46 +0000773 return CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000774 }
775};
776
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000777//===---------------------------------------===//
778// 'memmove' Optimizations
779
Chris Lattner3e8b6632009-09-02 06:11:42 +0000780struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000781 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000782 // These optimizations require TargetData.
783 if (!TD) return 0;
784
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000785 const FunctionType *FT = Callee->getFunctionType();
786 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000787 !FT->getParamType(0)->isPointerTy() ||
788 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000789 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000790 return 0;
791
792 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000793 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
794 CI->getArgOperand(2), 1);
Gabor Greifaee5dc12010-06-24 10:42:46 +0000795 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000796 }
797};
798
799//===---------------------------------------===//
800// 'memset' Optimizations
801
Chris Lattner3e8b6632009-09-02 06:11:42 +0000802struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000803 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000804 // These optimizations require TargetData.
805 if (!TD) return 0;
806
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000807 const FunctionType *FT = Callee->getFunctionType();
808 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000809 !FT->getParamType(0)->isPointerTy() ||
810 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000811 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000812 return 0;
813
814 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000815 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
816 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
Gabor Greifaee5dc12010-06-24 10:42:46 +0000817 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000818 }
819};
820
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000821//===----------------------------------------------------------------------===//
822// Math Library Optimizations
823//===----------------------------------------------------------------------===//
824
825//===---------------------------------------===//
826// 'pow*' Optimizations
827
Chris Lattner3e8b6632009-09-02 06:11:42 +0000828struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000829 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000830 const FunctionType *FT = Callee->getFunctionType();
831 // Just make sure this has 2 arguments of the same FP type, which match the
832 // result type.
833 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
834 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000835 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000836 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000837
Gabor Greifaee5dc12010-06-24 10:42:46 +0000838 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000839 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
840 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
841 return Op1C;
842 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000843 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000844 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000845
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000846 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
847 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000848
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000849 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000850 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000851
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000852 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000853 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
854 // This is faster than calling pow, and still handles negative zero
855 // and negative infinite correctly.
856 // TODO: In fast-math mode, this could be just sqrt(x).
857 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000858 Value *Inf = ConstantFP::getInfinity(CI->getType());
859 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000860 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
861 Callee->getAttributes());
862 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
863 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000864 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
865 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
866 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000867 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000868
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000869 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
870 return Op1;
871 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000872 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000873 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000874 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000875 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000876 return 0;
877 }
878};
879
880//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000881// 'exp2' Optimizations
882
Chris Lattner3e8b6632009-09-02 06:11:42 +0000883struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000884 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000885 const FunctionType *FT = Callee->getFunctionType();
886 // Just make sure this has 1 argument of FP type, which matches the
887 // result type.
888 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000889 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000890 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000891
Gabor Greifaee5dc12010-06-24 10:42:46 +0000892 Value *Op = CI->getArgOperand(0);
Chris Lattnere818f772008-05-02 18:43:35 +0000893 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
894 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
895 Value *LdExpArg = 0;
896 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
897 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000898 LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty(), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000899 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
900 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000901 LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty(), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000902 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000903
Chris Lattnere818f772008-05-02 18:43:35 +0000904 if (LdExpArg) {
905 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000906 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000907 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000908 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000909 Name = "ldexp";
910 else
911 Name = "ldexpl";
912
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000913 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000914 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000915 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000916
917 Module *M = Caller->getParent();
918 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000919 Op->getType(),
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000920 B.getInt32Ty(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000921 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
922 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
923 CI->setCallingConv(F->getCallingConv());
924
925 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000926 }
927 return 0;
928 }
929};
Chris Lattnere818f772008-05-02 18:43:35 +0000930
931//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000932// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
933
Chris Lattner3e8b6632009-09-02 06:11:42 +0000934struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000935 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000936 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000937 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
938 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000939 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000940
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000941 // If this is something like 'floor((double)floatval)', convert to floorf.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000942 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000943 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000944 return 0;
945
946 // floor((double)floatval) -> (double)floorf(floatval)
947 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000948 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
949 Callee->getAttributes());
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000950 return B.CreateFPExt(V, B.getDoubleTy());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000951 }
952};
953
954//===----------------------------------------------------------------------===//
955// Integer Optimizations
956//===----------------------------------------------------------------------===//
957
958//===---------------------------------------===//
959// 'ffs*' Optimizations
960
Chris Lattner3e8b6632009-09-02 06:11:42 +0000961struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000962 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000963 const FunctionType *FT = Callee->getFunctionType();
964 // Just make sure this has 2 arguments of the same FP type, which match the
965 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000966 if (FT->getNumParams() != 1 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000967 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000968 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000969 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000970
Gabor Greifaee5dc12010-06-24 10:42:46 +0000971 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000972
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000973 // Constant fold.
974 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
975 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000976 return Constant::getNullValue(CI->getType());
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000977 // ffs(c) -> cttz(c)+1
978 return B.getInt32(CI->getValue().countTrailingZeros() + 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000979 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000980
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000981 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
982 const Type *ArgType = Op->getType();
983 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
984 Intrinsic::cttz, &ArgType, 1);
985 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000986 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000987 V = B.CreateIntCast(V, B.getInt32Ty(), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000988
Owen Andersona7235ea2009-07-31 20:28:14 +0000989 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000990 return B.CreateSelect(Cond, V, B.getInt32(0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000991 }
992};
993
994//===---------------------------------------===//
995// 'isdigit' Optimizations
996
Chris Lattner3e8b6632009-09-02 06:11:42 +0000997struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000998 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000999 const FunctionType *FT = Callee->getFunctionType();
1000 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +00001001 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001002 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001003 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001004
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001005 // isdigit(c) -> (c-'0') <u 10
Gabor Greifaee5dc12010-06-24 10:42:46 +00001006 Value *Op = CI->getArgOperand(0);
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001007 Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
1008 Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001009 return B.CreateZExt(Op, CI->getType());
1010 }
1011};
1012
1013//===---------------------------------------===//
1014// 'isascii' Optimizations
1015
Chris Lattner3e8b6632009-09-02 06:11:42 +00001016struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001017 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001018 const FunctionType *FT = Callee->getFunctionType();
1019 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +00001020 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001021 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001022 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001023
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001024 // isascii(c) -> c <u 128
Gabor Greifaee5dc12010-06-24 10:42:46 +00001025 Value *Op = CI->getArgOperand(0);
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001026 Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001027 return B.CreateZExt(Op, CI->getType());
1028 }
1029};
Eric Christopher37c8b862009-10-07 21:14:25 +00001030
Chris Lattner313f0e62008-06-09 08:26:51 +00001031//===---------------------------------------===//
1032// 'abs', 'labs', 'llabs' Optimizations
1033
Chris Lattner3e8b6632009-09-02 06:11:42 +00001034struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001035 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001036 const FunctionType *FT = Callee->getFunctionType();
1037 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +00001038 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +00001039 FT->getParamType(0) != FT->getReturnType())
1040 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001041
Chris Lattner313f0e62008-06-09 08:26:51 +00001042 // abs(x) -> x >s -1 ? x : -x
Gabor Greifaee5dc12010-06-24 10:42:46 +00001043 Value *Op = CI->getArgOperand(0);
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001044 Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001045 "ispos");
1046 Value *Neg = B.CreateNeg(Op, "neg");
1047 return B.CreateSelect(Pos, Op, Neg);
1048 }
1049};
Eric Christopher37c8b862009-10-07 21:14:25 +00001050
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001051
1052//===---------------------------------------===//
1053// 'toascii' Optimizations
1054
Chris Lattner3e8b6632009-09-02 06:11:42 +00001055struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001056 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001057 const FunctionType *FT = Callee->getFunctionType();
1058 // We require i32(i32)
1059 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001060 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001061 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001062
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001063 // isascii(c) -> c & 0x7f
Gabor Greifaee5dc12010-06-24 10:42:46 +00001064 return B.CreateAnd(CI->getArgOperand(0),
Owen Andersoneed707b2009-07-24 23:12:02 +00001065 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001066 }
1067};
1068
1069//===----------------------------------------------------------------------===//
1070// Formatting and IO Optimizations
1071//===----------------------------------------------------------------------===//
1072
1073//===---------------------------------------===//
1074// 'printf' Optimizations
1075
Chris Lattner3e8b6632009-09-02 06:11:42 +00001076struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001077 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001078 // Require one fixed pointer argument and an integer/void result.
1079 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001080 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1081 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001082 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001083 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001084
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001085 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001086 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001087 if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001088 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001089
1090 // Empty format string -> noop.
1091 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001092 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001093 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001094
Chris Lattner74965f22009-11-09 04:57:04 +00001095 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1096 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001097 if (FormatStr.size() == 1) {
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001098 Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +00001099 if (CI->use_empty()) return CI;
1100 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001101 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001102
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001103 // printf("foo\n") --> puts("foo")
1104 if (FormatStr[FormatStr.size()-1] == '\n' &&
1105 FormatStr.find('%') == std::string::npos) { // no format characters.
1106 // Create a string literal with no \n on it. We expect the constant merge
1107 // pass to be run after this pass, to merge duplicate strings.
1108 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001109 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001110 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1111 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +00001112 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001113 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001114 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001115 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001116
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001117 // Optimize specific format strings.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001118 // printf("%c", chr) --> putchar(chr)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001119 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001120 CI->getArgOperand(1)->getType()->isIntegerTy()) {
1121 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001122
Chris Lattner74965f22009-11-09 04:57:04 +00001123 if (CI->use_empty()) return CI;
1124 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001125 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001126
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001127 // printf("%s\n", str) --> puts(str)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001128 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001129 CI->getArgOperand(1)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001130 CI->use_empty()) {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001131 EmitPutS(CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001132 return CI;
1133 }
1134 return 0;
1135 }
1136};
1137
1138//===---------------------------------------===//
1139// 'sprintf' Optimizations
1140
Chris Lattner3e8b6632009-09-02 06:11:42 +00001141struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001142 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001143 // Require two fixed pointer arguments and an integer result.
1144 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001145 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1146 !FT->getParamType(1)->isPointerTy() ||
1147 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001148 return 0;
1149
1150 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001151 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001152 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001153 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001154
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001155 // If we just have a format string (nothing else crazy) transform it.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001156 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001157 // Make sure there's no % in the constant array. We could try to handle
1158 // %% -> % in the future if we cared.
1159 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1160 if (FormatStr[i] == '%')
1161 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001162
1163 // These optimizations require TargetData.
1164 if (!TD) return 0;
1165
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001166 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001167 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
1168 ConstantInt::get(TD->getIntPtrType(*Context), // Copy the
1169 FormatStr.size() + 1), 1); // nul byte.
Owen Andersoneed707b2009-07-24 23:12:02 +00001170 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001171 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001172
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001173 // The remaining optimizations require the format string to be "%s" or "%c"
1174 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001175 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1176 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001177 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001178
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001179 // Decode the second character of the format string.
1180 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001181 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Gabor Greifaee5dc12010-06-24 10:42:46 +00001182 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001183 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
Gabor Greifaee5dc12010-06-24 10:42:46 +00001184 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001185 B.CreateStore(V, Ptr);
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001186 Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");
1187 B.CreateStore(B.getInt8(0), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001188
Owen Andersoneed707b2009-07-24 23:12:02 +00001189 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001190 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001191
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001192 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001193 // These optimizations require TargetData.
1194 if (!TD) return 0;
1195
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001196 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001197 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001198
Gabor Greifaee5dc12010-06-24 10:42:46 +00001199 Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001200 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001201 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001202 "leninc");
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001203 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001204
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001205 // The sprintf result is the unincremented number of bytes in the string.
1206 return B.CreateIntCast(Len, CI->getType(), false);
1207 }
1208 return 0;
1209 }
1210};
1211
1212//===---------------------------------------===//
1213// 'fwrite' Optimizations
1214
Chris Lattner3e8b6632009-09-02 06:11:42 +00001215struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001216 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001217 // Require a pointer, an integer, an integer, a pointer, returning integer.
1218 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001219 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1220 !FT->getParamType(1)->isIntegerTy() ||
1221 !FT->getParamType(2)->isIntegerTy() ||
1222 !FT->getParamType(3)->isPointerTy() ||
1223 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001224 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001225
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001226 // Get the element size and count.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001227 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1228 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001229 if (!SizeC || !CountC) return 0;
1230 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001231
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001232 // If this is writing zero records, remove the call (it's a noop).
1233 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001234 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001235
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001236 // If this is writing one byte, turn it into fputc.
1237 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001238 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1239 EmitFPutC(Char, CI->getArgOperand(3), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001240 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001241 }
1242
1243 return 0;
1244 }
1245};
1246
1247//===---------------------------------------===//
1248// 'fputs' Optimizations
1249
Chris Lattner3e8b6632009-09-02 06:11:42 +00001250struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001251 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001252 // These optimizations require TargetData.
1253 if (!TD) return 0;
1254
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001255 // Require two pointers. Also, we can't optimize if return value is used.
1256 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001257 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1258 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001259 !CI->use_empty())
1260 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001261
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001262 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001263 uint64_t Len = GetStringLength(CI->getArgOperand(0));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001264 if (!Len) return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001265 EmitFWrite(CI->getArgOperand(0),
Owen Anderson1d0be152009-08-13 21:58:54 +00001266 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001267 CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001268 return CI; // Known to have no uses (see above).
1269 }
1270};
1271
1272//===---------------------------------------===//
1273// 'fprintf' Optimizations
1274
Chris Lattner3e8b6632009-09-02 06:11:42 +00001275struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001276 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001277 // Require two fixed paramters as pointers and integer result.
1278 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001279 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1280 !FT->getParamType(1)->isPointerTy() ||
1281 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001282 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001283
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001284 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001285 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001286 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001287 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001288
1289 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001290 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001291 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1292 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001293 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001294
1295 // These optimizations require TargetData.
1296 if (!TD) return 0;
1297
Gabor Greifaee5dc12010-06-24 10:42:46 +00001298 EmitFWrite(CI->getArgOperand(1),
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001299 ConstantInt::get(TD->getIntPtrType(*Context),
1300 FormatStr.size()),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001301 CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001302 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001303 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001304
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001305 // The remaining optimizations require the format string to be "%s" or "%c"
1306 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001307 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1308 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001309 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001310
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001311 // Decode the second character of the format string.
1312 if (FormatStr[1] == 'c') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001313 // fprintf(F, "%c", chr) --> fputc(chr, F)
1314 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1315 EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001316 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001317 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001318
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001319 if (FormatStr[1] == 's') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001320 // fprintf(F, "%s", str) --> fputs(str, F)
1321 if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001322 return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001323 EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001324 return CI;
1325 }
1326 return 0;
1327 }
1328};
1329
Anders Carlsson303023d2010-11-30 06:19:18 +00001330//===---------------------------------------===//
1331// 'puts' Optimizations
1332
1333struct PutsOpt : public LibCallOptimization {
1334 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1335 // Require one fixed pointer argument and an integer/void result.
1336 const FunctionType *FT = Callee->getFunctionType();
1337 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1338 !(FT->getReturnType()->isIntegerTy() ||
1339 FT->getReturnType()->isVoidTy()))
1340 return 0;
1341
1342 // Check for a constant string.
1343 std::string Str;
1344 if (!GetConstantStringInfo(CI->getArgOperand(0), Str))
1345 return 0;
1346
1347 if (Str.empty()) {
1348 // puts("") -> putchar('\n')
1349 Value *Res = EmitPutChar(B.getInt32('\n'), B, TD);
1350 if (CI->use_empty()) return CI;
1351 return B.CreateIntCast(Res, CI->getType(), true);
1352 }
1353
1354 return 0;
1355 }
1356};
1357
Bill Wendlingac178222008-05-05 21:37:59 +00001358} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001359
1360//===----------------------------------------------------------------------===//
1361// SimplifyLibCalls Pass Implementation
1362//===----------------------------------------------------------------------===//
1363
1364namespace {
1365 /// This pass optimizes well known library functions from libc and libm.
1366 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001367 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001368 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001369 // String and Memory LibCall Optimizations
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001370 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrRChrOpt StrRChr;
1371 StrCmpOpt StrCmp; StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
Benjamin Kramer05f585e2010-09-29 23:52:12 +00001372 StrNCpyOpt StrNCpy; StrLenOpt StrLen; StrPBrkOpt StrPBrk;
Benjamin Kramer9510a252010-09-30 00:58:35 +00001373 StrToOpt StrTo; StrSpnOpt StrSpn; StrCSpnOpt StrCSpn; StrStrOpt StrStr;
Chris Lattner24604112009-12-16 09:32:05 +00001374 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001375 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001376 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001377 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001378 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1379 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001380 // Formatting and IO Optimizations
1381 SPrintFOpt SPrintF; PrintFOpt PrintF;
1382 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Anders Carlsson303023d2010-11-30 06:19:18 +00001383 PutsOpt Puts;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001384
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001385 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001386 public:
1387 static char ID; // Pass identification
Owen Anderson081c34b2010-10-19 17:21:58 +00001388 SimplifyLibCalls() : FunctionPass(ID), StrCpy(false), StrCpyChk(true) {
1389 initializeSimplifyLibCallsPass(*PassRegistry::getPassRegistry());
1390 }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001391 void InitOptimizations();
1392 bool runOnFunction(Function &F);
1393
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001394 void setDoesNotAccessMemory(Function &F);
1395 void setOnlyReadsMemory(Function &F);
1396 void setDoesNotThrow(Function &F);
1397 void setDoesNotCapture(Function &F, unsigned n);
1398 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001399 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001400
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001401 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001402 }
1403 };
1404 char SimplifyLibCalls::ID = 0;
1405} // end anonymous namespace.
1406
Owen Andersond13db2c2010-07-21 22:09:45 +00001407INITIALIZE_PASS(SimplifyLibCalls, "simplify-libcalls",
Owen Andersonce665bd2010-10-07 22:25:06 +00001408 "Simplify well-known library calls", false, false)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001409
1410// Public interface to the Simplify LibCalls pass.
1411FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001412 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001413}
1414
1415/// Optimizations - Populate the Optimizations map with all the optimizations
1416/// we know.
1417void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001418 // String and Memory LibCall Optimizations
1419 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001420 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001421 Optimizations["strchr"] = &StrChr;
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001422 Optimizations["strrchr"] = &StrRChr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001423 Optimizations["strcmp"] = &StrCmp;
1424 Optimizations["strncmp"] = &StrNCmp;
1425 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001426 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001427 Optimizations["strlen"] = &StrLen;
Benjamin Kramer05f585e2010-09-29 23:52:12 +00001428 Optimizations["strpbrk"] = &StrPBrk;
Nick Lewycky4c498412009-02-13 15:31:46 +00001429 Optimizations["strtol"] = &StrTo;
1430 Optimizations["strtod"] = &StrTo;
1431 Optimizations["strtof"] = &StrTo;
1432 Optimizations["strtoul"] = &StrTo;
1433 Optimizations["strtoll"] = &StrTo;
1434 Optimizations["strtold"] = &StrTo;
1435 Optimizations["strtoull"] = &StrTo;
Benjamin Kramer9510a252010-09-30 00:58:35 +00001436 Optimizations["strspn"] = &StrSpn;
1437 Optimizations["strcspn"] = &StrCSpn;
Chris Lattner24604112009-12-16 09:32:05 +00001438 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001439 Optimizations["memcmp"] = &MemCmp;
1440 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001441 Optimizations["memmove"] = &MemMove;
1442 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001443
Evan Cheng0289b412010-03-23 15:48:04 +00001444 // _chk variants of String and Memory LibCall Optimizations.
Evan Cheng0289b412010-03-23 15:48:04 +00001445 Optimizations["__strcpy_chk"] = &StrCpyChk;
1446
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001447 // Math Library Optimizations
1448 Optimizations["powf"] = &Pow;
1449 Optimizations["pow"] = &Pow;
1450 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001451 Optimizations["llvm.pow.f32"] = &Pow;
1452 Optimizations["llvm.pow.f64"] = &Pow;
1453 Optimizations["llvm.pow.f80"] = &Pow;
1454 Optimizations["llvm.pow.f128"] = &Pow;
1455 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001456 Optimizations["exp2l"] = &Exp2;
1457 Optimizations["exp2"] = &Exp2;
1458 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001459 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1460 Optimizations["llvm.exp2.f128"] = &Exp2;
1461 Optimizations["llvm.exp2.f80"] = &Exp2;
1462 Optimizations["llvm.exp2.f64"] = &Exp2;
1463 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001464
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001465#ifdef HAVE_FLOORF
1466 Optimizations["floor"] = &UnaryDoubleFP;
1467#endif
1468#ifdef HAVE_CEILF
1469 Optimizations["ceil"] = &UnaryDoubleFP;
1470#endif
1471#ifdef HAVE_ROUNDF
1472 Optimizations["round"] = &UnaryDoubleFP;
1473#endif
1474#ifdef HAVE_RINTF
1475 Optimizations["rint"] = &UnaryDoubleFP;
1476#endif
1477#ifdef HAVE_NEARBYINTF
1478 Optimizations["nearbyint"] = &UnaryDoubleFP;
1479#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001480
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001481 // Integer Optimizations
1482 Optimizations["ffs"] = &FFS;
1483 Optimizations["ffsl"] = &FFS;
1484 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001485 Optimizations["abs"] = &Abs;
1486 Optimizations["labs"] = &Abs;
1487 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001488 Optimizations["isdigit"] = &IsDigit;
1489 Optimizations["isascii"] = &IsAscii;
1490 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001491
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001492 // Formatting and IO Optimizations
1493 Optimizations["sprintf"] = &SPrintF;
1494 Optimizations["printf"] = &PrintF;
1495 Optimizations["fwrite"] = &FWrite;
1496 Optimizations["fputs"] = &FPuts;
1497 Optimizations["fprintf"] = &FPrintF;
Anders Carlsson303023d2010-11-30 06:19:18 +00001498 Optimizations["puts"] = &Puts;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001499}
1500
1501
1502/// runOnFunction - Top level algorithm.
1503///
1504bool SimplifyLibCalls::runOnFunction(Function &F) {
1505 if (Optimizations.empty())
1506 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001507
Dan Gohmanf14d9192009-08-18 00:48:13 +00001508 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001509
Owen Andersone922c022009-07-22 00:24:57 +00001510 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001511
1512 bool Changed = false;
1513 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1514 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1515 // Ignore non-calls.
1516 CallInst *CI = dyn_cast<CallInst>(I++);
1517 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001518
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001519 // Ignore indirect calls and calls to non-external functions.
1520 Function *Callee = CI->getCalledFunction();
1521 if (Callee == 0 || !Callee->isDeclaration() ||
1522 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1523 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001524
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001525 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001526 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1527 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001528
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001529 // Set the builder to the instruction after the call.
1530 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001531
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001532 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001533 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001534 if (Result == 0) continue;
1535
David Greene6a6b90e2010-01-05 01:27:21 +00001536 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1537 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001538
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001539 // Something changed!
1540 Changed = true;
1541 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001542
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001543 // Inspect the instruction after the call (which was potentially just
1544 // added) next.
1545 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001546
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001547 if (CI != Result && !CI->use_empty()) {
1548 CI->replaceAllUsesWith(Result);
1549 if (!Result->hasName())
1550 Result->takeName(CI);
1551 }
1552 CI->eraseFromParent();
1553 }
1554 }
1555 return Changed;
1556}
1557
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001558// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001559
1560void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1561 if (!F.doesNotAccessMemory()) {
1562 F.setDoesNotAccessMemory();
1563 ++NumAnnotated;
1564 Modified = true;
1565 }
1566}
1567void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1568 if (!F.onlyReadsMemory()) {
1569 F.setOnlyReadsMemory();
1570 ++NumAnnotated;
1571 Modified = true;
1572 }
1573}
1574void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1575 if (!F.doesNotThrow()) {
1576 F.setDoesNotThrow();
1577 ++NumAnnotated;
1578 Modified = true;
1579 }
1580}
1581void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1582 if (!F.doesNotCapture(n)) {
1583 F.setDoesNotCapture(n);
1584 ++NumAnnotated;
1585 Modified = true;
1586 }
1587}
1588void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1589 if (!F.doesNotAlias(n)) {
1590 F.setDoesNotAlias(n);
1591 ++NumAnnotated;
1592 Modified = true;
1593 }
1594}
1595
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001596/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001597///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001598bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001599 Modified = false;
1600 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1601 Function &F = *I;
1602 if (!F.isDeclaration())
1603 continue;
1604
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001605 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001606 continue;
1607
1608 const FunctionType *FTy = F.getFunctionType();
1609
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001610 StringRef Name = F.getName();
1611 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001612 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001613 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001614 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001615 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001616 continue;
1617 setOnlyReadsMemory(F);
1618 setDoesNotThrow(F);
1619 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001620 } else if (Name == "strchr" ||
1621 Name == "strrchr") {
1622 if (FTy->getNumParams() != 2 ||
1623 !FTy->getParamType(0)->isPointerTy() ||
1624 !FTy->getParamType(1)->isIntegerTy())
1625 continue;
1626 setOnlyReadsMemory(F);
1627 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001628 } else if (Name == "strcpy" ||
1629 Name == "stpcpy" ||
1630 Name == "strcat" ||
1631 Name == "strtol" ||
1632 Name == "strtod" ||
1633 Name == "strtof" ||
1634 Name == "strtoul" ||
1635 Name == "strtoll" ||
1636 Name == "strtold" ||
1637 Name == "strncat" ||
1638 Name == "strncpy" ||
1639 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001640 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001641 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001642 continue;
1643 setDoesNotThrow(F);
1644 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001645 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001646 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001647 !FTy->getParamType(0)->isPointerTy() ||
1648 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001649 continue;
1650 setDoesNotThrow(F);
1651 setDoesNotCapture(F, 1);
1652 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001653 } else if (Name == "strcmp" ||
1654 Name == "strspn" ||
1655 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001656 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001657 Name == "strcoll" ||
1658 Name == "strcasecmp" ||
1659 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001660 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001661 !FTy->getParamType(0)->isPointerTy() ||
1662 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001663 continue;
1664 setOnlyReadsMemory(F);
1665 setDoesNotThrow(F);
1666 setDoesNotCapture(F, 1);
1667 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001668 } else if (Name == "strstr" ||
1669 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001670 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001671 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001672 continue;
1673 setOnlyReadsMemory(F);
1674 setDoesNotThrow(F);
1675 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001676 } else if (Name == "strtok" ||
1677 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001678 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001679 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001680 continue;
1681 setDoesNotThrow(F);
1682 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001683 } else if (Name == "scanf" ||
1684 Name == "setbuf" ||
1685 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001686 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001687 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001688 continue;
1689 setDoesNotThrow(F);
1690 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001691 } else if (Name == "strdup" ||
1692 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001693 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001694 !FTy->getReturnType()->isPointerTy() ||
1695 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001696 continue;
1697 setDoesNotThrow(F);
1698 setDoesNotAlias(F, 0);
1699 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001700 } else if (Name == "stat" ||
1701 Name == "sscanf" ||
1702 Name == "sprintf" ||
1703 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001704 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001705 !FTy->getParamType(0)->isPointerTy() ||
1706 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001707 continue;
1708 setDoesNotThrow(F);
1709 setDoesNotCapture(F, 1);
1710 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001711 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001712 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001713 !FTy->getParamType(0)->isPointerTy() ||
1714 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001715 continue;
1716 setDoesNotThrow(F);
1717 setDoesNotCapture(F, 1);
1718 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001719 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001720 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001721 !FTy->getParamType(1)->isPointerTy() ||
1722 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001723 continue;
1724 setDoesNotThrow(F);
1725 setDoesNotCapture(F, 2);
1726 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001727 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001728 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001729 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001730 continue;
1731 // May throw; "system" is a valid pthread cancellation point.
1732 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001733 }
1734 break;
1735 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001736 if (Name == "malloc") {
1737 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001738 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001739 continue;
1740 setDoesNotThrow(F);
1741 setDoesNotAlias(F, 0);
1742 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001743 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001744 !FTy->getParamType(0)->isPointerTy() ||
1745 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001746 continue;
1747 setOnlyReadsMemory(F);
1748 setDoesNotThrow(F);
1749 setDoesNotCapture(F, 1);
1750 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001751 } else if (Name == "memchr" ||
1752 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001753 if (FTy->getNumParams() != 3)
1754 continue;
1755 setOnlyReadsMemory(F);
1756 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001757 } else if (Name == "modf" ||
1758 Name == "modff" ||
1759 Name == "modfl" ||
1760 Name == "memcpy" ||
1761 Name == "memccpy" ||
1762 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001763 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001764 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001765 continue;
1766 setDoesNotThrow(F);
1767 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001768 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001769 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001770 continue;
1771 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001772 } else if (Name == "mkdir" ||
1773 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001774 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001775 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001776 continue;
1777 setDoesNotThrow(F);
1778 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001779 }
1780 break;
1781 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001782 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001783 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001784 !FTy->getParamType(0)->isPointerTy() ||
1785 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001786 continue;
1787 setDoesNotThrow(F);
1788 setDoesNotAlias(F, 0);
1789 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001790 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001791 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001792 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001793 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001794 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001795 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001796 } else if (Name == "rmdir" ||
1797 Name == "rewind" ||
1798 Name == "remove" ||
1799 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001800 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001801 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001802 continue;
1803 setDoesNotThrow(F);
1804 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001805 } else if (Name == "rename" ||
1806 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001807 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001808 !FTy->getParamType(0)->isPointerTy() ||
1809 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001810 continue;
1811 setDoesNotThrow(F);
1812 setDoesNotCapture(F, 1);
1813 setDoesNotCapture(F, 2);
1814 }
1815 break;
1816 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001817 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001818 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001819 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001820 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001821 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001822 setDoesNotCapture(F, 2);
1823 }
1824 break;
1825 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001826 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001827 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001828 !FTy->getParamType(0)->isPointerTy() ||
1829 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001830 continue;
1831 setDoesNotThrow(F);
1832 setDoesNotCapture(F, 1);
1833 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001834 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001835 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001836 !FTy->getParamType(0)->isPointerTy() ||
1837 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001838 continue;
1839 setDoesNotThrow(F);
1840 setOnlyReadsMemory(F);
1841 setDoesNotCapture(F, 1);
1842 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001843 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001844 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001845 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001846 continue;
1847 setDoesNotThrow(F);
1848 setDoesNotCapture(F, 1);
1849 }
1850 break;
1851 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001852 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001853 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001854 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001855 continue;
1856 setDoesNotThrow(F);
1857 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001858 } else if (Name == "chmod" ||
1859 Name == "chown" ||
1860 Name == "ctermid" ||
1861 Name == "clearerr" ||
1862 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001863 if (FTy->getNumParams() == 0 ||
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 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001871 if (Name == "atoi" ||
1872 Name == "atol" ||
1873 Name == "atof" ||
1874 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001875 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001876 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001877 continue;
1878 setDoesNotThrow(F);
1879 setOnlyReadsMemory(F);
1880 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001881 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001882 if (FTy->getNumParams() != 2 ||
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 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001890 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001891 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001892 !FTy->getReturnType()->isPointerTy() ||
1893 !FTy->getParamType(0)->isPointerTy() ||
1894 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001895 continue;
1896 setDoesNotThrow(F);
1897 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001898 setDoesNotCapture(F, 1);
1899 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001900 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001901 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001902 !FTy->getReturnType()->isPointerTy() ||
1903 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001904 continue;
1905 setDoesNotThrow(F);
1906 setDoesNotAlias(F, 0);
1907 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001908 } else if (Name == "feof" ||
1909 Name == "free" ||
1910 Name == "fseek" ||
1911 Name == "ftell" ||
1912 Name == "fgetc" ||
1913 Name == "fseeko" ||
1914 Name == "ftello" ||
1915 Name == "fileno" ||
1916 Name == "fflush" ||
1917 Name == "fclose" ||
1918 Name == "fsetpos" ||
1919 Name == "flockfile" ||
1920 Name == "funlockfile" ||
1921 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001922 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001923 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001924 continue;
1925 setDoesNotThrow(F);
1926 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001927 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001928 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001929 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001930 continue;
1931 setDoesNotThrow(F);
1932 setDoesNotCapture(F, 1);
1933 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001934 } else if (Name == "fputc" ||
1935 Name == "fstat" ||
1936 Name == "frexp" ||
1937 Name == "frexpf" ||
1938 Name == "frexpl" ||
1939 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001940 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001941 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001942 continue;
1943 setDoesNotThrow(F);
1944 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001945 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001946 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001947 !FTy->getParamType(0)->isPointerTy() ||
1948 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001949 continue;
1950 setDoesNotThrow(F);
1951 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001952 } else if (Name == "fread" ||
1953 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001954 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001955 !FTy->getParamType(0)->isPointerTy() ||
1956 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001957 continue;
1958 setDoesNotThrow(F);
1959 setDoesNotCapture(F, 1);
1960 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001961 } else if (Name == "fputs" ||
1962 Name == "fscanf" ||
1963 Name == "fprintf" ||
1964 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001965 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001966 !FTy->getParamType(0)->isPointerTy() ||
1967 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001968 continue;
1969 setDoesNotThrow(F);
1970 setDoesNotCapture(F, 1);
1971 setDoesNotCapture(F, 2);
1972 }
1973 break;
1974 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001975 if (Name == "getc" ||
1976 Name == "getlogin_r" ||
1977 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001978 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001979 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001980 continue;
1981 setDoesNotThrow(F);
1982 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001983 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001984 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001985 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001986 continue;
1987 setDoesNotThrow(F);
1988 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001989 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001990 } else if (Name == "gets" ||
1991 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001992 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001993 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001994 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001995 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001996 continue;
1997 setDoesNotThrow(F);
1998 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001999 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002000 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002001 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002002 continue;
2003 setDoesNotThrow(F);
2004 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002005 }
2006 break;
2007 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002008 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002009 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002010 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002011 continue;
2012 setDoesNotThrow(F);
2013 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002014 } else if (Name == "uname" ||
2015 Name == "unlink" ||
2016 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002017 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002018 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002019 continue;
2020 setDoesNotThrow(F);
2021 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002022 } else if (Name == "utime" ||
2023 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002024 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002025 !FTy->getParamType(0)->isPointerTy() ||
2026 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002027 continue;
2028 setDoesNotThrow(F);
2029 setDoesNotCapture(F, 1);
2030 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002031 }
2032 break;
2033 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002034 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002035 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002036 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002037 continue;
2038 setDoesNotThrow(F);
2039 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002040 } else if (Name == "puts" ||
2041 Name == "printf" ||
2042 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002043 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002044 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002045 continue;
2046 setDoesNotThrow(F);
2047 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002048 } else if (Name == "pread" ||
2049 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002050 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002051 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002052 continue;
2053 // May throw; these are valid pthread cancellation points.
2054 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002055 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002056 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002057 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002058 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002059 !FTy->getReturnType()->isPointerTy() ||
2060 !FTy->getParamType(0)->isPointerTy() ||
2061 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002062 continue;
2063 setDoesNotThrow(F);
2064 setDoesNotAlias(F, 0);
2065 setDoesNotCapture(F, 1);
2066 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002067 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002068 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002069 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002070 continue;
2071 setDoesNotThrow(F);
2072 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002073 }
2074 break;
2075 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002076 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002077 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002078 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002079 continue;
2080 setDoesNotThrow(F);
2081 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002082 } else if (Name == "vsscanf" ||
2083 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002084 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002085 !FTy->getParamType(1)->isPointerTy() ||
2086 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002087 continue;
2088 setDoesNotThrow(F);
2089 setDoesNotCapture(F, 1);
2090 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002091 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00002092 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002093 continue;
2094 setDoesNotThrow(F);
2095 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002096 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002097 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002098 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002099 continue;
2100 setDoesNotThrow(F);
2101 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002102 } else if (Name == "vfprintf" ||
2103 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002104 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002105 !FTy->getParamType(0)->isPointerTy() ||
2106 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002107 continue;
2108 setDoesNotThrow(F);
2109 setDoesNotCapture(F, 1);
2110 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002111 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002112 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002113 !FTy->getParamType(0)->isPointerTy() ||
2114 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002115 continue;
2116 setDoesNotThrow(F);
2117 setDoesNotCapture(F, 1);
2118 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002119 }
2120 break;
2121 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002122 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002123 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002124 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002125 continue;
2126 // May throw; "open" is a valid pthread cancellation point.
2127 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002128 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002129 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002130 !FTy->getReturnType()->isPointerTy() ||
2131 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002132 continue;
2133 setDoesNotThrow(F);
2134 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002135 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002136 }
2137 break;
2138 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002139 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00002140 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002141 continue;
2142 setDoesNotThrow(F);
2143 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002144 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002145 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002146 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002147 continue;
2148 setDoesNotThrow(F);
2149 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002150 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002151 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002152 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002153 if (Name == "htonl" ||
2154 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002155 setDoesNotThrow(F);
2156 setDoesNotAccessMemory(F);
2157 }
2158 break;
2159 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002160 if (Name == "ntohl" ||
2161 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002162 setDoesNotThrow(F);
2163 setDoesNotAccessMemory(F);
2164 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002165 break;
2166 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002167 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002168 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002169 !FTy->getParamType(0)->isPointerTy() ||
2170 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002171 continue;
2172 setDoesNotThrow(F);
2173 setDoesNotCapture(F, 1);
2174 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002175 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002176 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002177 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002178 continue;
2179 setDoesNotThrow(F);
2180 setDoesNotCapture(F, 1);
2181 }
2182 break;
2183 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002184 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002185 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002186 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002187 continue;
2188 // May throw; places call through function pointer.
2189 setDoesNotCapture(F, 4);
2190 }
2191 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002192 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002193 if (Name == "__strdup" ||
2194 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002195 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002196 !FTy->getReturnType()->isPointerTy() ||
2197 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002198 continue;
2199 setDoesNotThrow(F);
2200 setDoesNotAlias(F, 0);
2201 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002202 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002203 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002204 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002205 continue;
2206 setDoesNotThrow(F);
2207 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002208 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002209 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002210 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002211 continue;
2212 setDoesNotThrow(F);
2213 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002214 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002215 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002216 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002217 continue;
2218 setDoesNotThrow(F);
2219 setDoesNotCapture(F, 2);
2220 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002221 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002222 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002223 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002224 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002225 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002226 continue;
2227 setDoesNotThrow(F);
2228 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002229 } else if (Name == "\1stat64" ||
2230 Name == "\1lstat64" ||
2231 Name == "\1statvfs64" ||
2232 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002233 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002234 !FTy->getParamType(0)->isPointerTy() ||
2235 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002236 continue;
2237 setDoesNotThrow(F);
2238 setDoesNotCapture(F, 1);
2239 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002240 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002241 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002242 !FTy->getReturnType()->isPointerTy() ||
2243 !FTy->getParamType(0)->isPointerTy() ||
2244 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002245 continue;
2246 setDoesNotThrow(F);
2247 setDoesNotAlias(F, 0);
2248 setDoesNotCapture(F, 1);
2249 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002250 } else if (Name == "\1fseeko64" ||
2251 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002252 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002253 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002254 continue;
2255 setDoesNotThrow(F);
2256 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002257 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002258 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002259 continue;
2260 setDoesNotThrow(F);
2261 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002262 } else if (Name == "\1fstat64" ||
2263 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002264 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002265 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002266 continue;
2267 setDoesNotThrow(F);
2268 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002269 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002270 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002271 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002272 continue;
2273 // May throw; "open" is a valid pthread cancellation point.
2274 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002275 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002276 break;
2277 }
2278 }
2279 return Modified;
2280}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002281
2282// TODO:
2283// Additional cases that we need to add to this file:
2284//
2285// cbrt:
2286// * cbrt(expN(X)) -> expN(x/3)
2287// * cbrt(sqrt(x)) -> pow(x,1/6)
2288// * cbrt(sqrt(x)) -> pow(x,1/9)
2289//
2290// cos, cosf, cosl:
2291// * cos(-x) -> cos(x)
2292//
2293// exp, expf, expl:
2294// * exp(log(x)) -> x
2295//
2296// log, logf, logl:
2297// * log(exp(x)) -> x
2298// * log(x**y) -> y*log(x)
2299// * log(exp(y)) -> y*log(e)
2300// * log(exp2(y)) -> y*log(2)
2301// * log(exp10(y)) -> y*log(10)
2302// * log(sqrt(x)) -> 0.5*log(x)
2303// * log(pow(x,y)) -> y*log(x)
2304//
2305// lround, lroundf, lroundl:
2306// * lround(cnst) -> cnst'
2307//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002308// pow, powf, powl:
2309// * pow(exp(x),y) -> exp(x*y)
2310// * pow(sqrt(x),y) -> pow(x,y*0.5)
2311// * pow(pow(x,y),z)-> pow(x,y*z)
2312//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002313// round, roundf, roundl:
2314// * round(cnst) -> cnst'
2315//
2316// signbit:
2317// * signbit(cnst) -> cnst'
2318// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2319//
2320// sqrt, sqrtf, sqrtl:
2321// * sqrt(expN(x)) -> expN(x*0.5)
2322// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2323// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2324//
2325// stpcpy:
2326// * stpcpy(str, "literal") ->
2327// llvm.memcpy(str,"literal",strlen("literal")+1,1)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002328//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002329// tan, tanf, tanl:
2330// * tan(atan(x)) -> x
2331//
2332// trunc, truncf, truncl:
2333// * trunc(cnst) -> cnst'
2334//
2335//