blob: 8a0f6c0c182d54b94675f3d96a51d85fb945a6a4 [file] [log] [blame]
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001//===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a simple pass that applies a variety of small
11// optimizations for calls to specific well-known function calls (e.g. runtime
Chris Lattnere9f9a7e2009-09-03 05:19:59 +000012// library functions). Any optimization that takes the very simple form
13// "replace call to library function with simpler code that provides the same
14// result" belongs in this file.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000015//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "simplify-libcalls"
19#include "llvm/Transforms/Scalar.h"
Eric Christopherb6174e32010-03-05 22:25:30 +000020#include "llvm/Transforms/Utils/BuildLibCalls.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000021#include "llvm/Intrinsics.h"
Owen Andersonfa5cbd62009-07-03 19:42:02 +000022#include "llvm/LLVMContext.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000023#include "llvm/Module.h"
24#include "llvm/Pass.h"
25#include "llvm/Support/IRBuilder.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000026#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000027#include "llvm/Target/TargetData.h"
28#include "llvm/ADT/SmallPtrSet.h"
29#include "llvm/ADT/StringMap.h"
30#include "llvm/ADT/Statistic.h"
Daniel Dunbar473955f2009-07-29 22:00:43 +000031#include "llvm/ADT/STLExtras.h"
Chris Lattner56b4f2b2008-05-01 06:39:12 +000032#include "llvm/Support/Debug.h"
Daniel Dunbarf0443c12009-07-26 08:34:35 +000033#include "llvm/Support/raw_ostream.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000034#include "llvm/Config/config.h"
35using namespace llvm;
36
37STATISTIC(NumSimplified, "Number of library calls simplified");
Nick Lewycky0f8df9a2009-01-04 20:27:34 +000038STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000039
40//===----------------------------------------------------------------------===//
41// Optimizer Base Class
42//===----------------------------------------------------------------------===//
43
44/// This class is the abstract base class for the set of optimizations that
45/// corresponds to one library call.
46namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +000047class LibCallOptimization {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000048protected:
49 Function *Caller;
50 const TargetData *TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000051 LLVMContext* Context;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000052public:
Evan Chengeb8c6452010-03-24 20:19:04 +000053 LibCallOptimization() { }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000054 virtual ~LibCallOptimization() {}
55
56 /// CallOptimizer - This pure virtual method is implemented by base classes to
57 /// do various optimizations. If this returns null then no transformation was
58 /// performed. If it returns CI, then it transformed the call and CI is to be
59 /// deleted. If it returns something else, replace CI with the new value and
60 /// delete CI.
Eric Christopher37c8b862009-10-07 21:14:25 +000061 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
Eric Christopher7a61d702008-08-08 19:39:37 +000062 =0;
Eric Christopher37c8b862009-10-07 21:14:25 +000063
Dan Gohmanf14d9192009-08-18 00:48:13 +000064 Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000065 Caller = CI->getParent()->getParent();
Dan Gohmanf14d9192009-08-18 00:48:13 +000066 this->TD = TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000067 if (CI->getCalledFunction())
Owen Andersone922c022009-07-22 00:24:57 +000068 Context = &CI->getCalledFunction()->getContext();
Rafael Espindolae96af562010-06-16 19:34:01 +000069
70 // We never change the calling convention.
71 if (CI->getCallingConv() != llvm::CallingConv::C)
72 return NULL;
73
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000074 return CallOptimizer(CI->getCalledFunction(), CI, B);
75 }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000076};
77} // End anonymous namespace.
78
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000079
80//===----------------------------------------------------------------------===//
81// Helper Functions
82//===----------------------------------------------------------------------===//
83
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000084/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +000085/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000086static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
87 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
88 UI != E; ++UI) {
89 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
90 if (IC->isEquality())
91 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
92 if (C->isNullValue())
93 continue;
94 // Unknown instruction.
95 return false;
96 }
97 return true;
98}
99
Benjamin Kramer386e9182010-06-15 21:34:25 +0000100/// IsOnlyUsedInEqualityComparison - Return true if it is only used in equality
101/// comparisons with With.
102static bool IsOnlyUsedInEqualityComparison(Value *V, Value *With) {
103 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
104 UI != E; ++UI) {
105 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
106 if (IC->isEquality() && IC->getOperand(1) == With)
107 continue;
108 // Unknown instruction.
109 return false;
110 }
111 return true;
112}
113
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000114//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000115// String and Memory LibCall Optimizations
116//===----------------------------------------------------------------------===//
117
118//===---------------------------------------===//
119// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000120namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000121struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000122 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000123 // Verify the "strcat" function prototype.
124 const FunctionType *FT = Callee->getFunctionType();
125 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000126 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000127 FT->getParamType(0) != FT->getReturnType() ||
128 FT->getParamType(1) != FT->getReturnType())
129 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000130
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000131 // Extract some information from the instruction
Gabor Greifaee5dc12010-06-24 10:42:46 +0000132 Value *Dst = CI->getArgOperand(0);
133 Value *Src = CI->getArgOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000134
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000135 // See if we can get the length of the input string.
136 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000137 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000138 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000139
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000140 // Handle the simple, do-nothing case: strcat(x, "") -> x
141 if (Len == 0)
142 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000143
144 // These optimizations require TargetData.
145 if (!TD) return 0;
146
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000147 EmitStrLenMemCpy(Src, Dst, Len, B);
148 return Dst;
149 }
150
151 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000152 // We need to find the end of the destination string. That's where the
153 // memory is to be moved to. We just generate a call to strlen.
Eric Christopherb6174e32010-03-05 22:25:30 +0000154 Value *DstLen = EmitStrLen(Dst, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000155
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000156 // Now that we have the destination's length, we must index into the
157 // destination's pointer to get the actual memcpy destination (end of
158 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000159 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000160
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000161 // We have enough information to now generate the memcpy call to do the
162 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000163 EmitMemCpy(CpyDst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000164 ConstantInt::get(TD->getIntPtrType(*Context), Len+1),
165 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000166 }
167};
168
169//===---------------------------------------===//
170// 'strncat' Optimizations
171
Chris Lattner3e8b6632009-09-02 06:11:42 +0000172struct StrNCatOpt : public StrCatOpt {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000173 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
174 // Verify the "strncat" function prototype.
175 const FunctionType *FT = Callee->getFunctionType();
176 if (FT->getNumParams() != 3 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000177 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000178 FT->getParamType(0) != FT->getReturnType() ||
179 FT->getParamType(1) != FT->getReturnType() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000180 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000181 return 0;
182
183 // Extract some information from the instruction
Gabor Greifaee5dc12010-06-24 10:42:46 +0000184 Value *Dst = CI->getArgOperand(0);
185 Value *Src = CI->getArgOperand(1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000186 uint64_t Len;
187
188 // We don't do anything if length is not constant
Gabor Greifaee5dc12010-06-24 10:42:46 +0000189 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000190 Len = LengthArg->getZExtValue();
191 else
192 return 0;
193
194 // See if we can get the length of the input string.
195 uint64_t SrcLen = GetStringLength(Src);
196 if (SrcLen == 0) return 0;
197 --SrcLen; // Unbias length.
198
199 // Handle the simple, do-nothing cases:
200 // strncat(x, "", c) -> x
201 // strncat(x, c, 0) -> x
202 if (SrcLen == 0 || Len == 0) return Dst;
203
Dan Gohmanf14d9192009-08-18 00:48:13 +0000204 // These optimizations require TargetData.
205 if (!TD) return 0;
206
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000207 // We don't optimize this case
208 if (Len < SrcLen) return 0;
209
210 // strncat(x, s, c) -> strcat(x, s)
211 // s is constant so the strcat can be optimized further
Chris Lattner5db4cdf2009-04-12 18:22:33 +0000212 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000213 return Dst;
214 }
215};
216
217//===---------------------------------------===//
218// 'strchr' Optimizations
219
Chris Lattner3e8b6632009-09-02 06:11:42 +0000220struct StrChrOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000221 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000222 // Verify the "strchr" function prototype.
223 const FunctionType *FT = Callee->getFunctionType();
224 if (FT->getNumParams() != 2 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000225 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000226 FT->getParamType(0) != FT->getReturnType())
227 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000228
Gabor Greifaee5dc12010-06-24 10:42:46 +0000229 Value *SrcStr = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000230
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000231 // If the second operand is non-constant, see if we can compute the length
232 // of the input string and turn this into memchr.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000233 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000234 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000235 // These optimizations require TargetData.
236 if (!TD) return 0;
237
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000238 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000239 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000240 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000241
Gabor Greifaee5dc12010-06-24 10:42:46 +0000242 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000243 ConstantInt::get(TD->getIntPtrType(*Context), Len),
244 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000245 }
246
247 // Otherwise, the character is a constant, see if the first argument is
248 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000249 std::string Str;
250 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000251 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000252
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000253 // strchr can find the nul character.
254 Str += '\0';
255 char CharValue = CharC->getSExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +0000256
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000257 // Compute the offset.
258 uint64_t i = 0;
259 while (1) {
260 if (i == Str.size()) // Didn't find the char. strchr returns null.
Owen Andersona7235ea2009-07-31 20:28:14 +0000261 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000262 // Did we find our match?
263 if (Str[i] == CharValue)
264 break;
265 ++i;
266 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000267
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000268 // strchr(s+n,c) -> gep(s+n+i,c)
Owen Anderson1d0be152009-08-13 21:58:54 +0000269 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000270 return B.CreateGEP(SrcStr, Idx, "strchr");
271 }
272};
273
274//===---------------------------------------===//
275// 'strcmp' Optimizations
276
Chris Lattner3e8b6632009-09-02 06:11:42 +0000277struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000278 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000279 // Verify the "strcmp" function prototype.
280 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000281 if (FT->getNumParams() != 2 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000282 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000283 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000284 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000285 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000286
Gabor Greifaee5dc12010-06-24 10:42:46 +0000287 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000288 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000289 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000290
Bill Wendling0582ae92009-03-13 04:39:26 +0000291 std::string Str1, Str2;
292 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
293 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000294
Bill Wendling0582ae92009-03-13 04:39:26 +0000295 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000296 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000297
Bill Wendling0582ae92009-03-13 04:39:26 +0000298 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000299 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000300
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000301 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000302 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000303 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000304 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000305
306 // strcmp(P, "x") -> memcmp(P, "x", 2)
307 uint64_t Len1 = GetStringLength(Str1P);
308 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000309 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000310 // These optimizations require TargetData.
311 if (!TD) return 0;
312
Nick Lewycky13a09e22008-12-21 00:19:21 +0000313 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000314 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000315 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000316 }
317
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000318 return 0;
319 }
320};
321
322//===---------------------------------------===//
323// 'strncmp' Optimizations
324
Chris Lattner3e8b6632009-09-02 06:11:42 +0000325struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000326 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000327 // Verify the "strncmp" function prototype.
328 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000329 if (FT->getNumParams() != 3 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000330 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000331 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000332 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000333 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000334 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000335
Gabor Greifaee5dc12010-06-24 10:42:46 +0000336 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000337 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000338 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000339
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000340 // Get the length argument if it is constant.
341 uint64_t Length;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000342 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000343 Length = LengthArg->getZExtValue();
344 else
345 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000346
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000347 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000348 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000349
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000350 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000351 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD);
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000352
Bill Wendling0582ae92009-03-13 04:39:26 +0000353 std::string Str1, Str2;
354 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
355 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000356
Bill Wendling0582ae92009-03-13 04:39:26 +0000357 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000358 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000359
Bill Wendling0582ae92009-03-13 04:39:26 +0000360 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000361 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000362
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000363 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000364 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000365 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000366 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000367 return 0;
368 }
369};
370
371
372//===---------------------------------------===//
373// 'strcpy' Optimizations
374
Chris Lattner3e8b6632009-09-02 06:11:42 +0000375struct StrCpyOpt : public LibCallOptimization {
Evan Chengeb8c6452010-03-24 20:19:04 +0000376 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
377
378 StrCpyOpt(bool c) : OptChkCall(c) {}
379
Eric Christopher7a61d702008-08-08 19:39:37 +0000380 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000381 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000382 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000383 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000384 if (FT->getNumParams() != NumParams ||
385 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000386 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000387 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000388 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000389
Gabor Greifaee5dc12010-06-24 10:42:46 +0000390 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000391 if (Dst == Src) // strcpy(x,x) -> x
392 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000393
Dan Gohmanf14d9192009-08-18 00:48:13 +0000394 // These optimizations require TargetData.
395 if (!TD) return 0;
396
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000397 // See if we can get the length of the input string.
398 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000399 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000400
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000401 // We have enough information to now generate the memcpy call to do the
402 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000403 if (OptChkCall)
404 EmitMemCpyChk(Dst, Src,
405 ConstantInt::get(TD->getIntPtrType(*Context), Len),
Gabor Greifaee5dc12010-06-24 10:42:46 +0000406 CI->getArgOperand(2), B, TD);
Evan Cheng0289b412010-03-23 15:48:04 +0000407 else
408 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000409 ConstantInt::get(TD->getIntPtrType(*Context), Len),
410 1, false, B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000411 return Dst;
412 }
413};
414
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000415//===---------------------------------------===//
416// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000417
Chris Lattner3e8b6632009-09-02 06:11:42 +0000418struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000419 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
420 const FunctionType *FT = Callee->getFunctionType();
421 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
422 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000423 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000424 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000425 return 0;
426
Gabor Greifaee5dc12010-06-24 10:42:46 +0000427 Value *Dst = CI->getArgOperand(0);
428 Value *Src = CI->getArgOperand(1);
429 Value *LenOp = CI->getArgOperand(2);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000430
431 // See if we can get the length of the input string.
432 uint64_t SrcLen = GetStringLength(Src);
433 if (SrcLen == 0) return 0;
434 --SrcLen;
435
436 if (SrcLen == 0) {
437 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Mon P Wang20adc9d2010-04-04 03:10:48 +0000438 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'),
439 LenOp, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000440 return Dst;
441 }
442
443 uint64_t Len;
444 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
445 Len = LengthArg->getZExtValue();
446 else
447 return 0;
448
449 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
450
Dan Gohmanf14d9192009-08-18 00:48:13 +0000451 // These optimizations require TargetData.
452 if (!TD) return 0;
453
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000454 // Let strncpy handle the zero padding
455 if (Len > SrcLen+1) return 0;
456
457 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000458 EmitMemCpy(Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000459 ConstantInt::get(TD->getIntPtrType(*Context), Len),
460 1, false, B, TD);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000461
462 return Dst;
463 }
464};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000465
466//===---------------------------------------===//
467// 'strlen' Optimizations
468
Chris Lattner3e8b6632009-09-02 06:11:42 +0000469struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000470 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000471 const FunctionType *FT = Callee->getFunctionType();
472 if (FT->getNumParams() != 1 ||
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000473 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000474 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000475 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000476
Gabor Greifaee5dc12010-06-24 10:42:46 +0000477 Value *Src = CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000478
479 // Constant folding: strlen("xyz") -> 3
480 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000481 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000482
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000483 // strlen(x) != 0 --> *x != 0
484 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000485 if (IsOnlyUsedInZeroEqualityComparison(CI))
486 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
487 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000488 }
489};
490
491//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000492// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000493
Chris Lattner3e8b6632009-09-02 06:11:42 +0000494struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000495 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
496 const FunctionType *FT = Callee->getFunctionType();
497 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000498 !FT->getParamType(0)->isPointerTy() ||
499 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000500 return 0;
501
Gabor Greifaee5dc12010-06-24 10:42:46 +0000502 Value *EndPtr = CI->getArgOperand(1);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000503 if (isa<ConstantPointerNull>(EndPtr)) {
504 CI->setOnlyReadsMemory();
Nick Lewycky4c498412009-02-13 15:31:46 +0000505 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000506 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000507
508 return 0;
509 }
510};
511
Chris Lattner24604112009-12-16 09:32:05 +0000512//===---------------------------------------===//
513// 'strstr' Optimizations
514
515struct StrStrOpt : public LibCallOptimization {
516 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
517 const FunctionType *FT = Callee->getFunctionType();
518 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000519 !FT->getParamType(0)->isPointerTy() ||
520 !FT->getParamType(1)->isPointerTy() ||
521 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000522 return 0;
523
524 // fold strstr(x, x) -> x.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000525 if (CI->getArgOperand(0) == CI->getArgOperand(1))
526 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000527
Benjamin Kramer386e9182010-06-15 21:34:25 +0000528 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000529 if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
530 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD);
531 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000532 StrLen, B, TD);
533 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
534 UI != UE; ) {
535 ICmpInst *Old = cast<ICmpInst>(UI++);
536 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
537 ConstantInt::getNullValue(StrNCmp->getType()),
538 "cmp");
539 Old->replaceAllUsesWith(Cmp);
540 Old->eraseFromParent();
541 }
542 return CI;
543 }
544
Chris Lattner24604112009-12-16 09:32:05 +0000545 // See if either input string is a constant string.
546 std::string SearchStr, ToFindStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000547 bool HasStr1 = GetConstantStringInfo(CI->getArgOperand(0), SearchStr);
548 bool HasStr2 = GetConstantStringInfo(CI->getArgOperand(1), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000549
Chris Lattner24604112009-12-16 09:32:05 +0000550 // fold strstr(x, "") -> x.
551 if (HasStr2 && ToFindStr.empty())
Gabor Greifaee5dc12010-06-24 10:42:46 +0000552 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000553
Chris Lattner24604112009-12-16 09:32:05 +0000554 // If both strings are known, constant fold it.
555 if (HasStr1 && HasStr2) {
556 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000557
Chris Lattner24604112009-12-16 09:32:05 +0000558 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
559 return Constant::getNullValue(CI->getType());
560
561 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000562 Value *Result = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner24604112009-12-16 09:32:05 +0000563 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
564 return B.CreateBitCast(Result, CI->getType());
565 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000566
Chris Lattner24604112009-12-16 09:32:05 +0000567 // fold strstr(x, "y") -> strchr(x, 'y').
568 if (HasStr2 && ToFindStr.size() == 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000569 return B.CreateBitCast(EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TD),
Chris Lattner24604112009-12-16 09:32:05 +0000570 CI->getType());
571 return 0;
572 }
573};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000574
Nick Lewycky4c498412009-02-13 15:31:46 +0000575
576//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000577// 'memcmp' Optimizations
578
Chris Lattner3e8b6632009-09-02 06:11:42 +0000579struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000580 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000581 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000582 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
583 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000584 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000585 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000586
Gabor Greifaee5dc12010-06-24 10:42:46 +0000587 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000588
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000589 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000590 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000591
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000592 // Make sure we have a constant length.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000593 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000594 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000595 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000596
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000597 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000598 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000599
Benjamin Kramer48aefe12010-05-25 22:53:43 +0000600 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
601 if (Len == 1) {
602 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
603 CI->getType(), "lhsv");
604 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
605 CI->getType(), "rhsv");
Benjamin Kramer1464c1d2010-05-26 09:45:04 +0000606 return B.CreateSub(LHSV, RHSV, "chardiff");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000607 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000608
Benjamin Kramer992a6372009-11-05 17:44:22 +0000609 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
610 std::string LHSStr, RHSStr;
611 if (GetConstantStringInfo(LHS, LHSStr) &&
612 GetConstantStringInfo(RHS, RHSStr)) {
613 // Make sure we're not reading out-of-bounds memory.
614 if (Len > LHSStr.length() || Len > RHSStr.length())
615 return 0;
616 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
617 return ConstantInt::get(CI->getType(), Ret);
618 }
619
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000620 return 0;
621 }
622};
623
624//===---------------------------------------===//
625// 'memcpy' Optimizations
626
Chris Lattner3e8b6632009-09-02 06:11:42 +0000627struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000628 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000629 // These optimizations require TargetData.
630 if (!TD) return 0;
631
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000632 const FunctionType *FT = Callee->getFunctionType();
633 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000634 !FT->getParamType(0)->isPointerTy() ||
635 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000636 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000637 return 0;
638
639 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000640 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
641 CI->getArgOperand(2), 1, false, B, TD);
642 return CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000643 }
644};
645
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000646//===---------------------------------------===//
647// 'memmove' Optimizations
648
Chris Lattner3e8b6632009-09-02 06:11:42 +0000649struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000650 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000651 // These optimizations require TargetData.
652 if (!TD) return 0;
653
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000654 const FunctionType *FT = Callee->getFunctionType();
655 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000656 !FT->getParamType(0)->isPointerTy() ||
657 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000658 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000659 return 0;
660
661 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000662 EmitMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
663 CI->getArgOperand(2), 1, false, B, TD);
664 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000665 }
666};
667
668//===---------------------------------------===//
669// 'memset' Optimizations
670
Chris Lattner3e8b6632009-09-02 06:11:42 +0000671struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000672 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000673 // These optimizations require TargetData.
674 if (!TD) return 0;
675
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000676 const FunctionType *FT = Callee->getFunctionType();
677 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000678 !FT->getParamType(0)->isPointerTy() ||
679 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000680 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000681 return 0;
682
683 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000684 Value *Val = B.CreateIntCast(CI->getArgOperand(1), Type::getInt8Ty(*Context),
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000685 false);
Gabor Greifaee5dc12010-06-24 10:42:46 +0000686 EmitMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), false, B, TD);
687 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000688 }
689};
690
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000691//===----------------------------------------------------------------------===//
692// Math Library Optimizations
693//===----------------------------------------------------------------------===//
694
695//===---------------------------------------===//
696// 'pow*' Optimizations
697
Chris Lattner3e8b6632009-09-02 06:11:42 +0000698struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000699 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000700 const FunctionType *FT = Callee->getFunctionType();
701 // Just make sure this has 2 arguments of the same FP type, which match the
702 // result type.
703 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
704 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000705 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000706 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000707
Gabor Greifaee5dc12010-06-24 10:42:46 +0000708 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000709 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
710 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
711 return Op1C;
712 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000713 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000714 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000715
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000716 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
717 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000718
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000719 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000720 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000721
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000722 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000723 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
724 // This is faster than calling pow, and still handles negative zero
725 // and negative infinite correctly.
726 // TODO: In fast-math mode, this could be just sqrt(x).
727 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000728 Value *Inf = ConstantFP::getInfinity(CI->getType());
729 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000730 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
731 Callee->getAttributes());
732 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
733 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000734 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
735 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
736 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000737 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000738
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000739 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
740 return Op1;
741 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000742 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000743 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000744 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000745 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000746 return 0;
747 }
748};
749
750//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000751// 'exp2' Optimizations
752
Chris Lattner3e8b6632009-09-02 06:11:42 +0000753struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000754 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000755 const FunctionType *FT = Callee->getFunctionType();
756 // Just make sure this has 1 argument of FP type, which matches the
757 // result type.
758 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000759 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000760 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000761
Gabor Greifaee5dc12010-06-24 10:42:46 +0000762 Value *Op = CI->getArgOperand(0);
Chris Lattnere818f772008-05-02 18:43:35 +0000763 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
764 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
765 Value *LdExpArg = 0;
766 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
767 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000768 LdExpArg = B.CreateSExt(OpC->getOperand(0),
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000769 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000770 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
771 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Eric Christopher37c8b862009-10-07 21:14:25 +0000772 LdExpArg = B.CreateZExt(OpC->getOperand(0),
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000773 Type::getInt32Ty(*Context), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000774 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000775
Chris Lattnere818f772008-05-02 18:43:35 +0000776 if (LdExpArg) {
777 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000778 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000779 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000780 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000781 Name = "ldexp";
782 else
783 Name = "ldexpl";
784
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000785 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000786 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000787 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000788
789 Module *M = Caller->getParent();
790 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000791 Op->getType(),
Eric Christopher3a8bb732010-02-02 00:13:06 +0000792 Type::getInt32Ty(*Context),NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000793 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
794 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
795 CI->setCallingConv(F->getCallingConv());
796
797 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000798 }
799 return 0;
800 }
801};
Chris Lattnere818f772008-05-02 18:43:35 +0000802
803//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000804// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
805
Chris Lattner3e8b6632009-09-02 06:11:42 +0000806struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000807 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000808 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000809 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
810 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000811 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000812
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000813 // If this is something like 'floor((double)floatval)', convert to floorf.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000814 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000815 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000816 return 0;
817
818 // floor((double)floatval) -> (double)floorf(floatval)
819 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000820 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
821 Callee->getAttributes());
Owen Anderson1d0be152009-08-13 21:58:54 +0000822 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000823 }
824};
825
826//===----------------------------------------------------------------------===//
827// Integer Optimizations
828//===----------------------------------------------------------------------===//
829
830//===---------------------------------------===//
831// 'ffs*' Optimizations
832
Chris Lattner3e8b6632009-09-02 06:11:42 +0000833struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000834 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000835 const FunctionType *FT = Callee->getFunctionType();
836 // Just make sure this has 2 arguments of the same FP type, which match the
837 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000838 if (FT->getNumParams() != 1 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000839 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000840 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000841 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000842
Gabor Greifaee5dc12010-06-24 10:42:46 +0000843 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000844
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000845 // Constant fold.
846 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
847 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000848 return Constant::getNullValue(CI->getType());
Owen Anderson1d0be152009-08-13 21:58:54 +0000849 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000850 CI->getValue().countTrailingZeros()+1);
851 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000852
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000853 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
854 const Type *ArgType = Op->getType();
855 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
856 Intrinsic::cttz, &ArgType, 1);
857 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000858 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Owen Anderson1d0be152009-08-13 21:58:54 +0000859 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000860
Owen Andersona7235ea2009-07-31 20:28:14 +0000861 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000862 return B.CreateSelect(Cond, V,
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000863 ConstantInt::get(Type::getInt32Ty(*Context), 0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000864 }
865};
866
867//===---------------------------------------===//
868// 'isdigit' Optimizations
869
Chris Lattner3e8b6632009-09-02 06:11:42 +0000870struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000871 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000872 const FunctionType *FT = Callee->getFunctionType();
873 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000874 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000875 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000876 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000877
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000878 // isdigit(c) -> (c-'0') <u 10
Gabor Greifaee5dc12010-06-24 10:42:46 +0000879 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000880 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000881 "isdigittmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000882 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000883 "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000884 return B.CreateZExt(Op, CI->getType());
885 }
886};
887
888//===---------------------------------------===//
889// 'isascii' Optimizations
890
Chris Lattner3e8b6632009-09-02 06:11:42 +0000891struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000892 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000893 const FunctionType *FT = Callee->getFunctionType();
894 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +0000895 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000896 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000897 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000898
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000899 // isascii(c) -> c <u 128
Gabor Greifaee5dc12010-06-24 10:42:46 +0000900 Value *Op = CI->getArgOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +0000901 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000902 "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000903 return B.CreateZExt(Op, CI->getType());
904 }
905};
Eric Christopher37c8b862009-10-07 21:14:25 +0000906
Chris Lattner313f0e62008-06-09 08:26:51 +0000907//===---------------------------------------===//
908// 'abs', 'labs', 'llabs' Optimizations
909
Chris Lattner3e8b6632009-09-02 06:11:42 +0000910struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000911 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +0000912 const FunctionType *FT = Callee->getFunctionType();
913 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +0000914 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +0000915 FT->getParamType(0) != FT->getReturnType())
916 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000917
Chris Lattner313f0e62008-06-09 08:26:51 +0000918 // abs(x) -> x >s -1 ? x : -x
Gabor Greifaee5dc12010-06-24 10:42:46 +0000919 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000920 Value *Pos = B.CreateICmpSGT(Op,
Owen Andersona7235ea2009-07-31 20:28:14 +0000921 Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +0000922 "ispos");
923 Value *Neg = B.CreateNeg(Op, "neg");
924 return B.CreateSelect(Pos, Op, Neg);
925 }
926};
Eric Christopher37c8b862009-10-07 21:14:25 +0000927
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000928
929//===---------------------------------------===//
930// 'toascii' Optimizations
931
Chris Lattner3e8b6632009-09-02 06:11:42 +0000932struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000933 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000934 const FunctionType *FT = Callee->getFunctionType();
935 // We require i32(i32)
936 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000937 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000938 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000939
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000940 // isascii(c) -> c & 0x7f
Gabor Greifaee5dc12010-06-24 10:42:46 +0000941 return B.CreateAnd(CI->getArgOperand(0),
Owen Andersoneed707b2009-07-24 23:12:02 +0000942 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000943 }
944};
945
946//===----------------------------------------------------------------------===//
947// Formatting and IO Optimizations
948//===----------------------------------------------------------------------===//
949
950//===---------------------------------------===//
951// 'printf' Optimizations
952
Chris Lattner3e8b6632009-09-02 06:11:42 +0000953struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000954 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000955 // Require one fixed pointer argument and an integer/void result.
956 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000957 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
958 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000959 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000960 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000961
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000962 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +0000963 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000964 if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +0000965 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000966
967 // Empty format string -> noop.
968 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +0000969 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000970 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000971
Chris Lattner74965f22009-11-09 04:57:04 +0000972 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
973 // in case there is an error writing to stdout.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000974 if (FormatStr.size() == 1) {
Chris Lattner74965f22009-11-09 04:57:04 +0000975 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000976 FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +0000977 if (CI->use_empty()) return CI;
978 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000979 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000980
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000981 // printf("foo\n") --> puts("foo")
982 if (FormatStr[FormatStr.size()-1] == '\n' &&
983 FormatStr.find('%') == std::string::npos) { // no format characters.
984 // Create a string literal with no \n on it. We expect the constant merge
985 // pass to be run after this pass, to merge duplicate strings.
986 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +0000987 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +0000988 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
989 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +0000990 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000991 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +0000992 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000993 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000994
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000995 // Optimize specific format strings.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000996 // printf("%c", chr) --> putchar(chr)
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000997 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +0000998 CI->getArgOperand(1)->getType()->isIntegerTy()) {
999 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001000
Chris Lattner74965f22009-11-09 04:57:04 +00001001 if (CI->use_empty()) return CI;
1002 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001003 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001004
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001005 // printf("%s\n", str) --> puts(str)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001006 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001007 CI->getArgOperand(1)->getType()->isPointerTy() &&
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001008 CI->use_empty()) {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001009 EmitPutS(CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001010 return CI;
1011 }
1012 return 0;
1013 }
1014};
1015
1016//===---------------------------------------===//
1017// 'sprintf' Optimizations
1018
Chris Lattner3e8b6632009-09-02 06:11:42 +00001019struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001020 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001021 // Require two fixed pointer arguments and an integer result.
1022 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001023 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1024 !FT->getParamType(1)->isPointerTy() ||
1025 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001026 return 0;
1027
1028 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001029 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001030 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001031 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001032
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001033 // If we just have a format string (nothing else crazy) transform it.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001034 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001035 // Make sure there's no % in the constant array. We could try to handle
1036 // %% -> % in the future if we cared.
1037 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1038 if (FormatStr[i] == '%')
1039 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001040
1041 // These optimizations require TargetData.
1042 if (!TD) return 0;
1043
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001044 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001045 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), // Copy the nul byte.
Eric Christopherb6174e32010-03-05 22:25:30 +00001046 ConstantInt::get(TD->getIntPtrType(*Context),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001047 FormatStr.size()+1), 1, false, B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001048 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001049 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001050
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001051 // The remaining optimizations require the format string to be "%s" or "%c"
1052 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001053 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1054 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001055 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001056
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001057 // Decode the second character of the format string.
1058 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001059 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Gabor Greifaee5dc12010-06-24 10:42:46 +00001060 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1061 Value *V = B.CreateTrunc(CI->getArgOperand(2),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001062 Type::getInt8Ty(*Context), "char");
Gabor Greifaee5dc12010-06-24 10:42:46 +00001063 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001064 B.CreateStore(V, Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001065 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001066 "nul");
Owen Anderson1d0be152009-08-13 21:58:54 +00001067 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001068
Owen Andersoneed707b2009-07-24 23:12:02 +00001069 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001070 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001071
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001072 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001073 // These optimizations require TargetData.
1074 if (!TD) return 0;
1075
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001076 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001077 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001078
Gabor Greifaee5dc12010-06-24 10:42:46 +00001079 Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001080 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001081 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001082 "leninc");
Gabor Greifaee5dc12010-06-24 10:42:46 +00001083 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1, false, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001084
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001085 // The sprintf result is the unincremented number of bytes in the string.
1086 return B.CreateIntCast(Len, CI->getType(), false);
1087 }
1088 return 0;
1089 }
1090};
1091
1092//===---------------------------------------===//
1093// 'fwrite' Optimizations
1094
Chris Lattner3e8b6632009-09-02 06:11:42 +00001095struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001096 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001097 // Require a pointer, an integer, an integer, a pointer, returning integer.
1098 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001099 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1100 !FT->getParamType(1)->isIntegerTy() ||
1101 !FT->getParamType(2)->isIntegerTy() ||
1102 !FT->getParamType(3)->isPointerTy() ||
1103 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001104 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001105
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001106 // Get the element size and count.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001107 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1108 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001109 if (!SizeC || !CountC) return 0;
1110 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001111
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001112 // If this is writing zero records, remove the call (it's a noop).
1113 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001114 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001115
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001116 // If this is writing one byte, turn it into fputc.
1117 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001118 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1119 EmitFPutC(Char, CI->getArgOperand(3), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001120 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001121 }
1122
1123 return 0;
1124 }
1125};
1126
1127//===---------------------------------------===//
1128// 'fputs' Optimizations
1129
Chris Lattner3e8b6632009-09-02 06:11:42 +00001130struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001131 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001132 // These optimizations require TargetData.
1133 if (!TD) return 0;
1134
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001135 // Require two pointers. Also, we can't optimize if return value is used.
1136 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001137 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1138 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001139 !CI->use_empty())
1140 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001141
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001142 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001143 uint64_t Len = GetStringLength(CI->getArgOperand(0));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001144 if (!Len) return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001145 EmitFWrite(CI->getArgOperand(0),
Owen Anderson1d0be152009-08-13 21:58:54 +00001146 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001147 CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001148 return CI; // Known to have no uses (see above).
1149 }
1150};
1151
1152//===---------------------------------------===//
1153// 'fprintf' Optimizations
1154
Chris Lattner3e8b6632009-09-02 06:11:42 +00001155struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001156 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001157 // Require two fixed paramters as pointers and integer result.
1158 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001159 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1160 !FT->getParamType(1)->isPointerTy() ||
1161 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001162 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001163
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001164 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001165 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001166 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001167 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001168
1169 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001170 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001171 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1172 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001173 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001174
1175 // These optimizations require TargetData.
1176 if (!TD) return 0;
1177
Gabor Greifaee5dc12010-06-24 10:42:46 +00001178 EmitFWrite(CI->getArgOperand(1),
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001179 ConstantInt::get(TD->getIntPtrType(*Context),
1180 FormatStr.size()),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001181 CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001182 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001183 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001184
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001185 // The remaining optimizations require the format string to be "%s" or "%c"
1186 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001187 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1188 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001189 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001190
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001191 // Decode the second character of the format string.
1192 if (FormatStr[1] == 'c') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001193 // fprintf(F, "%c", chr) --> fputc(chr, F)
1194 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1195 EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001196 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001197 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001198
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001199 if (FormatStr[1] == 's') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001200 // fprintf(F, "%s", str) --> fputs(str, F)
1201 if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001202 return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001203 EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001204 return CI;
1205 }
1206 return 0;
1207 }
1208};
1209
Bill Wendlingac178222008-05-05 21:37:59 +00001210} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001211
1212//===----------------------------------------------------------------------===//
1213// SimplifyLibCalls Pass Implementation
1214//===----------------------------------------------------------------------===//
1215
1216namespace {
1217 /// This pass optimizes well known library functions from libc and libm.
1218 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001219 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001220 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001221 // String and Memory LibCall Optimizations
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001222 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
Evan Cheng0289b412010-03-23 15:48:04 +00001223 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
1224 StrNCpyOpt StrNCpy; StrLenOpt StrLen;
Chris Lattner24604112009-12-16 09:32:05 +00001225 StrToOpt StrTo; StrStrOpt StrStr;
1226 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001227 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001228 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001229 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001230 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1231 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001232 // Formatting and IO Optimizations
1233 SPrintFOpt SPrintF; PrintFOpt PrintF;
1234 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Eric Christopher80bf1d52009-11-21 01:01:30 +00001235
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001236 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001237 public:
1238 static char ID; // Pass identification
Evan Chengeb8c6452010-03-24 20:19:04 +00001239 SimplifyLibCalls() : FunctionPass(&ID), StrCpy(false), StrCpyChk(true) {}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001240 void InitOptimizations();
1241 bool runOnFunction(Function &F);
1242
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001243 void setDoesNotAccessMemory(Function &F);
1244 void setOnlyReadsMemory(Function &F);
1245 void setDoesNotThrow(Function &F);
1246 void setDoesNotCapture(Function &F, unsigned n);
1247 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001248 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001249
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001250 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001251 }
1252 };
1253 char SimplifyLibCalls::ID = 0;
1254} // end anonymous namespace.
1255
Owen Andersond13db2c2010-07-21 22:09:45 +00001256INITIALIZE_PASS(SimplifyLibCalls, "simplify-libcalls",
1257 "Simplify well-known library calls", false, false);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001258
1259// Public interface to the Simplify LibCalls pass.
1260FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001261 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001262}
1263
1264/// Optimizations - Populate the Optimizations map with all the optimizations
1265/// we know.
1266void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001267 // String and Memory LibCall Optimizations
1268 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001269 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001270 Optimizations["strchr"] = &StrChr;
1271 Optimizations["strcmp"] = &StrCmp;
1272 Optimizations["strncmp"] = &StrNCmp;
1273 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001274 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001275 Optimizations["strlen"] = &StrLen;
Nick Lewycky4c498412009-02-13 15:31:46 +00001276 Optimizations["strtol"] = &StrTo;
1277 Optimizations["strtod"] = &StrTo;
1278 Optimizations["strtof"] = &StrTo;
1279 Optimizations["strtoul"] = &StrTo;
1280 Optimizations["strtoll"] = &StrTo;
1281 Optimizations["strtold"] = &StrTo;
1282 Optimizations["strtoull"] = &StrTo;
Chris Lattner24604112009-12-16 09:32:05 +00001283 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001284 Optimizations["memcmp"] = &MemCmp;
1285 Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001286 Optimizations["memmove"] = &MemMove;
1287 Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001288
Evan Cheng0289b412010-03-23 15:48:04 +00001289 // _chk variants of String and Memory LibCall Optimizations.
Evan Cheng0289b412010-03-23 15:48:04 +00001290 Optimizations["__strcpy_chk"] = &StrCpyChk;
1291
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001292 // Math Library Optimizations
1293 Optimizations["powf"] = &Pow;
1294 Optimizations["pow"] = &Pow;
1295 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001296 Optimizations["llvm.pow.f32"] = &Pow;
1297 Optimizations["llvm.pow.f64"] = &Pow;
1298 Optimizations["llvm.pow.f80"] = &Pow;
1299 Optimizations["llvm.pow.f128"] = &Pow;
1300 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001301 Optimizations["exp2l"] = &Exp2;
1302 Optimizations["exp2"] = &Exp2;
1303 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001304 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1305 Optimizations["llvm.exp2.f128"] = &Exp2;
1306 Optimizations["llvm.exp2.f80"] = &Exp2;
1307 Optimizations["llvm.exp2.f64"] = &Exp2;
1308 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001309
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001310#ifdef HAVE_FLOORF
1311 Optimizations["floor"] = &UnaryDoubleFP;
1312#endif
1313#ifdef HAVE_CEILF
1314 Optimizations["ceil"] = &UnaryDoubleFP;
1315#endif
1316#ifdef HAVE_ROUNDF
1317 Optimizations["round"] = &UnaryDoubleFP;
1318#endif
1319#ifdef HAVE_RINTF
1320 Optimizations["rint"] = &UnaryDoubleFP;
1321#endif
1322#ifdef HAVE_NEARBYINTF
1323 Optimizations["nearbyint"] = &UnaryDoubleFP;
1324#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001325
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001326 // Integer Optimizations
1327 Optimizations["ffs"] = &FFS;
1328 Optimizations["ffsl"] = &FFS;
1329 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001330 Optimizations["abs"] = &Abs;
1331 Optimizations["labs"] = &Abs;
1332 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001333 Optimizations["isdigit"] = &IsDigit;
1334 Optimizations["isascii"] = &IsAscii;
1335 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001336
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001337 // Formatting and IO Optimizations
1338 Optimizations["sprintf"] = &SPrintF;
1339 Optimizations["printf"] = &PrintF;
1340 Optimizations["fwrite"] = &FWrite;
1341 Optimizations["fputs"] = &FPuts;
1342 Optimizations["fprintf"] = &FPrintF;
1343}
1344
1345
1346/// runOnFunction - Top level algorithm.
1347///
1348bool SimplifyLibCalls::runOnFunction(Function &F) {
1349 if (Optimizations.empty())
1350 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001351
Dan Gohmanf14d9192009-08-18 00:48:13 +00001352 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001353
Owen Andersone922c022009-07-22 00:24:57 +00001354 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001355
1356 bool Changed = false;
1357 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1358 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1359 // Ignore non-calls.
1360 CallInst *CI = dyn_cast<CallInst>(I++);
1361 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001362
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001363 // Ignore indirect calls and calls to non-external functions.
1364 Function *Callee = CI->getCalledFunction();
1365 if (Callee == 0 || !Callee->isDeclaration() ||
1366 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1367 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001368
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001369 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001370 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1371 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001372
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001373 // Set the builder to the instruction after the call.
1374 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001375
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001376 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001377 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001378 if (Result == 0) continue;
1379
David Greene6a6b90e2010-01-05 01:27:21 +00001380 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1381 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001382
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001383 // Something changed!
1384 Changed = true;
1385 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001386
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001387 // Inspect the instruction after the call (which was potentially just
1388 // added) next.
1389 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001390
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001391 if (CI != Result && !CI->use_empty()) {
1392 CI->replaceAllUsesWith(Result);
1393 if (!Result->hasName())
1394 Result->takeName(CI);
1395 }
1396 CI->eraseFromParent();
1397 }
1398 }
1399 return Changed;
1400}
1401
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001402// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001403
1404void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1405 if (!F.doesNotAccessMemory()) {
1406 F.setDoesNotAccessMemory();
1407 ++NumAnnotated;
1408 Modified = true;
1409 }
1410}
1411void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1412 if (!F.onlyReadsMemory()) {
1413 F.setOnlyReadsMemory();
1414 ++NumAnnotated;
1415 Modified = true;
1416 }
1417}
1418void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1419 if (!F.doesNotThrow()) {
1420 F.setDoesNotThrow();
1421 ++NumAnnotated;
1422 Modified = true;
1423 }
1424}
1425void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1426 if (!F.doesNotCapture(n)) {
1427 F.setDoesNotCapture(n);
1428 ++NumAnnotated;
1429 Modified = true;
1430 }
1431}
1432void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1433 if (!F.doesNotAlias(n)) {
1434 F.setDoesNotAlias(n);
1435 ++NumAnnotated;
1436 Modified = true;
1437 }
1438}
1439
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001440/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001441///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001442bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001443 Modified = false;
1444 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1445 Function &F = *I;
1446 if (!F.isDeclaration())
1447 continue;
1448
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001449 if (!F.hasName())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001450 continue;
1451
1452 const FunctionType *FTy = F.getFunctionType();
1453
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001454 StringRef Name = F.getName();
1455 switch (Name[0]) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001456 case 's':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001457 if (Name == "strlen") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001458 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001459 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001460 continue;
1461 setOnlyReadsMemory(F);
1462 setDoesNotThrow(F);
1463 setDoesNotCapture(F, 1);
Benjamin Kramer4446b042010-03-16 19:36:43 +00001464 } else if (Name == "strchr" ||
1465 Name == "strrchr") {
1466 if (FTy->getNumParams() != 2 ||
1467 !FTy->getParamType(0)->isPointerTy() ||
1468 !FTy->getParamType(1)->isIntegerTy())
1469 continue;
1470 setOnlyReadsMemory(F);
1471 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001472 } else if (Name == "strcpy" ||
1473 Name == "stpcpy" ||
1474 Name == "strcat" ||
1475 Name == "strtol" ||
1476 Name == "strtod" ||
1477 Name == "strtof" ||
1478 Name == "strtoul" ||
1479 Name == "strtoll" ||
1480 Name == "strtold" ||
1481 Name == "strncat" ||
1482 Name == "strncpy" ||
1483 Name == "strtoull") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001484 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001485 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001486 continue;
1487 setDoesNotThrow(F);
1488 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001489 } else if (Name == "strxfrm") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001490 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001491 !FTy->getParamType(0)->isPointerTy() ||
1492 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001493 continue;
1494 setDoesNotThrow(F);
1495 setDoesNotCapture(F, 1);
1496 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001497 } else if (Name == "strcmp" ||
1498 Name == "strspn" ||
1499 Name == "strncmp" ||
Benjamin Kramer4446b042010-03-16 19:36:43 +00001500 Name == "strcspn" ||
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001501 Name == "strcoll" ||
1502 Name == "strcasecmp" ||
1503 Name == "strncasecmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001504 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001505 !FTy->getParamType(0)->isPointerTy() ||
1506 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001507 continue;
1508 setOnlyReadsMemory(F);
1509 setDoesNotThrow(F);
1510 setDoesNotCapture(F, 1);
1511 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001512 } else if (Name == "strstr" ||
1513 Name == "strpbrk") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001514 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001515 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001516 continue;
1517 setOnlyReadsMemory(F);
1518 setDoesNotThrow(F);
1519 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001520 } else if (Name == "strtok" ||
1521 Name == "strtok_r") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001522 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001523 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001524 continue;
1525 setDoesNotThrow(F);
1526 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001527 } else if (Name == "scanf" ||
1528 Name == "setbuf" ||
1529 Name == "setvbuf") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001530 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001531 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001532 continue;
1533 setDoesNotThrow(F);
1534 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001535 } else if (Name == "strdup" ||
1536 Name == "strndup") {
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001537 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001538 !FTy->getReturnType()->isPointerTy() ||
1539 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001540 continue;
1541 setDoesNotThrow(F);
1542 setDoesNotAlias(F, 0);
1543 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001544 } else if (Name == "stat" ||
1545 Name == "sscanf" ||
1546 Name == "sprintf" ||
1547 Name == "statvfs") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001548 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001549 !FTy->getParamType(0)->isPointerTy() ||
1550 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001551 continue;
1552 setDoesNotThrow(F);
1553 setDoesNotCapture(F, 1);
1554 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001555 } else if (Name == "snprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001556 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001557 !FTy->getParamType(0)->isPointerTy() ||
1558 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001559 continue;
1560 setDoesNotThrow(F);
1561 setDoesNotCapture(F, 1);
1562 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001563 } else if (Name == "setitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001564 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001565 !FTy->getParamType(1)->isPointerTy() ||
1566 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001567 continue;
1568 setDoesNotThrow(F);
1569 setDoesNotCapture(F, 2);
1570 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001571 } else if (Name == "system") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001572 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001573 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001574 continue;
1575 // May throw; "system" is a valid pthread cancellation point.
1576 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001577 }
1578 break;
1579 case 'm':
Victor Hernandez83d63912009-09-18 22:35:49 +00001580 if (Name == "malloc") {
1581 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001582 !FTy->getReturnType()->isPointerTy())
Victor Hernandez83d63912009-09-18 22:35:49 +00001583 continue;
1584 setDoesNotThrow(F);
1585 setDoesNotAlias(F, 0);
1586 } else if (Name == "memcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001587 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001588 !FTy->getParamType(0)->isPointerTy() ||
1589 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001590 continue;
1591 setOnlyReadsMemory(F);
1592 setDoesNotThrow(F);
1593 setDoesNotCapture(F, 1);
1594 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001595 } else if (Name == "memchr" ||
1596 Name == "memrchr") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001597 if (FTy->getNumParams() != 3)
1598 continue;
1599 setOnlyReadsMemory(F);
1600 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001601 } else if (Name == "modf" ||
1602 Name == "modff" ||
1603 Name == "modfl" ||
1604 Name == "memcpy" ||
1605 Name == "memccpy" ||
1606 Name == "memmove") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001607 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001608 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001609 continue;
1610 setDoesNotThrow(F);
1611 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001612 } else if (Name == "memalign") {
Duncan Sands1df98592010-02-16 11:11:14 +00001613 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001614 continue;
1615 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001616 } else if (Name == "mkdir" ||
1617 Name == "mktime") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001618 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001619 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001620 continue;
1621 setDoesNotThrow(F);
1622 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001623 }
1624 break;
1625 case 'r':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001626 if (Name == "realloc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001627 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001628 !FTy->getParamType(0)->isPointerTy() ||
1629 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001630 continue;
1631 setDoesNotThrow(F);
1632 setDoesNotAlias(F, 0);
1633 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001634 } else if (Name == "read") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001635 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001636 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001637 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001638 // May throw; "read" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001639 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001640 } else if (Name == "rmdir" ||
1641 Name == "rewind" ||
1642 Name == "remove" ||
1643 Name == "realpath") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001644 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001645 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001646 continue;
1647 setDoesNotThrow(F);
1648 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001649 } else if (Name == "rename" ||
1650 Name == "readlink") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001651 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001652 !FTy->getParamType(0)->isPointerTy() ||
1653 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001654 continue;
1655 setDoesNotThrow(F);
1656 setDoesNotCapture(F, 1);
1657 setDoesNotCapture(F, 2);
1658 }
1659 break;
1660 case 'w':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001661 if (Name == "write") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001662 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001663 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001664 continue;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001665 // May throw; "write" is a valid pthread cancellation point.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001666 setDoesNotCapture(F, 2);
1667 }
1668 break;
1669 case 'b':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001670 if (Name == "bcopy") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001671 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001672 !FTy->getParamType(0)->isPointerTy() ||
1673 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001674 continue;
1675 setDoesNotThrow(F);
1676 setDoesNotCapture(F, 1);
1677 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001678 } else if (Name == "bcmp") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001679 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001680 !FTy->getParamType(0)->isPointerTy() ||
1681 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001682 continue;
1683 setDoesNotThrow(F);
1684 setOnlyReadsMemory(F);
1685 setDoesNotCapture(F, 1);
1686 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001687 } else if (Name == "bzero") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001688 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001689 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001690 continue;
1691 setDoesNotThrow(F);
1692 setDoesNotCapture(F, 1);
1693 }
1694 break;
1695 case 'c':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001696 if (Name == "calloc") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001697 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001698 !FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001699 continue;
1700 setDoesNotThrow(F);
1701 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001702 } else if (Name == "chmod" ||
1703 Name == "chown" ||
1704 Name == "ctermid" ||
1705 Name == "clearerr" ||
1706 Name == "closedir") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001707 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001708 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001709 continue;
1710 setDoesNotThrow(F);
1711 setDoesNotCapture(F, 1);
1712 }
1713 break;
1714 case 'a':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001715 if (Name == "atoi" ||
1716 Name == "atol" ||
1717 Name == "atof" ||
1718 Name == "atoll") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001719 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001720 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001721 continue;
1722 setDoesNotThrow(F);
1723 setOnlyReadsMemory(F);
1724 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001725 } else if (Name == "access") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001726 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001727 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001728 continue;
1729 setDoesNotThrow(F);
1730 setDoesNotCapture(F, 1);
1731 }
1732 break;
1733 case 'f':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001734 if (Name == "fopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001735 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001736 !FTy->getReturnType()->isPointerTy() ||
1737 !FTy->getParamType(0)->isPointerTy() ||
1738 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001739 continue;
1740 setDoesNotThrow(F);
1741 setDoesNotAlias(F, 0);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001742 setDoesNotCapture(F, 1);
1743 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001744 } else if (Name == "fdopen") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001745 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001746 !FTy->getReturnType()->isPointerTy() ||
1747 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001748 continue;
1749 setDoesNotThrow(F);
1750 setDoesNotAlias(F, 0);
1751 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001752 } else if (Name == "feof" ||
1753 Name == "free" ||
1754 Name == "fseek" ||
1755 Name == "ftell" ||
1756 Name == "fgetc" ||
1757 Name == "fseeko" ||
1758 Name == "ftello" ||
1759 Name == "fileno" ||
1760 Name == "fflush" ||
1761 Name == "fclose" ||
1762 Name == "fsetpos" ||
1763 Name == "flockfile" ||
1764 Name == "funlockfile" ||
1765 Name == "ftrylockfile") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001766 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001767 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001768 continue;
1769 setDoesNotThrow(F);
1770 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001771 } else if (Name == "ferror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001772 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001773 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001774 continue;
1775 setDoesNotThrow(F);
1776 setDoesNotCapture(F, 1);
1777 setOnlyReadsMemory(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001778 } else if (Name == "fputc" ||
1779 Name == "fstat" ||
1780 Name == "frexp" ||
1781 Name == "frexpf" ||
1782 Name == "frexpl" ||
1783 Name == "fstatvfs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001784 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001785 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001786 continue;
1787 setDoesNotThrow(F);
1788 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001789 } else if (Name == "fgets") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001790 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001791 !FTy->getParamType(0)->isPointerTy() ||
1792 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001793 continue;
1794 setDoesNotThrow(F);
1795 setDoesNotCapture(F, 3);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001796 } else if (Name == "fread" ||
1797 Name == "fwrite") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001798 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001799 !FTy->getParamType(0)->isPointerTy() ||
1800 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001801 continue;
1802 setDoesNotThrow(F);
1803 setDoesNotCapture(F, 1);
1804 setDoesNotCapture(F, 4);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001805 } else if (Name == "fputs" ||
1806 Name == "fscanf" ||
1807 Name == "fprintf" ||
1808 Name == "fgetpos") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001809 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001810 !FTy->getParamType(0)->isPointerTy() ||
1811 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001812 continue;
1813 setDoesNotThrow(F);
1814 setDoesNotCapture(F, 1);
1815 setDoesNotCapture(F, 2);
1816 }
1817 break;
1818 case 'g':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001819 if (Name == "getc" ||
1820 Name == "getlogin_r" ||
1821 Name == "getc_unlocked") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001822 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001823 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001824 continue;
1825 setDoesNotThrow(F);
1826 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001827 } else if (Name == "getenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001828 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001829 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001830 continue;
1831 setDoesNotThrow(F);
1832 setOnlyReadsMemory(F);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001833 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001834 } else if (Name == "gets" ||
1835 Name == "getchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001836 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001837 } else if (Name == "getitimer") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001838 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001839 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001840 continue;
1841 setDoesNotThrow(F);
1842 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001843 } else if (Name == "getpwnam") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001844 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001845 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001846 continue;
1847 setDoesNotThrow(F);
1848 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001849 }
1850 break;
1851 case 'u':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001852 if (Name == "ungetc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001853 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001854 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001855 continue;
1856 setDoesNotThrow(F);
1857 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001858 } else if (Name == "uname" ||
1859 Name == "unlink" ||
1860 Name == "unsetenv") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001861 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001862 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001863 continue;
1864 setDoesNotThrow(F);
1865 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001866 } else if (Name == "utime" ||
1867 Name == "utimes") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001868 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001869 !FTy->getParamType(0)->isPointerTy() ||
1870 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001871 continue;
1872 setDoesNotThrow(F);
1873 setDoesNotCapture(F, 1);
1874 setDoesNotCapture(F, 2);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001875 }
1876 break;
1877 case 'p':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001878 if (Name == "putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001879 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001880 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001881 continue;
1882 setDoesNotThrow(F);
1883 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001884 } else if (Name == "puts" ||
1885 Name == "printf" ||
1886 Name == "perror") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001887 if (FTy->getNumParams() != 1 ||
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 == "pread" ||
1893 Name == "pwrite") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001894 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001895 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001896 continue;
1897 // May throw; these are valid pthread cancellation points.
1898 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001899 } else if (Name == "putchar") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001900 setDoesNotThrow(F);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001901 } else if (Name == "popen") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001902 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001903 !FTy->getReturnType()->isPointerTy() ||
1904 !FTy->getParamType(0)->isPointerTy() ||
1905 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001906 continue;
1907 setDoesNotThrow(F);
1908 setDoesNotAlias(F, 0);
1909 setDoesNotCapture(F, 1);
1910 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001911 } else if (Name == "pclose") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001912 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001913 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001914 continue;
1915 setDoesNotThrow(F);
1916 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001917 }
1918 break;
1919 case 'v':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001920 if (Name == "vscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001921 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001922 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001923 continue;
1924 setDoesNotThrow(F);
1925 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001926 } else if (Name == "vsscanf" ||
1927 Name == "vfscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001928 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001929 !FTy->getParamType(1)->isPointerTy() ||
1930 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001931 continue;
1932 setDoesNotThrow(F);
1933 setDoesNotCapture(F, 1);
1934 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001935 } else if (Name == "valloc") {
Duncan Sands1df98592010-02-16 11:11:14 +00001936 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001937 continue;
1938 setDoesNotThrow(F);
1939 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001940 } else if (Name == "vprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001941 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001942 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001943 continue;
1944 setDoesNotThrow(F);
1945 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001946 } else if (Name == "vfprintf" ||
1947 Name == "vsprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001948 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001949 !FTy->getParamType(0)->isPointerTy() ||
1950 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001951 continue;
1952 setDoesNotThrow(F);
1953 setDoesNotCapture(F, 1);
1954 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001955 } else if (Name == "vsnprintf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001956 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001957 !FTy->getParamType(0)->isPointerTy() ||
1958 !FTy->getParamType(2)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00001959 continue;
1960 setDoesNotThrow(F);
1961 setDoesNotCapture(F, 1);
1962 setDoesNotCapture(F, 3);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001963 }
1964 break;
1965 case 'o':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001966 if (Name == "open") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001967 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001968 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001969 continue;
1970 // May throw; "open" is a valid pthread cancellation point.
1971 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001972 } else if (Name == "opendir") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001973 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001974 !FTy->getReturnType()->isPointerTy() ||
1975 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001976 continue;
1977 setDoesNotThrow(F);
1978 setDoesNotAlias(F, 0);
Nick Lewycky225f7472009-02-15 22:47:25 +00001979 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001980 }
1981 break;
1982 case 't':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001983 if (Name == "tmpfile") {
Duncan Sands1df98592010-02-16 11:11:14 +00001984 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001985 continue;
1986 setDoesNotThrow(F);
1987 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001988 } else if (Name == "times") {
Nick Lewycky225f7472009-02-15 22:47:25 +00001989 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00001990 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00001991 continue;
1992 setDoesNotThrow(F);
1993 setDoesNotCapture(F, 1);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001994 }
Nick Lewycky225f7472009-02-15 22:47:25 +00001995 break;
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001996 case 'h':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001997 if (Name == "htonl" ||
1998 Name == "htons") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001999 setDoesNotThrow(F);
2000 setDoesNotAccessMemory(F);
2001 }
2002 break;
2003 case 'n':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002004 if (Name == "ntohl" ||
2005 Name == "ntohs") {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002006 setDoesNotThrow(F);
2007 setDoesNotAccessMemory(F);
2008 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002009 break;
2010 case 'l':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002011 if (Name == "lstat") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002012 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002013 !FTy->getParamType(0)->isPointerTy() ||
2014 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002015 continue;
2016 setDoesNotThrow(F);
2017 setDoesNotCapture(F, 1);
2018 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002019 } else if (Name == "lchown") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002020 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002021 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002022 continue;
2023 setDoesNotThrow(F);
2024 setDoesNotCapture(F, 1);
2025 }
2026 break;
2027 case 'q':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002028 if (Name == "qsort") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002029 if (FTy->getNumParams() != 4 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002030 !FTy->getParamType(3)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002031 continue;
2032 // May throw; places call through function pointer.
2033 setDoesNotCapture(F, 4);
2034 }
2035 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002036 case '_':
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002037 if (Name == "__strdup" ||
2038 Name == "__strndup") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002039 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002040 !FTy->getReturnType()->isPointerTy() ||
2041 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002042 continue;
2043 setDoesNotThrow(F);
2044 setDoesNotAlias(F, 0);
2045 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002046 } else if (Name == "__strtok_r") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002047 if (FTy->getNumParams() != 3 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002048 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002049 continue;
2050 setDoesNotThrow(F);
2051 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002052 } else if (Name == "_IO_getc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002053 if (FTy->getNumParams() != 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002054 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002055 continue;
2056 setDoesNotThrow(F);
2057 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002058 } else if (Name == "_IO_putc") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002059 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002060 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002061 continue;
2062 setDoesNotThrow(F);
2063 setDoesNotCapture(F, 2);
2064 }
Nick Lewycky225f7472009-02-15 22:47:25 +00002065 break;
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002066 case 1:
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002067 if (Name == "\1__isoc99_scanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002068 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002069 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002070 continue;
2071 setDoesNotThrow(F);
2072 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002073 } else if (Name == "\1stat64" ||
2074 Name == "\1lstat64" ||
2075 Name == "\1statvfs64" ||
2076 Name == "\1__isoc99_sscanf") {
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002077 if (FTy->getNumParams() < 1 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002078 !FTy->getParamType(0)->isPointerTy() ||
2079 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002080 continue;
2081 setDoesNotThrow(F);
2082 setDoesNotCapture(F, 1);
2083 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002084 } else if (Name == "\1fopen64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002085 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002086 !FTy->getReturnType()->isPointerTy() ||
2087 !FTy->getParamType(0)->isPointerTy() ||
2088 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002089 continue;
2090 setDoesNotThrow(F);
2091 setDoesNotAlias(F, 0);
2092 setDoesNotCapture(F, 1);
2093 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002094 } else if (Name == "\1fseeko64" ||
2095 Name == "\1ftello64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002096 if (FTy->getNumParams() == 0 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002097 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002098 continue;
2099 setDoesNotThrow(F);
2100 setDoesNotCapture(F, 1);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002101 } else if (Name == "\1tmpfile64") {
Duncan Sands1df98592010-02-16 11:11:14 +00002102 if (!FTy->getReturnType()->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002103 continue;
2104 setDoesNotThrow(F);
2105 setDoesNotAlias(F, 0);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002106 } else if (Name == "\1fstat64" ||
2107 Name == "\1fstatvfs64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002108 if (FTy->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002109 !FTy->getParamType(1)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002110 continue;
2111 setDoesNotThrow(F);
2112 setDoesNotCapture(F, 2);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00002113 } else if (Name == "\1open64") {
Nick Lewycky225f7472009-02-15 22:47:25 +00002114 if (FTy->getNumParams() < 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +00002115 !FTy->getParamType(0)->isPointerTy())
Nick Lewycky225f7472009-02-15 22:47:25 +00002116 continue;
2117 // May throw; "open" is a valid pthread cancellation point.
2118 setDoesNotCapture(F, 1);
Nick Lewycky0b6679d2009-01-18 04:34:36 +00002119 }
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002120 break;
2121 }
2122 }
2123 return Modified;
2124}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002125
2126// TODO:
2127// Additional cases that we need to add to this file:
2128//
2129// cbrt:
2130// * cbrt(expN(X)) -> expN(x/3)
2131// * cbrt(sqrt(x)) -> pow(x,1/6)
2132// * cbrt(sqrt(x)) -> pow(x,1/9)
2133//
2134// cos, cosf, cosl:
2135// * cos(-x) -> cos(x)
2136//
2137// exp, expf, expl:
2138// * exp(log(x)) -> x
2139//
2140// log, logf, logl:
2141// * log(exp(x)) -> x
2142// * log(x**y) -> y*log(x)
2143// * log(exp(y)) -> y*log(e)
2144// * log(exp2(y)) -> y*log(2)
2145// * log(exp10(y)) -> y*log(10)
2146// * log(sqrt(x)) -> 0.5*log(x)
2147// * log(pow(x,y)) -> y*log(x)
2148//
2149// lround, lroundf, lroundl:
2150// * lround(cnst) -> cnst'
2151//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002152// pow, powf, powl:
2153// * pow(exp(x),y) -> exp(x*y)
2154// * pow(sqrt(x),y) -> pow(x,y*0.5)
2155// * pow(pow(x,y),z)-> pow(x,y*z)
2156//
2157// puts:
2158// * puts("") -> putchar("\n")
2159//
2160// round, roundf, roundl:
2161// * round(cnst) -> cnst'
2162//
2163// signbit:
2164// * signbit(cnst) -> cnst'
2165// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2166//
2167// sqrt, sqrtf, sqrtl:
2168// * sqrt(expN(x)) -> expN(x*0.5)
2169// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2170// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2171//
2172// stpcpy:
2173// * stpcpy(str, "literal") ->
2174// llvm.memcpy(str,"literal",strlen("literal")+1,1)
2175// strrchr:
2176// * strrchr(s,c) -> reverse_offset_of_in(c,s)
2177// (if c is a constant integer and s is a constant string)
2178// * strrchr(s1,0) -> strchr(s1,0)
2179//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002180// strpbrk:
2181// * strpbrk(s,a) -> offset_in_for(s,a)
2182// (if s and a are both constant strings)
2183// * strpbrk(s,"") -> 0
2184// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2185//
2186// strspn, strcspn:
2187// * strspn(s,a) -> const_int (if both args are constant)
2188// * strspn("",a) -> 0
2189// * strspn(s,"") -> 0
2190// * strcspn(s,a) -> const_int (if both args are constant)
2191// * strcspn("",a) -> 0
2192// * strcspn(s,"") -> strlen(a)
2193//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002194// tan, tanf, tanl:
2195// * tan(atan(x)) -> x
2196//
2197// trunc, truncf, truncl:
2198// * trunc(cnst) -> cnst'
2199//
2200//