blob: 86ddeac13ee6e9f6ae054f21612df02e34135dcd [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:
53 LibCallOptimization() { }
54 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();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000069 return CallOptimizer(CI->getCalledFunction(), CI, B);
70 }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000071};
72} // End anonymous namespace.
73
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000074
75//===----------------------------------------------------------------------===//
76// Helper Functions
77//===----------------------------------------------------------------------===//
78
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000079/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +000080/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000081static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
82 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
83 UI != E; ++UI) {
84 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
85 if (IC->isEquality())
86 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
87 if (C->isNullValue())
88 continue;
89 // Unknown instruction.
90 return false;
91 }
92 return true;
93}
94
95//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000096// String and Memory LibCall Optimizations
97//===----------------------------------------------------------------------===//
98
99//===---------------------------------------===//
100// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000101namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000102struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000103 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000104 // Verify the "strcat" function prototype.
105 const FunctionType *FT = Callee->getFunctionType();
106 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000107 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000108 FT->getParamType(0) != FT->getReturnType() ||
109 FT->getParamType(1) != FT->getReturnType())
110 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000111
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000112 // Extract some information from the instruction
113 Value *Dst = CI->getOperand(1);
114 Value *Src = CI->getOperand(2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000115
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000116 // See if we can get the length of the input string.
117 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000118 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000119 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000120
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000121 // Handle the simple, do-nothing case: strcat(x, "") -> x
122 if (Len == 0)
123 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000124
125 // These optimizations require TargetData.
126 if (!TD) return 0;
127
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000128 EmitStrLenMemCpy(Src, Dst, Len, B);
129 return Dst;
130 }
131
132 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000133 // We need to find the end of the destination string. That's where the
134 // memory is to be moved to. We just generate a call to strlen.
Eric Christopherb6174e32010-03-05 22:25:30 +0000135 Value *DstLen = EmitStrLen(Dst, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000136
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000137 // Now that we have the destination's length, we must index into the
138 // destination's pointer to get the actual memcpy destination (end of
139 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000140 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000141
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000142 // We have enough information to now generate the memcpy call to do the
143 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000144 EmitMemCpy(CpyDst, Src,
Eric Christopherb6174e32010-03-05 22:25:30 +0000145 ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000146 }
147};
148
149//===---------------------------------------===//
150// 'strncat' Optimizations
151
Chris Lattner3e8b6632009-09-02 06:11:42 +0000152struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000153 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
154 // Verify the "strncat" function prototype.
155 const FunctionType *FT = Callee->getFunctionType();
156 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000157 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000158 FT->getParamType(0) != FT->getReturnType() ||
159 FT->getParamType(1) != FT->getReturnType() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000160 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000161 return 0;
162
163 // Extract some information from the instruction
164 Value *Dst = CI->getOperand(1);
165 Value *Src = CI->getOperand(2);
166 uint64_t Len;
167
168 // We don't do anything if length is not constant
169 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
170 Len = LengthArg->getZExtValue();
171 else
172 return 0;
173
174 // See if we can get the length of the input string.
175 uint64_t SrcLen = GetStringLength(Src);
176 if (SrcLen == 0) return 0;
177 --SrcLen; // Unbias length.
178
179 // Handle the simple, do-nothing cases:
180 // strncat(x, "", c) -> x
181 // strncat(x, c, 0) -> x
182 if (SrcLen == 0 || Len == 0) return Dst;
183
Dan Gohmanf14d9192009-08-18 00:48:13 +0000184 // These optimizations require TargetData.
185 if (!TD) return 0;
186
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000187 // We don't optimize this case
188 if (Len < SrcLen) return 0;
189
190 // strncat(x, s, c) -> strcat(x, s)
191 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000192 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000193 return Dst;
194 }
195};
196
197//===---------------------------------------===//
198// 'strchr' Optimizations
199
Chris Lattner3e8b6632009-09-02 06:11:42 +0000200struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000201 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000202 // Verify the "strchr" function prototype.
203 const FunctionType *FT = Callee->getFunctionType();
204 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000205 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000206 FT->getParamType(0) != FT->getReturnType())
207 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000208
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000209 Value *SrcStr = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000210
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000211 // If the second operand is non-constant, see if we can compute the length
212 // of the input string and turn this into memchr.
213 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
214 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000215 // These optimizations require TargetData.
216 if (!TD) return 0;
217
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000218 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000219 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000220 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000221
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000222 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000223 ConstantInt::get(TD->getIntPtrType(*Context), Len),
224 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000225 }
226
227 // Otherwise, the character is a constant, see if the first argument is
228 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000229 std::string Str;
230 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000231 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000232
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000233 // strchr can find the nul character.
234 Str += '\0';
235 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000236
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000237 // Compute the offset.
238 uint64_t i = 0;
239 while (1) {
240 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000241 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000242 // Did we find our match?
243 if (Str[i] == CharValue)
244 break;
245 ++i;
246 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000247
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000248 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000249 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000250 return B.CreateGEP(SrcStr, Idx, "strchr");
251 }
252};
253
254//===---------------------------------------===//
255// 'strcmp' Optimizations
256
Chris Lattner3e8b6632009-09-02 06:11:42 +0000257struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000258 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000259 // Verify the "strcmp" function prototype.
260 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000261 if (FT->getNumParams() != 2 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000262 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000263 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000264 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000265 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000266
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000267 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
268 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000269 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000270
Bill Wendling0582ae92009-03-13 04:39:26 +0000271 std::string Str1, Str2;
272 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
273 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000274
Bill Wendling0582ae92009-03-13 04:39:26 +0000275 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000276 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000277
Bill Wendling0582ae92009-03-13 04:39:26 +0000278 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000279 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000280
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000281 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000282 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000283 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000284 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000285
286 // strcmp(P, "x") -> memcmp(P, "x", 2)
287 uint64_t Len1 = GetStringLength(Str1P);
288 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000289 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000290 // These optimizations require TargetData.
291 if (!TD) return 0;
292
Nick Lewycky13a09e22008-12-21 00:19:21 +0000293 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000294 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000295 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000296 }
297
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000298 return 0;
299 }
300};
301
302//===---------------------------------------===//
303// 'strncmp' Optimizations
304
Chris Lattner3e8b6632009-09-02 06:11:42 +0000305struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000306 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000307 // Verify the "strncmp" function prototype.
308 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000309 if (FT->getNumParams() != 3 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000310 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000311 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000312 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000313 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000314 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000315
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000316 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
317 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000318 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000319
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000320 // Get the length argument if it is constant.
321 uint64_t Length;
322 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
323 Length = LengthArg->getZExtValue();
324 else
325 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000326
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000327 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000328 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000329
Bill Wendling0582ae92009-03-13 04:39:26 +0000330 std::string Str1, Str2;
331 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
332 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000333
Bill Wendling0582ae92009-03-13 04:39:26 +0000334 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000335 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000336
Bill Wendling0582ae92009-03-13 04:39:26 +0000337 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000338 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000339
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000340 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000341 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000342 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000343 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000344 return 0;
345 }
346};
347
348
349//===---------------------------------------===//
350// 'strcpy' Optimizations
351
Chris Lattner3e8b6632009-09-02 06:11:42 +0000352struct StrCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000353 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000354 // Verify the "strcpy" function prototype.
355 const FunctionType *FT = Callee->getFunctionType();
356 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
357 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000358 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000359 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000360
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000361 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
362 if (Dst == Src) // strcpy(x,x) -> x
363 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000364
Dan Gohmanf14d9192009-08-18 00:48:13 +0000365 // These optimizations require TargetData.
366 if (!TD) return 0;
367
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000368 // See if we can get the length of the input string.
369 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000370 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000371
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000372 // We have enough information to now generate the memcpy call to do the
373 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000374 EmitMemCpy(Dst, Src,
Eric Christopherb6174e32010-03-05 22:25:30 +0000375 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000376 return Dst;
377 }
378};
379
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000380//===---------------------------------------===//
381// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000382
Chris Lattner3e8b6632009-09-02 06:11:42 +0000383struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000384 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
385 const FunctionType *FT = Callee->getFunctionType();
386 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
387 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000388 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000389 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000390 return 0;
391
392 Value *Dst = CI->getOperand(1);
393 Value *Src = CI->getOperand(2);
394 Value *LenOp = CI->getOperand(3);
395
396 // See if we can get the length of the input string.
397 uint64_t SrcLen = GetStringLength(Src);
398 if (SrcLen == 0) return 0;
399 --SrcLen;
400
401 if (SrcLen == 0) {
402 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000403 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
Eric Christopherb6174e32010-03-05 22:25:30 +0000404 B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000405 return Dst;
406 }
407
408 uint64_t Len;
409 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
410 Len = LengthArg->getZExtValue();
411 else
412 return 0;
413
414 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
415
Dan Gohmanf14d9192009-08-18 00:48:13 +0000416 // These optimizations require TargetData.
417 if (!TD) return 0;
418
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000419 // Let strncpy handle the zero padding
420 if (Len > SrcLen+1) return 0;
421
422 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000423 EmitMemCpy(Dst, Src,
Eric Christopherb6174e32010-03-05 22:25:30 +0000424 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000425
426 return Dst;
427 }
428};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000429
430//===---------------------------------------===//
431// 'strlen' Optimizations
432
Chris Lattner3e8b6632009-09-02 06:11:42 +0000433struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000434 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000435 const FunctionType *FT = Callee->getFunctionType();
436 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000437 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000438 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000439 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000440
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000441 Value *Src = CI->getOperand(1);
442
443 // Constant folding: strlen("xyz") -> 3
444 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000445 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000446
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000447 // strlen(x) != 0 --> *x != 0
448 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000449 if (IsOnlyUsedInZeroEqualityComparison(CI))
450 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
451 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000452 }
453};
454
455//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000456// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000457
Chris Lattner3e8b6632009-09-02 06:11:42 +0000458struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000459 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
460 const FunctionType *FT = Callee->getFunctionType();
461 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000462 !FT->getParamType(0)->isPointerTy() ||
463 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000464 return 0;
465
466 Value *EndPtr = CI->getOperand(2);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000467 if (isa<ConstantPointerNull>(EndPtr)) {
468 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000469 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000470 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000471
472 return 0;
473 }
474};
475
Chris Lattner24604112009-12-16 09:32:05 +0000476//===---------------------------------------===//
477// 'strstr' Optimizations
478
479struct StrStrOpt : public LibCallOptimization {
480 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
481 const FunctionType *FT = Callee->getFunctionType();
482 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000483 !FT->getParamType(0)->isPointerTy() ||
484 !FT->getParamType(1)->isPointerTy() ||
485 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000486 return 0;
487
488 // fold strstr(x, x) -> x.
489 if (CI->getOperand(1) == CI->getOperand(2))
490 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000491
Chris Lattner24604112009-12-16 09:32:05 +0000492 // See if either input string is a constant string.
493 std::string SearchStr, ToFindStr;
494 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
495 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000496
Chris Lattner24604112009-12-16 09:32:05 +0000497 // fold strstr(x, "") -> x.
498 if (HasStr2 && ToFindStr.empty())
499 return B.CreateBitCast(CI->getOperand(1), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000500
Chris Lattner24604112009-12-16 09:32:05 +0000501 // If both strings are known, constant fold it.
502 if (HasStr1 && HasStr2) {
503 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000504
Chris Lattner24604112009-12-16 09:32:05 +0000505 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
506 return Constant::getNullValue(CI->getType());
507
508 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
509 Value *Result = CastToCStr(CI->getOperand(1), B);
510 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
511 return B.CreateBitCast(Result, CI->getType());
512 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000513
Chris Lattner24604112009-12-16 09:32:05 +0000514 // fold strstr(x, "y") -> strchr(x, 'y').
515 if (HasStr2 && ToFindStr.size() == 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000516 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B, TD),
Chris Lattner24604112009-12-16 09:32:05 +0000517 CI->getType());
518 return 0;
519 }
520};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000521
Nick Lewycky4c498412009-02-13 15:31:46 +0000522
523//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000524// 'memcmp' Optimizations
525
Chris Lattner3e8b6632009-09-02 06:11:42 +0000526struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000527 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000528 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000529 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
530 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000531 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000532 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000533
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000534 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000535
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000536 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000537 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000538
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000539 // Make sure we have a constant length.
540 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000541 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000542 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000543
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000544 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000545 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000546
547 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
548 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
549 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
Chris Lattner0e98e4d2009-05-30 18:43:04 +0000550 return B.CreateSExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000551 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000552
Benjamin Kramer992a6372009-11-05 17:44:22 +0000553 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
554 std::string LHSStr, RHSStr;
555 if (GetConstantStringInfo(LHS, LHSStr) &&
556 GetConstantStringInfo(RHS, RHSStr)) {
557 // Make sure we're not reading out-of-bounds memory.
558 if (Len > LHSStr.length() || Len > RHSStr.length())
559 return 0;
560 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
561 return ConstantInt::get(CI->getType(), Ret);
562 }
563
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000564 return 0;
565 }
566};
567
568//===---------------------------------------===//
569// 'memcpy' Optimizations
570
Chris Lattner3e8b6632009-09-02 06:11:42 +0000571struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000572 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000573 // These optimizations require TargetData.
574 if (!TD) return 0;
575
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000576 const FunctionType *FT = Callee->getFunctionType();
577 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000578 !FT->getParamType(0)->isPointerTy() ||
579 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000580 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000581 return 0;
582
583 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000584 EmitMemCpy(CI->getOperand(1), CI->getOperand(2),
585 CI->getOperand(3), 1, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000586 return CI->getOperand(1);
587 }
588};
589
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000590//===---------------------------------------===//
591// 'memmove' Optimizations
592
Chris Lattner3e8b6632009-09-02 06:11:42 +0000593struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000594 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000595 // These optimizations require TargetData.
596 if (!TD) return 0;
597
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000598 const FunctionType *FT = Callee->getFunctionType();
599 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000600 !FT->getParamType(0)->isPointerTy() ||
601 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000602 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000603 return 0;
604
605 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Eric Christopherb6174e32010-03-05 22:25:30 +0000606 EmitMemMove(CI->getOperand(1), CI->getOperand(2),
607 CI->getOperand(3), 1, B, TD);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000608 return CI->getOperand(1);
609 }
610};
611
612//===---------------------------------------===//
613// 'memset' Optimizations
614
Chris Lattner3e8b6632009-09-02 06:11:42 +0000615struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000616 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000617 // These optimizations require TargetData.
618 if (!TD) return 0;
619
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000620 const FunctionType *FT = Callee->getFunctionType();
621 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000622 !FT->getParamType(0)->isPointerTy() ||
623 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000624 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000625 return 0;
626
627 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Eric Christopher37c8b862009-10-07 21:14:25 +0000628 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
629 false);
Eric Christopherb6174e32010-03-05 22:25:30 +0000630 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B, TD);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000631 return CI->getOperand(1);
632 }
633};
634
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000635//===----------------------------------------------------------------------===//
Eric Christopher80bf1d52009-11-21 01:01:30 +0000636// Object Size Checking Optimizations
637//===----------------------------------------------------------------------===//
638
639//===---------------------------------------===//
Eric Christopher80bf1d52009-11-21 01:01:30 +0000640// 'memcpy_chk' Optimizations
641
642struct MemCpyChkOpt : public LibCallOptimization {
643 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
644 // These optimizations require TargetData.
645 if (!TD) return 0;
646
647 const FunctionType *FT = Callee->getFunctionType();
648 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000649 !FT->getParamType(0)->isPointerTy() ||
650 !FT->getParamType(1)->isPointerTy() ||
651 !FT->getParamType(3)->isIntegerTy() ||
Eric Christopherf734be22009-12-22 01:23:51 +0000652 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eric Christopher80bf1d52009-11-21 01:01:30 +0000653 return 0;
654
Evan Chenga79eb382010-03-05 20:59:47 +0000655 ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
656 if (!ObjSizeCI)
Eric Christopher80bf1d52009-11-21 01:01:30 +0000657 return 0;
Evan Chenga79eb382010-03-05 20:59:47 +0000658 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
659 if (ObjSizeCI->isAllOnesValue() ||
660 (SizeCI && ObjSizeCI->getValue().uge(SizeCI->getValue()))) {
Eric Christopherb6174e32010-03-05 22:25:30 +0000661 EmitMemCpy(CI->getOperand(1), CI->getOperand(2),
662 CI->getOperand(3), 1, B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000663 return CI->getOperand(1);
664 }
665
666 return 0;
667 }
668};
669
670//===---------------------------------------===//
671// 'memset_chk' Optimizations
672
673struct MemSetChkOpt : public LibCallOptimization {
674 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
675 // These optimizations require TargetData.
676 if (!TD) return 0;
677
678 const FunctionType *FT = Callee->getFunctionType();
679 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000680 !FT->getParamType(0)->isPointerTy() ||
681 !FT->getParamType(1)->isIntegerTy() ||
682 !FT->getParamType(3)->isIntegerTy() ||
Eric Christopher80bf1d52009-11-21 01:01:30 +0000683 FT->getParamType(2) != TD->getIntPtrType(*Context))
684 return 0;
685
Evan Chenga79eb382010-03-05 20:59:47 +0000686 ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
687 if (!ObjSizeCI)
Eric Christopher80bf1d52009-11-21 01:01:30 +0000688 return 0;
Evan Chenga79eb382010-03-05 20:59:47 +0000689 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
690 if (ObjSizeCI->isAllOnesValue() ||
691 (SizeCI && ObjSizeCI->getValue().uge(SizeCI->getValue()))) {
Eric Christopher80bf1d52009-11-21 01:01:30 +0000692 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
693 false);
Eric Christopherb6174e32010-03-05 22:25:30 +0000694 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000695 return CI->getOperand(1);
696 }
697
698 return 0;
699 }
700};
701
702//===---------------------------------------===//
703// 'memmove_chk' Optimizations
704
705struct MemMoveChkOpt : public LibCallOptimization {
706 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
707 // These optimizations require TargetData.
708 if (!TD) return 0;
709
710 const FunctionType *FT = Callee->getFunctionType();
711 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000712 !FT->getParamType(0)->isPointerTy() ||
713 !FT->getParamType(1)->isPointerTy() ||
714 !FT->getParamType(3)->isIntegerTy() ||
Eric Christopher80bf1d52009-11-21 01:01:30 +0000715 FT->getParamType(2) != TD->getIntPtrType(*Context))
716 return 0;
717
Evan Chenga79eb382010-03-05 20:59:47 +0000718 ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
719 if (!ObjSizeCI)
Eric Christopher80bf1d52009-11-21 01:01:30 +0000720 return 0;
Evan Chenga79eb382010-03-05 20:59:47 +0000721 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
722 if (ObjSizeCI->isAllOnesValue() ||
723 (SizeCI && ObjSizeCI->getValue().uge(SizeCI->getValue()))) {
Eric Christopher80bf1d52009-11-21 01:01:30 +0000724 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
Eric Christopherb6174e32010-03-05 22:25:30 +0000725 1, B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +0000726 return CI->getOperand(1);
727 }
728
729 return 0;
730 }
731};
732
Eric Christopher7672d042010-01-23 05:29:06 +0000733struct StrCpyChkOpt : public LibCallOptimization {
734 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Eric Christopher7672d042010-01-23 05:29:06 +0000735 const FunctionType *FT = Callee->getFunctionType();
736 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000737 !FT->getParamType(0)->isPointerTy() ||
738 !FT->getParamType(1)->isPointerTy())
Eric Christopher7672d042010-01-23 05:29:06 +0000739 return 0;
740
Evan Chenga79eb382010-03-05 20:59:47 +0000741 ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
742 if (!ObjSizeCI)
Eric Christopher7672d042010-01-23 05:29:06 +0000743 return 0;
744
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000745 // If a) we don't have any length information, or b) we know this will
746 // fit then just lower to a plain strcpy. Otherwise we'll keep our
747 // strcpy_chk call which may fail at runtime if the size is too long.
748 // TODO: It might be nice to get a maximum length out of the possible
749 // string lengths for varying.
Evan Chenga79eb382010-03-05 20:59:47 +0000750 if (ObjSizeCI->isAllOnesValue() ||
751 ObjSizeCI->getZExtValue() >= GetStringLength(CI->getOperand(2)))
Eric Christopherb6174e32010-03-05 22:25:30 +0000752 return EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B, TD);
Eric Christopher7672d042010-01-23 05:29:06 +0000753
754 return 0;
755 }
756};
757
758
Eric Christopher80bf1d52009-11-21 01:01:30 +0000759//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000760// Math Library Optimizations
761//===----------------------------------------------------------------------===//
762
763//===---------------------------------------===//
764// 'pow*' Optimizations
765
Chris Lattner3e8b6632009-09-02 06:11:42 +0000766struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000767 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000768 const FunctionType *FT = Callee->getFunctionType();
769 // Just make sure this has 2 arguments of the same FP type, which match the
770 // result type.
771 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
772 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000773 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000774 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000775
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000776 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
777 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
778 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
779 return Op1C;
780 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000781 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000782 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000783
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000784 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
785 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000786
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000787 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000788 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000789
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000790 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000791 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
792 // This is faster than calling pow, and still handles negative zero
793 // and negative infinite correctly.
794 // TODO: In fast-math mode, this could be just sqrt(x).
795 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000796 Value *Inf = ConstantFP::getInfinity(CI->getType());
797 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000798 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
799 Callee->getAttributes());
800 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
801 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000802 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
803 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
804 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000805 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000806
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000807 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
808 return Op1;
809 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000810 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000811 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000812 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000813 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000814 return 0;
815 }
816};
817
818//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000819// 'exp2' Optimizations
820
Chris Lattner3e8b6632009-09-02 06:11:42 +0000821struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000822 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000823 const FunctionType *FT = Callee->getFunctionType();
824 // Just make sure this has 1 argument of FP type, which matches the
825 // result type.
826 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000827 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000828 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000829
Chris Lattnere818f772008-05-02 18:43:35 +0000830 Value *Op = CI->getOperand(1);
831 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
832 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
833 Value *LdExpArg = 0;
834 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
835 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000836 LdExpArg = B.CreateSExt(OpC->getOperand(0),
837 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000838 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
839 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000840 LdExpArg = B.CreateZExt(OpC->getOperand(0),
841 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000842 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000843
Chris Lattnere818f772008-05-02 18:43:35 +0000844 if (LdExpArg) {
845 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000846 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000847 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000848 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000849 Name = "ldexp";
850 else
851 Name = "ldexpl";
852
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000853 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000854 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000855 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000856
857 Module *M = Caller->getParent();
858 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000859 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000860 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000861 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
862 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
863 CI->setCallingConv(F->getCallingConv());
864
865 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000866 }
867 return 0;
868 }
869};
Chris Lattnere818f772008-05-02 18:43:35 +0000870
871//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000872// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
873
Chris Lattner3e8b6632009-09-02 06:11:42 +0000874struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000875 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000876 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000877 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
878 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000879 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000880
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000881 // If this is something like 'floor((double)floatval)', convert to floorf.
882 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000883 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000884 return 0;
885
886 // floor((double)floatval) -> (double)floorf(floatval)
887 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000888 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
889 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000890 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000891 }
892};
893
894//===----------------------------------------------------------------------===//
895// Integer Optimizations
896//===----------------------------------------------------------------------===//
897
898//===---------------------------------------===//
899// 'ffs*' Optimizations
900
Chris Lattner3e8b6632009-09-02 06:11:42 +0000901struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000902 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000903 const FunctionType *FT = Callee->getFunctionType();
904 // Just make sure this has 2 arguments of the same FP type, which match the
905 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000906 if (FT->getNumParams() != 1 ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000907 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000908 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000909 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000910
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000911 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000912
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000913 // Constant fold.
914 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
915 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000916 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000917 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000918 CI->getValue().countTrailingZeros()+1);
919 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000920
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000921 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
922 const Type *ArgType = Op->getType();
923 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
924 Intrinsic::cttz, &ArgType, 1);
925 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000926 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000927 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000928
Owen Andersona7235ea2009-07-31 20:28:14 +0000929 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000930 return B.CreateSelect(Cond, V,
931 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000932 }
933};
934
935//===---------------------------------------===//
936// 'isdigit' Optimizations
937
Chris Lattner3e8b6632009-09-02 06:11:42 +0000938struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000939 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000940 const FunctionType *FT = Callee->getFunctionType();
941 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000942 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000943 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000944 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000945
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000946 // isdigit(c) -> (c-'0') <u 10
947 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000948 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000949 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000950 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000951 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000952 return B.CreateZExt(Op, CI->getType());
953 }
954};
955
956//===---------------------------------------===//
957// 'isascii' Optimizations
958
Chris Lattner3e8b6632009-09-02 06:11:42 +0000959struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000960 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000961 const FunctionType *FT = Callee->getFunctionType();
962 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000963 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000964 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000965 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000966
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000967 // isascii(c) -> c <u 128
968 Value *Op = CI->getOperand(1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000969 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000970 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000971 return B.CreateZExt(Op, CI->getType());
972 }
973};
Eric Christopher37c8b862009-10-07 21:14:25 +0000974
Chris Lattner313f0e62008-06-09 08:26:51 +0000975//===---------------------------------------===//
976// 'abs', 'labs', 'llabs' Optimizations
977
Chris Lattner3e8b6632009-09-02 06:11:42 +0000978struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000979 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +0000980 const FunctionType *FT = Callee->getFunctionType();
981 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +0000982 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +0000983 FT->getParamType(0) != FT->getReturnType())
984 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000985
Chris Lattner313f0e62008-06-09 08:26:51 +0000986 // abs(x) -> x >s -1 ? x : -x
987 Value *Op = CI->getOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000988 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +0000989 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +0000990 "ispos");
991 Value *Neg = B.CreateNeg(Op, "neg");
992 return B.CreateSelect(Pos, Op, Neg);
993 }
994};
Eric Christopher37c8b862009-10-07 21:14:25 +0000995
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000996
997//===---------------------------------------===//
998// 'toascii' Optimizations
999
Chris Lattner3e8b6632009-09-02 06:11:42 +00001000struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001001 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001002 const FunctionType *FT = Callee->getFunctionType();
1003 // We require i32(i32)
1004 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001005 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001006 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001007
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001008 // isascii(c) -> c & 0x7f
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001009 return B.CreateAnd(CI->getOperand(1),
Owen Andersoneed707b2009-07-24 23:12:02 +00001010 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001011 }
1012};
1013
1014//===----------------------------------------------------------------------===//
1015// Formatting and IO Optimizations
1016//===----------------------------------------------------------------------===//
1017
1018//===---------------------------------------===//
1019// 'printf' Optimizations
1020
Chris Lattner3e8b6632009-09-02 06:11:42 +00001021struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001022 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001023 // Require one fixed pointer argument and an integer/void result.
1024 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001025 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1026 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001027 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001028 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001029
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001030 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001031 std::string FormatStr;
1032 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1033 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001034
1035 // Empty format string -> noop.
1036 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001037 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001038 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001039
Chris Lattner74965f22009-11-09 04:57:04 +00001040 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
1041 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001042 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +00001043 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +00001044 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +00001045 if (CI->use_empty()) return CI;
1046 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001047 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001048
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001049 // printf("foo\n") --> puts("foo")
1050 if (FormatStr[FormatStr.size()-1] == '\n' &&
1051 FormatStr.find('%') == std::string::npos) { // no format characters.
1052 // Create a string literal with no \n on it. We expect the constant merge
1053 // pass to be run after this pass, to merge duplicate strings.
1054 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001055 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001056 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1057 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +00001058 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001059 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001060 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001061 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001062
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001063 // Optimize specific format strings.
1064 // printf("%c", chr) --> putchar(*(i8*)dst)
1065 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
Duncan Sands1df98592010-02-16 11:11:14 +00001066 CI->getOperand(2)->getType()->isIntegerTy()) {
Eric Christopherb6174e32010-03-05 22:25:30 +00001067 Value *Res = EmitPutChar(CI->getOperand(2), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001068
Chris Lattner74965f22009-11-09 04:57:04 +00001069 if (CI->use_empty()) return CI;
1070 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001071 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001072
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001073 // printf("%s\n", str) --> puts(str)
1074 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
Duncan Sands1df98592010-02-16 11:11:14 +00001075 CI->getOperand(2)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001076 CI->use_empty()) {
Eric Christopherb6174e32010-03-05 22:25:30 +00001077 EmitPutS(CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001078 return CI;
1079 }
1080 return 0;
1081 }
1082};
1083
1084//===---------------------------------------===//
1085// 'sprintf' Optimizations
1086
Chris Lattner3e8b6632009-09-02 06:11:42 +00001087struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001088 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001089 // Require two fixed pointer arguments and an integer result.
1090 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001091 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1092 !FT->getParamType(1)->isPointerTy() ||
1093 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001094 return 0;
1095
1096 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001097 std::string FormatStr;
1098 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1099 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001100
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001101 // If we just have a format string (nothing else crazy) transform it.
1102 if (CI->getNumOperands() == 3) {
1103 // Make sure there's no % in the constant array. We could try to handle
1104 // %% -> % in the future if we cared.
1105 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1106 if (FormatStr[i] == '%')
1107 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001108
1109 // These optimizations require TargetData.
1110 if (!TD) return 0;
1111
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001112 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1113 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
Eric Christopherb6174e32010-03-05 22:25:30 +00001114 ConstantInt::get(TD->getIntPtrType(*Context),
1115 FormatStr.size()+1), 1, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001116 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001117 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001118
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001119 // The remaining optimizations require the format string to be "%s" or "%c"
1120 // and have an extra operand.
1121 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1122 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001123
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001124 // Decode the second character of the format string.
1125 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001126 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Duncan Sands1df98592010-02-16 11:11:14 +00001127 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001128 Value *V = B.CreateTrunc(CI->getOperand(3),
1129 Type::getInt8Ty(*Context), "char");
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001130 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1131 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001132 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1133 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001134 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001135
Owen Andersoneed707b2009-07-24 23:12:02 +00001136 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001137 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001138
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001139 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001140 // These optimizations require TargetData.
1141 if (!TD) return 0;
1142
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001143 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Duncan Sands1df98592010-02-16 11:11:14 +00001144 if (!CI->getOperand(3)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001145
Eric Christopherb6174e32010-03-05 22:25:30 +00001146 Value *Len = EmitStrLen(CI->getOperand(3), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001147 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001148 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001149 "leninc");
Eric Christopherb6174e32010-03-05 22:25:30 +00001150 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001151
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001152 // The sprintf result is the unincremented number of bytes in the string.
1153 return B.CreateIntCast(Len, CI->getType(), false);
1154 }
1155 return 0;
1156 }
1157};
1158
1159//===---------------------------------------===//
1160// 'fwrite' Optimizations
1161
Chris Lattner3e8b6632009-09-02 06:11:42 +00001162struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001163 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001164 // Require a pointer, an integer, an integer, a pointer, returning integer.
1165 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001166 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1167 !FT->getParamType(1)->isIntegerTy() ||
1168 !FT->getParamType(2)->isIntegerTy() ||
1169 !FT->getParamType(3)->isPointerTy() ||
1170 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001171 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001172
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001173 // Get the element size and count.
1174 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1175 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1176 if (!SizeC || !CountC) return 0;
1177 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001178
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001179 // If this is writing zero records, remove the call (it's a noop).
1180 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001181 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001182
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001183 // If this is writing one byte, turn it into fputc.
1184 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1185 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
Eric Christopherb6174e32010-03-05 22:25:30 +00001186 EmitFPutC(Char, CI->getOperand(4), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001187 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001188 }
1189
1190 return 0;
1191 }
1192};
1193
1194//===---------------------------------------===//
1195// 'fputs' Optimizations
1196
Chris Lattner3e8b6632009-09-02 06:11:42 +00001197struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001198 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001199 // These optimizations require TargetData.
1200 if (!TD) return 0;
1201
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001202 // Require two pointers. Also, we can't optimize if return value is used.
1203 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001204 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1205 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001206 !CI->use_empty())
1207 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001208
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001209 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1210 uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001211 if (!Len) return 0;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001212 EmitFWrite(CI->getOperand(1),
Owen Anderson1d0be152009-08-13 21:58:54 +00001213 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Eric Christopherb6174e32010-03-05 22:25:30 +00001214 CI->getOperand(2), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001215 return CI; // Known to have no uses (see above).
1216 }
1217};
1218
1219//===---------------------------------------===//
1220// 'fprintf' Optimizations
1221
Chris Lattner3e8b6632009-09-02 06:11:42 +00001222struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001223 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001224 // Require two fixed paramters as pointers and integer result.
1225 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001226 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1227 !FT->getParamType(1)->isPointerTy() ||
1228 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001229 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001230
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001231 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001232 std::string FormatStr;
1233 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1234 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001235
1236 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1237 if (CI->getNumOperands() == 3) {
1238 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1239 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001240 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001241
1242 // These optimizations require TargetData.
1243 if (!TD) return 0;
1244
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001245 EmitFWrite(CI->getOperand(2),
1246 ConstantInt::get(TD->getIntPtrType(*Context),
1247 FormatStr.size()),
Eric Christopherb6174e32010-03-05 22:25:30 +00001248 CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001249 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001250 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001251
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001252 // The remaining optimizations require the format string to be "%s" or "%c"
1253 // and have an extra operand.
1254 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1255 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001256
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001257 // Decode the second character of the format string.
1258 if (FormatStr[1] == 'c') {
1259 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
Duncan Sands1df98592010-02-16 11:11:14 +00001260 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
Eric Christopherb6174e32010-03-05 22:25:30 +00001261 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001262 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001263 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001264
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001265 if (FormatStr[1] == 's') {
1266 // fprintf(F, "%s", str) -> fputs(str, F)
Duncan Sands1df98592010-02-16 11:11:14 +00001267 if (!CI->getOperand(3)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001268 return 0;
Eric Christopherb6174e32010-03-05 22:25:30 +00001269 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001270 return CI;
1271 }
1272 return 0;
1273 }
1274};
1275
Bill Wendlingac178222008-05-05 21:37:59 +00001276} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001277
1278//===----------------------------------------------------------------------===//
1279// SimplifyLibCalls Pass Implementation
1280//===----------------------------------------------------------------------===//
1281
1282namespace {
1283 /// This pass optimizes well known library functions from libc and libm.
1284 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001285 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001286 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001287 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001288 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1289 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001290 StrToOpt StrTo; StrStrOpt StrStr;
1291 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001292 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001293 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001294 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001295 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1296 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001297 // Formatting and IO Optimizations
1298 SPrintFOpt SPrintF; PrintFOpt PrintF;
1299 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001300
1301 // Object Size Checking
Eric Christopher80bf1d52009-11-21 01:01:30 +00001302 MemCpyChkOpt MemCpyChk; MemSetChkOpt MemSetChk; MemMoveChkOpt MemMoveChk;
Eric Christopher7672d042010-01-23 05:29:06 +00001303 StrCpyChkOpt StrCpyChk;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001304
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001305 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001306 public:
1307 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +00001308 SimplifyLibCalls() : FunctionPass(&ID) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001309
1310 void InitOptimizations();
1311 bool runOnFunction(Function &F);
1312
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001313 void setDoesNotAccessMemory(Function &F);
1314 void setOnlyReadsMemory(Function &F);
1315 void setDoesNotThrow(Function &F);
1316 void setDoesNotCapture(Function &F, unsigned n);
1317 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001318 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001319
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001320 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001321 }
1322 };
1323 char SimplifyLibCalls::ID = 0;
1324} // end anonymous namespace.
1325
1326static RegisterPass<SimplifyLibCalls>
1327X("simplify-libcalls", "Simplify well-known library calls");
1328
1329// Public interface to the Simplify LibCalls pass.
1330FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001331 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001332}
1333
1334/// Optimizations - Populate the Optimizations map with all the optimizations
1335/// we know.
1336void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001337 // String and Memory LibCall Optimizations
1338 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001339 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001340 Optimizations["strchr"] = &StrChr;
1341 Optimizations["strcmp"] = &StrCmp;
1342 Optimizations["strncmp"] = &StrNCmp;
1343 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001344 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001345 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001346 Optimizations["strtol"] = &StrTo;
1347 Optimizations["strtod"] = &StrTo;
1348 Optimizations["strtof"] = &StrTo;
1349 Optimizations["strtoul"] = &StrTo;
1350 Optimizations["strtoll"] = &StrTo;
1351 Optimizations["strtold"] = &StrTo;
1352 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001353 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001354 Optimizations["memcmp"] = &MemCmp;
1355 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001356 Optimizations["memmove"] = &MemMove;
1357 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001358
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001359 // Math Library Optimizations
1360 Optimizations["powf"] = &Pow;
1361 Optimizations["pow"] = &Pow;
1362 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001363 Optimizations["llvm.pow.f32"] = &Pow;
1364 Optimizations["llvm.pow.f64"] = &Pow;
1365 Optimizations["llvm.pow.f80"] = &Pow;
1366 Optimizations["llvm.pow.f128"] = &Pow;
1367 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001368 Optimizations["exp2l"] = &Exp2;
1369 Optimizations["exp2"] = &Exp2;
1370 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001371 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1372 Optimizations["llvm.exp2.f128"] = &Exp2;
1373 Optimizations["llvm.exp2.f80"] = &Exp2;
1374 Optimizations["llvm.exp2.f64"] = &Exp2;
1375 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001376
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001377#ifdef HAVE_FLOORF
1378 Optimizations["floor"] = &UnaryDoubleFP;
1379#endif
1380#ifdef HAVE_CEILF
1381 Optimizations["ceil"] = &UnaryDoubleFP;
1382#endif
1383#ifdef HAVE_ROUNDF
1384 Optimizations["round"] = &UnaryDoubleFP;
1385#endif
1386#ifdef HAVE_RINTF
1387 Optimizations["rint"] = &UnaryDoubleFP;
1388#endif
1389#ifdef HAVE_NEARBYINTF
1390 Optimizations["nearbyint"] = &UnaryDoubleFP;
1391#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001392
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001393 // Integer Optimizations
1394 Optimizations["ffs"] = &FFS;
1395 Optimizations["ffsl"] = &FFS;
1396 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001397 Optimizations["abs"] = &Abs;
1398 Optimizations["labs"] = &Abs;
1399 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001400 Optimizations["isdigit"] = &IsDigit;
1401 Optimizations["isascii"] = &IsAscii;
1402 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001403
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001404 // Formatting and IO Optimizations
1405 Optimizations["sprintf"] = &SPrintF;
1406 Optimizations["printf"] = &PrintF;
1407 Optimizations["fwrite"] = &FWrite;
1408 Optimizations["fputs"] = &FPuts;
1409 Optimizations["fprintf"] = &FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001410
1411 // Object Size Checking
Eric Christopher80bf1d52009-11-21 01:01:30 +00001412 Optimizations["__memcpy_chk"] = &MemCpyChk;
1413 Optimizations["__memset_chk"] = &MemSetChk;
1414 Optimizations["__memmove_chk"] = &MemMoveChk;
Eric Christopher7672d042010-01-23 05:29:06 +00001415 Optimizations["__strcpy_chk"] = &StrCpyChk;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001416}
1417
1418
1419/// runOnFunction - Top level algorithm.
1420///
1421bool SimplifyLibCalls::runOnFunction(Function &F) {
1422 if (Optimizations.empty())
1423 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001424
Dan Gohmanf14d9192009-08-18 00:48:13 +00001425 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001426
Owen Andersone922c022009-07-22 00:24:57 +00001427 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001428
1429 bool Changed = false;
1430 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1431 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1432 // Ignore non-calls.
1433 CallInst *CI = dyn_cast<CallInst>(I++);
1434 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001435
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001436 // Ignore indirect calls and calls to non-external functions.
1437 Function *Callee = CI->getCalledFunction();
1438 if (Callee == 0 || !Callee->isDeclaration() ||
1439 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1440 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001441
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001442 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001443 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1444 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001445
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001446 // Set the builder to the instruction after the call.
1447 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001448
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001449 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001450 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001451 if (Result == 0) continue;
1452
David Greene6a6b90e2010-01-05 01:27:21 +00001453 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1454 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001455
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001456 // Something changed!
1457 Changed = true;
1458 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001459
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001460 // Inspect the instruction after the call (which was potentially just
1461 // added) next.
1462 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001463
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001464 if (CI != Result && !CI->use_empty()) {
1465 CI->replaceAllUsesWith(Result);
1466 if (!Result->hasName())
1467 Result->takeName(CI);
1468 }
1469 CI->eraseFromParent();
1470 }
1471 }
1472 return Changed;
1473}
1474
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001475// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001476
1477void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1478 if (!F.doesNotAccessMemory()) {
1479 F.setDoesNotAccessMemory();
1480 ++NumAnnotated;
1481 Modified = true;
1482 }
1483}
1484void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1485 if (!F.onlyReadsMemory()) {
1486 F.setOnlyReadsMemory();
1487 ++NumAnnotated;
1488 Modified = true;
1489 }
1490}
1491void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1492 if (!F.doesNotThrow()) {
1493 F.setDoesNotThrow();
1494 ++NumAnnotated;
1495 Modified = true;
1496 }
1497}
1498void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1499 if (!F.doesNotCapture(n)) {
1500 F.setDoesNotCapture(n);
1501 ++NumAnnotated;
1502 Modified = true;
1503 }
1504}
1505void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1506 if (!F.doesNotAlias(n)) {
1507 F.setDoesNotAlias(n);
1508 ++NumAnnotated;
1509 Modified = true;
1510 }
1511}
1512
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001513/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001514///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001515bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001516 Modified = false;
1517 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1518 Function &F = *I;
1519 if (!F.isDeclaration())
1520 continue;
1521
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001522 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001523 continue;
1524
1525 const FunctionType *FTy = F.getFunctionType();
1526
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001527 StringRef Name = F.getName();
1528 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001529 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001530 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001531 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001532 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001533 continue;
1534 setOnlyReadsMemory(F);
1535 setDoesNotThrow(F);
1536 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001537 } else if (Name == "strcpy" ||
1538 Name == "stpcpy" ||
1539 Name == "strcat" ||
1540 Name == "strtol" ||
1541 Name == "strtod" ||
1542 Name == "strtof" ||
1543 Name == "strtoul" ||
1544 Name == "strtoll" ||
1545 Name == "strtold" ||
1546 Name == "strncat" ||
1547 Name == "strncpy" ||
1548 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001549 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001550 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001551 continue;
1552 setDoesNotThrow(F);
1553 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001554 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001555 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001556 !FTy->getParamType(0)->isPointerTy() ||
1557 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001558 continue;
1559 setDoesNotThrow(F);
1560 setDoesNotCapture(F, 1);
1561 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001562 } else if (Name == "strcmp" ||
1563 Name == "strspn" ||
1564 Name == "strncmp" ||
1565 Name ==" strcspn" ||
1566 Name == "strcoll" ||
1567 Name == "strcasecmp" ||
1568 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001569 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001570 !FTy->getParamType(0)->isPointerTy() ||
1571 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001572 continue;
1573 setOnlyReadsMemory(F);
1574 setDoesNotThrow(F);
1575 setDoesNotCapture(F, 1);
1576 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001577 } else if (Name == "strstr" ||
1578 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001579 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001580 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001581 continue;
1582 setOnlyReadsMemory(F);
1583 setDoesNotThrow(F);
1584 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001585 } else if (Name == "strtok" ||
1586 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001587 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001588 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001589 continue;
1590 setDoesNotThrow(F);
1591 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001592 } else if (Name == "scanf" ||
1593 Name == "setbuf" ||
1594 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001595 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001596 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001597 continue;
1598 setDoesNotThrow(F);
1599 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001600 } else if (Name == "strdup" ||
1601 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001602 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001603 !FTy->getReturnType()->isPointerTy() ||
1604 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001605 continue;
1606 setDoesNotThrow(F);
1607 setDoesNotAlias(F, 0);
1608 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001609 } else if (Name == "stat" ||
1610 Name == "sscanf" ||
1611 Name == "sprintf" ||
1612 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001613 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001614 !FTy->getParamType(0)->isPointerTy() ||
1615 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001616 continue;
1617 setDoesNotThrow(F);
1618 setDoesNotCapture(F, 1);
1619 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001620 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001621 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001622 !FTy->getParamType(0)->isPointerTy() ||
1623 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001624 continue;
1625 setDoesNotThrow(F);
1626 setDoesNotCapture(F, 1);
1627 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001628 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001629 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001630 !FTy->getParamType(1)->isPointerTy() ||
1631 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001632 continue;
1633 setDoesNotThrow(F);
1634 setDoesNotCapture(F, 2);
1635 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001636 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001637 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001638 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001639 continue;
1640 // May throw; "system" is a valid pthread cancellation point.
1641 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001642 }
1643 break;
1644 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001645 if (Name == "malloc") {
1646 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001647 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001648 continue;
1649 setDoesNotThrow(F);
1650 setDoesNotAlias(F, 0);
1651 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001652 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001653 !FTy->getParamType(0)->isPointerTy() ||
1654 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001655 continue;
1656 setOnlyReadsMemory(F);
1657 setDoesNotThrow(F);
1658 setDoesNotCapture(F, 1);
1659 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001660 } else if (Name == "memchr" ||
1661 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001662 if (FTy->getNumParams() != 3)
1663 continue;
1664 setOnlyReadsMemory(F);
1665 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001666 } else if (Name == "modf" ||
1667 Name == "modff" ||
1668 Name == "modfl" ||
1669 Name == "memcpy" ||
1670 Name == "memccpy" ||
1671 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001672 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001673 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001674 continue;
1675 setDoesNotThrow(F);
1676 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001677 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001678 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001679 continue;
1680 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001681 } else if (Name == "mkdir" ||
1682 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001683 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001684 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001685 continue;
1686 setDoesNotThrow(F);
1687 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001688 }
1689 break;
1690 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001691 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001692 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001693 !FTy->getParamType(0)->isPointerTy() ||
1694 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001695 continue;
1696 setDoesNotThrow(F);
1697 setDoesNotAlias(F, 0);
1698 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001699 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001700 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001701 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001702 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001703 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001704 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001705 } else if (Name == "rmdir" ||
1706 Name == "rewind" ||
1707 Name == "remove" ||
1708 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001709 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001710 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001711 continue;
1712 setDoesNotThrow(F);
1713 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001714 } else if (Name == "rename" ||
1715 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001716 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001717 !FTy->getParamType(0)->isPointerTy() ||
1718 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001719 continue;
1720 setDoesNotThrow(F);
1721 setDoesNotCapture(F, 1);
1722 setDoesNotCapture(F, 2);
1723 }
1724 break;
1725 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001726 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001727 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001728 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001729 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001730 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001731 setDoesNotCapture(F, 2);
1732 }
1733 break;
1734 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001735 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001736 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001737 !FTy->getParamType(0)->isPointerTy() ||
1738 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001739 continue;
1740 setDoesNotThrow(F);
1741 setDoesNotCapture(F, 1);
1742 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001743 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001744 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001745 !FTy->getParamType(0)->isPointerTy() ||
1746 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001747 continue;
1748 setDoesNotThrow(F);
1749 setOnlyReadsMemory(F);
1750 setDoesNotCapture(F, 1);
1751 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001752 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001753 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001754 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001755 continue;
1756 setDoesNotThrow(F);
1757 setDoesNotCapture(F, 1);
1758 }
1759 break;
1760 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001761 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001762 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001763 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001764 continue;
1765 setDoesNotThrow(F);
1766 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001767 } else if (Name == "chmod" ||
1768 Name == "chown" ||
1769 Name == "ctermid" ||
1770 Name == "clearerr" ||
1771 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001772 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001773 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001774 continue;
1775 setDoesNotThrow(F);
1776 setDoesNotCapture(F, 1);
1777 }
1778 break;
1779 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001780 if (Name == "atoi" ||
1781 Name == "atol" ||
1782 Name == "atof" ||
1783 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001784 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001785 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001786 continue;
1787 setDoesNotThrow(F);
1788 setOnlyReadsMemory(F);
1789 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001790 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001791 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001792 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001793 continue;
1794 setDoesNotThrow(F);
1795 setDoesNotCapture(F, 1);
1796 }
1797 break;
1798 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001799 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001800 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001801 !FTy->getReturnType()->isPointerTy() ||
1802 !FTy->getParamType(0)->isPointerTy() ||
1803 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001804 continue;
1805 setDoesNotThrow(F);
1806 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001807 setDoesNotCapture(F, 1);
1808 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001809 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001810 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001811 !FTy->getReturnType()->isPointerTy() ||
1812 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001813 continue;
1814 setDoesNotThrow(F);
1815 setDoesNotAlias(F, 0);
1816 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001817 } else if (Name == "feof" ||
1818 Name == "free" ||
1819 Name == "fseek" ||
1820 Name == "ftell" ||
1821 Name == "fgetc" ||
1822 Name == "fseeko" ||
1823 Name == "ftello" ||
1824 Name == "fileno" ||
1825 Name == "fflush" ||
1826 Name == "fclose" ||
1827 Name == "fsetpos" ||
1828 Name == "flockfile" ||
1829 Name == "funlockfile" ||
1830 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001831 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001832 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001833 continue;
1834 setDoesNotThrow(F);
1835 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001836 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001837 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001838 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001839 continue;
1840 setDoesNotThrow(F);
1841 setDoesNotCapture(F, 1);
1842 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001843 } else if (Name == "fputc" ||
1844 Name == "fstat" ||
1845 Name == "frexp" ||
1846 Name == "frexpf" ||
1847 Name == "frexpl" ||
1848 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001849 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001850 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001851 continue;
1852 setDoesNotThrow(F);
1853 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001854 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001855 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001856 !FTy->getParamType(0)->isPointerTy() ||
1857 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001858 continue;
1859 setDoesNotThrow(F);
1860 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001861 } else if (Name == "fread" ||
1862 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001863 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001864 !FTy->getParamType(0)->isPointerTy() ||
1865 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001866 continue;
1867 setDoesNotThrow(F);
1868 setDoesNotCapture(F, 1);
1869 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001870 } else if (Name == "fputs" ||
1871 Name == "fscanf" ||
1872 Name == "fprintf" ||
1873 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001874 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001875 !FTy->getParamType(0)->isPointerTy() ||
1876 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001877 continue;
1878 setDoesNotThrow(F);
1879 setDoesNotCapture(F, 1);
1880 setDoesNotCapture(F, 2);
1881 }
1882 break;
1883 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001884 if (Name == "getc" ||
1885 Name == "getlogin_r" ||
1886 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001887 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001888 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001889 continue;
1890 setDoesNotThrow(F);
1891 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001892 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001893 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001894 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001895 continue;
1896 setDoesNotThrow(F);
1897 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001898 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001899 } else if (Name == "gets" ||
1900 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001901 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001902 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001903 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001904 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001905 continue;
1906 setDoesNotThrow(F);
1907 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001908 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001909 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001910 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001911 continue;
1912 setDoesNotThrow(F);
1913 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001914 }
1915 break;
1916 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001917 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001918 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001919 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001920 continue;
1921 setDoesNotThrow(F);
1922 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001923 } else if (Name == "uname" ||
1924 Name == "unlink" ||
1925 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001926 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001927 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001928 continue;
1929 setDoesNotThrow(F);
1930 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001931 } else if (Name == "utime" ||
1932 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001933 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001934 !FTy->getParamType(0)->isPointerTy() ||
1935 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001936 continue;
1937 setDoesNotThrow(F);
1938 setDoesNotCapture(F, 1);
1939 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001940 }
1941 break;
1942 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001943 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001944 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001945 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001946 continue;
1947 setDoesNotThrow(F);
1948 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001949 } else if (Name == "puts" ||
1950 Name == "printf" ||
1951 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001952 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001953 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001954 continue;
1955 setDoesNotThrow(F);
1956 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001957 } else if (Name == "pread" ||
1958 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001959 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001960 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001961 continue;
1962 // May throw; these are valid pthread cancellation points.
1963 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001964 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001965 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001966 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001967 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001968 !FTy->getReturnType()->isPointerTy() ||
1969 !FTy->getParamType(0)->isPointerTy() ||
1970 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001971 continue;
1972 setDoesNotThrow(F);
1973 setDoesNotAlias(F, 0);
1974 setDoesNotCapture(F, 1);
1975 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001976 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001977 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001978 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001979 continue;
1980 setDoesNotThrow(F);
1981 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001982 }
1983 break;
1984 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001985 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001986 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001987 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001988 continue;
1989 setDoesNotThrow(F);
1990 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001991 } else if (Name == "vsscanf" ||
1992 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001993 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001994 !FTy->getParamType(1)->isPointerTy() ||
1995 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001996 continue;
1997 setDoesNotThrow(F);
1998 setDoesNotCapture(F, 1);
1999 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002000 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00002001 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002002 continue;
2003 setDoesNotThrow(F);
2004 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002005 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002006 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002007 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002008 continue;
2009 setDoesNotThrow(F);
2010 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002011 } else if (Name == "vfprintf" ||
2012 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002013 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002014 !FTy->getParamType(0)->isPointerTy() ||
2015 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002016 continue;
2017 setDoesNotThrow(F);
2018 setDoesNotCapture(F, 1);
2019 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002020 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002021 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002022 !FTy->getParamType(0)->isPointerTy() ||
2023 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002024 continue;
2025 setDoesNotThrow(F);
2026 setDoesNotCapture(F, 1);
2027 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002028 }
2029 break;
2030 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002031 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002032 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002033 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002034 continue;
2035 // May throw; "open" is a valid pthread cancellation point.
2036 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002037 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002038 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002039 !FTy->getReturnType()->isPointerTy() ||
2040 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002041 continue;
2042 setDoesNotThrow(F);
2043 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00002044 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002045 }
2046 break;
2047 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002048 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00002049 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002050 continue;
2051 setDoesNotThrow(F);
2052 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002053 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002054 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002055 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002056 continue;
2057 setDoesNotThrow(F);
2058 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002059 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002060 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002061 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002062 if (Name == "htonl" ||
2063 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002064 setDoesNotThrow(F);
2065 setDoesNotAccessMemory(F);
2066 }
2067 break;
2068 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002069 if (Name == "ntohl" ||
2070 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002071 setDoesNotThrow(F);
2072 setDoesNotAccessMemory(F);
2073 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002074 break;
2075 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002076 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002077 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002078 !FTy->getParamType(0)->isPointerTy() ||
2079 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002080 continue;
2081 setDoesNotThrow(F);
2082 setDoesNotCapture(F, 1);
2083 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002084 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002085 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002086 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002087 continue;
2088 setDoesNotThrow(F);
2089 setDoesNotCapture(F, 1);
2090 }
2091 break;
2092 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002093 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002094 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002095 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002096 continue;
2097 // May throw; places call through function pointer.
2098 setDoesNotCapture(F, 4);
2099 }
2100 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002101 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002102 if (Name == "__strdup" ||
2103 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002104 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002105 !FTy->getReturnType()->isPointerTy() ||
2106 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002107 continue;
2108 setDoesNotThrow(F);
2109 setDoesNotAlias(F, 0);
2110 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002111 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002112 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002113 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002114 continue;
2115 setDoesNotThrow(F);
2116 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002117 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002118 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002119 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002120 continue;
2121 setDoesNotThrow(F);
2122 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002123 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002124 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002125 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002126 continue;
2127 setDoesNotThrow(F);
2128 setDoesNotCapture(F, 2);
2129 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002130 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002131 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002132 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002133 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002134 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002135 continue;
2136 setDoesNotThrow(F);
2137 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002138 } else if (Name == "\1stat64" ||
2139 Name == "\1lstat64" ||
2140 Name == "\1statvfs64" ||
2141 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002142 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002143 !FTy->getParamType(0)->isPointerTy() ||
2144 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002145 continue;
2146 setDoesNotThrow(F);
2147 setDoesNotCapture(F, 1);
2148 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002149 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002150 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002151 !FTy->getReturnType()->isPointerTy() ||
2152 !FTy->getParamType(0)->isPointerTy() ||
2153 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002154 continue;
2155 setDoesNotThrow(F);
2156 setDoesNotAlias(F, 0);
2157 setDoesNotCapture(F, 1);
2158 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002159 } else if (Name == "\1fseeko64" ||
2160 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002161 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002162 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002163 continue;
2164 setDoesNotThrow(F);
2165 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002166 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002167 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002168 continue;
2169 setDoesNotThrow(F);
2170 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002171 } else if (Name == "\1fstat64" ||
2172 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002173 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002174 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002175 continue;
2176 setDoesNotThrow(F);
2177 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002178 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002179 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002180 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002181 continue;
2182 // May throw; "open" is a valid pthread cancellation point.
2183 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002184 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002185 break;
2186 }
2187 }
2188 return Modified;
2189}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002190
2191// TODO:
2192// Additional cases that we need to add to this file:
2193//
2194// cbrt:
2195// * cbrt(expN(X)) -> expN(x/3)
2196// * cbrt(sqrt(x)) -> pow(x,1/6)
2197// * cbrt(sqrt(x)) -> pow(x,1/9)
2198//
2199// cos, cosf, cosl:
2200// * cos(-x) -> cos(x)
2201//
2202// exp, expf, expl:
2203// * exp(log(x)) -> x
2204//
2205// log, logf, logl:
2206// * log(exp(x)) -> x
2207// * log(x**y) -> y*log(x)
2208// * log(exp(y)) -> y*log(e)
2209// * log(exp2(y)) -> y*log(2)
2210// * log(exp10(y)) -> y*log(10)
2211// * log(sqrt(x)) -> 0.5*log(x)
2212// * log(pow(x,y)) -> y*log(x)
2213//
2214// lround, lroundf, lroundl:
2215// * lround(cnst) -> cnst'
2216//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002217// pow, powf, powl:
2218// * pow(exp(x),y) -> exp(x*y)
2219// * pow(sqrt(x),y) -> pow(x,y*0.5)
2220// * pow(pow(x,y),z)-> pow(x,y*z)
2221//
2222// puts:
2223// * puts("") -> putchar("\n")
2224//
2225// round, roundf, roundl:
2226// * round(cnst) -> cnst'
2227//
2228// signbit:
2229// * signbit(cnst) -> cnst'
2230// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2231//
2232// sqrt, sqrtf, sqrtl:
2233// * sqrt(expN(x)) -> expN(x*0.5)
2234// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2235// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2236//
2237// stpcpy:
2238// * stpcpy(str, "literal") ->
2239// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2240// strrchr:
2241// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2242// (if c is a constant integer and s is a constant string)
2243// * strrchr(s1,0) -> strchr(s1,0)
2244//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002245// strpbrk:
2246// * strpbrk(s,a) -> offset_in_for(s,a)
2247// (if s and a are both constant strings)
2248// * strpbrk(s,"") -> 0
2249// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2250//
2251// strspn, strcspn:
2252// * strspn(s,a) -> const_int (if both args are constant)
2253// * strspn("",a) -> 0
2254// * strspn(s,"") -> 0
2255// * strcspn(s,a) -> const_int (if both args are constant)
2256// * strcspn("",a) -> 0
2257// * strcspn(s,"") -> strlen(a)
2258//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002259// tan, tanf, tanl:
2260// * tan(atan(x)) -> x
2261//
2262// trunc, truncf, truncl:
2263// * trunc(cnst) -> cnst'
2264//
2265//