blob: 9f136d4e3077a2821469d5454b2ca7fa6af008a5 [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"
Chris Lattnerafbf4832011-02-24 07:16:14 +000028#include "llvm/Target/TargetLibraryInfo.h"
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000029#include "llvm/ADT/SmallPtrSet.h"
30#include "llvm/ADT/StringMap.h"
31#include "llvm/ADT/Statistic.h"
Daniel Dunbar473955f2009-07-29 22:00:43 +000032#include "llvm/ADT/STLExtras.h"
Chris Lattner56b4f2b2008-05-01 06:39:12 +000033#include "llvm/Support/Debug.h"
Daniel Dunbarf0443c12009-07-26 08:34:35 +000034#include "llvm/Support/raw_ostream.h"
Chris Lattnerafbf4832011-02-24 07:16:14 +000035#include "llvm/Config/config.h" // FIXME: Shouldn't depend on host!
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000036using namespace llvm;
37
38STATISTIC(NumSimplified, "Number of library calls simplified");
Nick Lewycky0f8df9a2009-01-04 20:27:34 +000039STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000040
41//===----------------------------------------------------------------------===//
42// Optimizer Base Class
43//===----------------------------------------------------------------------===//
44
45/// This class is the abstract base class for the set of optimizations that
46/// corresponds to one library call.
47namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +000048class LibCallOptimization {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000049protected:
50 Function *Caller;
51 const TargetData *TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000052 LLVMContext* Context;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000053public:
Evan Chengeb8c6452010-03-24 20:19:04 +000054 LibCallOptimization() { }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000055 virtual ~LibCallOptimization() {}
56
57 /// CallOptimizer - This pure virtual method is implemented by base classes to
58 /// do various optimizations. If this returns null then no transformation was
59 /// performed. If it returns CI, then it transformed the call and CI is to be
60 /// deleted. If it returns something else, replace CI with the new value and
61 /// delete CI.
Eric Christopher37c8b862009-10-07 21:14:25 +000062 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
Eric Christopher7a61d702008-08-08 19:39:37 +000063 =0;
Eric Christopher37c8b862009-10-07 21:14:25 +000064
Dan Gohmanf14d9192009-08-18 00:48:13 +000065 Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000066 Caller = CI->getParent()->getParent();
Dan Gohmanf14d9192009-08-18 00:48:13 +000067 this->TD = TD;
Owen Andersonfa5cbd62009-07-03 19:42:02 +000068 if (CI->getCalledFunction())
Owen Andersone922c022009-07-22 00:24:57 +000069 Context = &CI->getCalledFunction()->getContext();
Rafael Espindolae96af562010-06-16 19:34:01 +000070
71 // We never change the calling convention.
72 if (CI->getCallingConv() != llvm::CallingConv::C)
73 return NULL;
74
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000075 return CallOptimizer(CI->getCalledFunction(), CI, B);
76 }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000077};
78} // End anonymous namespace.
79
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000080
81//===----------------------------------------------------------------------===//
82// Helper Functions
83//===----------------------------------------------------------------------===//
84
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000085/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Eric Christopher37c8b862009-10-07 21:14:25 +000086/// value is equal or not-equal to zero.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +000087static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
88 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
89 UI != E; ++UI) {
90 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
91 if (IC->isEquality())
92 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
93 if (C->isNullValue())
94 continue;
95 // Unknown instruction.
96 return false;
97 }
98 return true;
99}
100
Benjamin Kramer386e9182010-06-15 21:34:25 +0000101/// IsOnlyUsedInEqualityComparison - Return true if it is only used in equality
102/// comparisons with With.
103static bool IsOnlyUsedInEqualityComparison(Value *V, Value *With) {
104 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
105 UI != E; ++UI) {
106 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
107 if (IC->isEquality() && IC->getOperand(1) == With)
108 continue;
109 // Unknown instruction.
110 return false;
111 }
112 return true;
113}
114
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000115//===----------------------------------------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000116// String and Memory LibCall Optimizations
117//===----------------------------------------------------------------------===//
118
119//===---------------------------------------===//
120// 'strcat' Optimizations
Chris Lattnere9f9a7e2009-09-03 05:19:59 +0000121namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000122struct StrCatOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000123 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000124 // Verify the "strcat" function prototype.
125 const FunctionType *FT = Callee->getFunctionType();
126 if (FT->getNumParams() != 2 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000127 FT->getReturnType() != B.getInt8PtrTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000128 FT->getParamType(0) != FT->getReturnType() ||
129 FT->getParamType(1) != FT->getReturnType())
130 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000131
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000132 // Extract some information from the instruction
Gabor Greifaee5dc12010-06-24 10:42:46 +0000133 Value *Dst = CI->getArgOperand(0);
134 Value *Src = CI->getArgOperand(1);
Eric Christopher37c8b862009-10-07 21:14:25 +0000135
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000136 // See if we can get the length of the input string.
137 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000138 if (Len == 0) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000139 --Len; // Unbias length.
Eric Christopher37c8b862009-10-07 21:14:25 +0000140
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000141 // Handle the simple, do-nothing case: strcat(x, "") -> x
142 if (Len == 0)
143 return Dst;
Dan Gohmanf14d9192009-08-18 00:48:13 +0000144
145 // These optimizations require TargetData.
146 if (!TD) return 0;
147
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000148 EmitStrLenMemCpy(Src, Dst, Len, B);
149 return Dst;
150 }
151
152 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000153 // We need to find the end of the destination string. That's where the
154 // memory is to be moved to. We just generate a call to strlen.
Eric Christopherb6174e32010-03-05 22:25:30 +0000155 Value *DstLen = EmitStrLen(Dst, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +0000156
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000157 // Now that we have the destination's length, we must index into the
158 // destination's pointer to get the actual memcpy destination (end of
159 // the string .. we're concatenating).
Ed Schoutenb5e0a962009-04-06 13:06:48 +0000160 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
Eric Christopher37c8b862009-10-07 21:14:25 +0000161
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000162 // We have enough information to now generate the memcpy call to do the
163 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000164 B.CreateMemCpy(CpyDst, Src,
165 ConstantInt::get(TD->getIntPtrType(*Context), Len + 1), 1);
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 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000177 FT->getReturnType() != B.getInt8PtrTy() ||
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 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000225 FT->getReturnType() != B.getInt8PtrTy() ||
Benjamin Kramer4c756792010-09-30 11:21:59 +0000226 FT->getParamType(0) != FT->getReturnType() ||
227 !FT->getParamType(1)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000228 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000229
Gabor Greifaee5dc12010-06-24 10:42:46 +0000230 Value *SrcStr = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000231
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000232 // If the second operand is non-constant, see if we can compute the length
233 // of the input string and turn this into memchr.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000234 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000235 if (CharC == 0) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000236 // These optimizations require TargetData.
237 if (!TD) return 0;
238
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000239 uint64_t Len = GetStringLength(SrcStr);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000240 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000241 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000242
Gabor Greifaee5dc12010-06-24 10:42:46 +0000243 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
Eric Christopherb6174e32010-03-05 22:25:30 +0000244 ConstantInt::get(TD->getIntPtrType(*Context), Len),
245 B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000246 }
247
248 // Otherwise, the character is a constant, see if the first argument is
249 // a string literal. If so, we can constant fold.
Bill Wendling0582ae92009-03-13 04:39:26 +0000250 std::string Str;
251 if (!GetConstantStringInfo(SrcStr, Str))
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000252 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000253
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000254 // strchr can find the nul character.
255 Str += '\0';
Eric Christopher37c8b862009-10-07 21:14:25 +0000256
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000257 // Compute the offset.
Benjamin Kramere2609902010-09-29 22:29:12 +0000258 size_t I = Str.find(CharC->getSExtValue());
259 if (I == std::string::npos) // Didn't find the char. strchr returns null.
260 return Constant::getNullValue(CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000261
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000262 // strchr(s+n,c) -> gep(s+n+i,c)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000263 return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000264 }
265};
266
267//===---------------------------------------===//
Benjamin Kramer06f25cf2010-09-29 21:50:51 +0000268// 'strrchr' Optimizations
269
270struct StrRChrOpt : public LibCallOptimization {
271 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
272 // Verify the "strrchr" function prototype.
273 const FunctionType *FT = Callee->getFunctionType();
274 if (FT->getNumParams() != 2 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000275 FT->getReturnType() != B.getInt8PtrTy() ||
Benjamin Kramer4c756792010-09-30 11:21:59 +0000276 FT->getParamType(0) != FT->getReturnType() ||
277 !FT->getParamType(1)->isIntegerTy(32))
Benjamin Kramer06f25cf2010-09-29 21:50:51 +0000278 return 0;
279
280 Value *SrcStr = CI->getArgOperand(0);
281 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
282
283 // Cannot fold anything if we're not looking for a constant.
284 if (!CharC)
285 return 0;
286
287 std::string Str;
288 if (!GetConstantStringInfo(SrcStr, Str)) {
289 // strrchr(s, 0) -> strchr(s, 0)
290 if (TD && CharC->isZero())
291 return EmitStrChr(SrcStr, '\0', B, TD);
292 return 0;
293 }
294
295 // strrchr can find the nul character.
296 Str += '\0';
297
298 // Compute the offset.
299 size_t I = Str.rfind(CharC->getSExtValue());
300 if (I == std::string::npos) // Didn't find the char. Return null.
301 return Constant::getNullValue(CI->getType());
302
303 // strrchr(s+n,c) -> gep(s+n+i,c)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000304 return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");
Benjamin Kramer06f25cf2010-09-29 21:50:51 +0000305 }
306};
307
308//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000309// 'strcmp' Optimizations
310
Chris Lattner3e8b6632009-09-02 06:11:42 +0000311struct StrCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000312 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000313 // Verify the "strcmp" function prototype.
314 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000315 if (FT->getNumParams() != 2 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000316 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000317 FT->getParamType(0) != FT->getParamType(1) ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000318 FT->getParamType(0) != B.getInt8PtrTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000319 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000320
Gabor Greifaee5dc12010-06-24 10:42:46 +0000321 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000322 if (Str1P == Str2P) // strcmp(x,x) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000323 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000324
Bill Wendling0582ae92009-03-13 04:39:26 +0000325 std::string Str1, Str2;
326 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
327 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000328
Bill Wendling0582ae92009-03-13 04:39:26 +0000329 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000330 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000331
Bill Wendling0582ae92009-03-13 04:39:26 +0000332 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000333 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000334
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000335 // strcmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000336 if (HasStr1 && HasStr2)
Eric Christopher37c8b862009-10-07 21:14:25 +0000337 return ConstantInt::get(CI->getType(),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000338 strcmp(Str1.c_str(),Str2.c_str()));
Nick Lewycky13a09e22008-12-21 00:19:21 +0000339
340 // strcmp(P, "x") -> memcmp(P, "x", 2)
341 uint64_t Len1 = GetStringLength(Str1P);
342 uint64_t Len2 = GetStringLength(Str2P);
Chris Lattner849832c2009-06-19 04:17:36 +0000343 if (Len1 && Len2) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000344 // These optimizations require TargetData.
345 if (!TD) return 0;
346
Nick Lewycky13a09e22008-12-21 00:19:21 +0000347 return EmitMemCmp(Str1P, Str2P,
Owen Anderson1d0be152009-08-13 21:58:54 +0000348 ConstantInt::get(TD->getIntPtrType(*Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000349 std::min(Len1, Len2)), B, TD);
Nick Lewycky13a09e22008-12-21 00:19:21 +0000350 }
351
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000352 return 0;
353 }
354};
355
356//===---------------------------------------===//
357// 'strncmp' Optimizations
358
Chris Lattner3e8b6632009-09-02 06:11:42 +0000359struct StrNCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000360 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000361 // Verify the "strncmp" function prototype.
362 const FunctionType *FT = Callee->getFunctionType();
Eric Christopher37c8b862009-10-07 21:14:25 +0000363 if (FT->getNumParams() != 3 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000364 !FT->getReturnType()->isIntegerTy(32) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000365 FT->getParamType(0) != FT->getParamType(1) ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000366 FT->getParamType(0) != B.getInt8PtrTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000367 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000368 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000369
Gabor Greifaee5dc12010-06-24 10:42:46 +0000370 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000371 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000372 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000373
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000374 // Get the length argument if it is constant.
375 uint64_t Length;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000376 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000377 Length = LengthArg->getZExtValue();
378 else
379 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000380
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000381 if (Length == 0) // strncmp(x,y,0) -> 0
Owen Andersoneed707b2009-07-24 23:12:02 +0000382 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000383
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000384 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000385 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD);
Benjamin Kramerea9ca022010-06-16 10:30:29 +0000386
Bill Wendling0582ae92009-03-13 04:39:26 +0000387 std::string Str1, Str2;
388 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
389 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
Eric Christopher37c8b862009-10-07 21:14:25 +0000390
Bill Wendling0582ae92009-03-13 04:39:26 +0000391 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000392 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000393
Bill Wendling0582ae92009-03-13 04:39:26 +0000394 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000395 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
Eric Christopher37c8b862009-10-07 21:14:25 +0000396
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000397 // strncmp(x, y) -> cnst (if both x and y are constant strings)
Bill Wendling0582ae92009-03-13 04:39:26 +0000398 if (HasStr1 && HasStr2)
Owen Andersoneed707b2009-07-24 23:12:02 +0000399 return ConstantInt::get(CI->getType(),
Bill Wendling0582ae92009-03-13 04:39:26 +0000400 strncmp(Str1.c_str(), Str2.c_str(), Length));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000401 return 0;
402 }
403};
404
405
406//===---------------------------------------===//
407// 'strcpy' Optimizations
408
Chris Lattner3e8b6632009-09-02 06:11:42 +0000409struct StrCpyOpt : public LibCallOptimization {
Evan Chengeb8c6452010-03-24 20:19:04 +0000410 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
411
412 StrCpyOpt(bool c) : OptChkCall(c) {}
413
Eric Christopher7a61d702008-08-08 19:39:37 +0000414 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000415 // Verify the "strcpy" function prototype.
Evan Cheng0289b412010-03-23 15:48:04 +0000416 unsigned NumParams = OptChkCall ? 3 : 2;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000417 const FunctionType *FT = Callee->getFunctionType();
Evan Cheng0289b412010-03-23 15:48:04 +0000418 if (FT->getNumParams() != NumParams ||
419 FT->getReturnType() != FT->getParamType(0) ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000420 FT->getParamType(0) != FT->getParamType(1) ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000421 FT->getParamType(0) != B.getInt8PtrTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000422 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000423
Gabor Greifaee5dc12010-06-24 10:42:46 +0000424 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000425 if (Dst == Src) // strcpy(x,x) -> x
426 return Src;
Eric Christopher37c8b862009-10-07 21:14:25 +0000427
Dan Gohmanf14d9192009-08-18 00:48:13 +0000428 // These optimizations require TargetData.
429 if (!TD) return 0;
430
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000431 // See if we can get the length of the input string.
432 uint64_t Len = GetStringLength(Src);
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000433 if (Len == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000434
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000435 // We have enough information to now generate the memcpy call to do the
436 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Evan Cheng0289b412010-03-23 15:48:04 +0000437 if (OptChkCall)
438 EmitMemCpyChk(Dst, Src,
439 ConstantInt::get(TD->getIntPtrType(*Context), Len),
Gabor Greifaee5dc12010-06-24 10:42:46 +0000440 CI->getArgOperand(2), B, TD);
Evan Cheng0289b412010-03-23 15:48:04 +0000441 else
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000442 B.CreateMemCpy(Dst, Src,
443 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000444 return Dst;
445 }
446};
447
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000448//===---------------------------------------===//
449// 'strncpy' Optimizations
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000450
Chris Lattner3e8b6632009-09-02 06:11:42 +0000451struct StrNCpyOpt : public LibCallOptimization {
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000452 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
453 const FunctionType *FT = Callee->getFunctionType();
454 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
455 FT->getParamType(0) != FT->getParamType(1) ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000456 FT->getParamType(0) != B.getInt8PtrTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000457 !FT->getParamType(2)->isIntegerTy())
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000458 return 0;
459
Gabor Greifaee5dc12010-06-24 10:42:46 +0000460 Value *Dst = CI->getArgOperand(0);
461 Value *Src = CI->getArgOperand(1);
462 Value *LenOp = CI->getArgOperand(2);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000463
464 // See if we can get the length of the input string.
465 uint64_t SrcLen = GetStringLength(Src);
466 if (SrcLen == 0) return 0;
467 --SrcLen;
468
469 if (SrcLen == 0) {
470 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000471 B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000472 return Dst;
473 }
474
475 uint64_t Len;
476 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
477 Len = LengthArg->getZExtValue();
478 else
479 return 0;
480
481 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
482
Dan Gohmanf14d9192009-08-18 00:48:13 +0000483 // These optimizations require TargetData.
484 if (!TD) return 0;
485
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000486 // Let strncpy handle the zero padding
487 if (Len > SrcLen+1) return 0;
488
489 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000490 B.CreateMemCpy(Dst, Src,
491 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
Chris Lattnerf5b6bc72009-04-12 05:06:39 +0000492
493 return Dst;
494 }
495};
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000496
497//===---------------------------------------===//
498// 'strlen' Optimizations
499
Chris Lattner3e8b6632009-09-02 06:11:42 +0000500struct StrLenOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000501 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000502 const FunctionType *FT = Callee->getFunctionType();
503 if (FT->getNumParams() != 1 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000504 FT->getParamType(0) != B.getInt8PtrTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000505 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000506 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000507
Gabor Greifaee5dc12010-06-24 10:42:46 +0000508 Value *Src = CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000509
510 // Constant folding: strlen("xyz") -> 3
511 if (uint64_t Len = GetStringLength(Src))
Owen Andersoneed707b2009-07-24 23:12:02 +0000512 return ConstantInt::get(CI->getType(), Len-1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000513
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000514 // strlen(x) != 0 --> *x != 0
515 // strlen(x) == 0 --> *x == 0
Chris Lattner98d67d72009-12-23 23:24:51 +0000516 if (IsOnlyUsedInZeroEqualityComparison(CI))
517 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
518 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000519 }
520};
521
Benjamin Kramer05f585e2010-09-29 23:52:12 +0000522
523//===---------------------------------------===//
524// 'strpbrk' Optimizations
525
526struct StrPBrkOpt : public LibCallOptimization {
527 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
528 const FunctionType *FT = Callee->getFunctionType();
529 if (FT->getNumParams() != 2 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000530 FT->getParamType(0) != B.getInt8PtrTy() ||
Benjamin Kramer05f585e2010-09-29 23:52:12 +0000531 FT->getParamType(1) != FT->getParamType(0) ||
532 FT->getReturnType() != FT->getParamType(0))
533 return 0;
534
535 std::string S1, S2;
536 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
537 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
538
539 // strpbrk(s, "") -> NULL
540 // strpbrk("", s) -> NULL
541 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
542 return Constant::getNullValue(CI->getType());
543
544 // Constant folding.
545 if (HasS1 && HasS2) {
546 size_t I = S1.find_first_of(S2);
547 if (I == std::string::npos) // No match.
548 return Constant::getNullValue(CI->getType());
549
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000550 return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");
Benjamin Kramer05f585e2010-09-29 23:52:12 +0000551 }
552
553 // strpbrk(s, "a") -> strchr(s, 'a')
554 if (TD && HasS2 && S2.size() == 1)
555 return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD);
556
557 return 0;
558 }
559};
560
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000561//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000562// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
Nick Lewycky4c498412009-02-13 15:31:46 +0000563
Chris Lattner3e8b6632009-09-02 06:11:42 +0000564struct StrToOpt : public LibCallOptimization {
Nick Lewycky4c498412009-02-13 15:31:46 +0000565 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
566 const FunctionType *FT = Callee->getFunctionType();
567 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000568 !FT->getParamType(0)->isPointerTy() ||
569 !FT->getParamType(1)->isPointerTy())
Nick Lewycky4c498412009-02-13 15:31:46 +0000570 return 0;
571
Gabor Greifaee5dc12010-06-24 10:42:46 +0000572 Value *EndPtr = CI->getArgOperand(1);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000573 if (isa<ConstantPointerNull>(EndPtr)) {
Dan Gohmanc32046e2010-12-17 01:09:43 +0000574 // With a null EndPtr, this function won't capture the main argument.
575 // It would be readonly too, except that it still may write to errno.
Nick Lewycky4c498412009-02-13 15:31:46 +0000576 CI->addAttribute(1, Attribute::NoCapture);
Nick Lewycky02b6a6a2009-02-13 17:08:33 +0000577 }
Nick Lewycky4c498412009-02-13 15:31:46 +0000578
579 return 0;
580 }
581};
582
Chris Lattner24604112009-12-16 09:32:05 +0000583//===---------------------------------------===//
Benjamin Kramer9510a252010-09-30 00:58:35 +0000584// 'strspn' Optimizations
585
586struct StrSpnOpt : public LibCallOptimization {
587 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
588 const FunctionType *FT = Callee->getFunctionType();
589 if (FT->getNumParams() != 2 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000590 FT->getParamType(0) != B.getInt8PtrTy() ||
Benjamin Kramer9510a252010-09-30 00:58:35 +0000591 FT->getParamType(1) != FT->getParamType(0) ||
592 !FT->getReturnType()->isIntegerTy())
593 return 0;
594
595 std::string S1, S2;
596 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
597 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
598
599 // strspn(s, "") -> 0
600 // strspn("", s) -> 0
601 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
602 return Constant::getNullValue(CI->getType());
603
604 // Constant folding.
605 if (HasS1 && HasS2)
606 return ConstantInt::get(CI->getType(), strspn(S1.c_str(), S2.c_str()));
607
608 return 0;
609 }
610};
611
612//===---------------------------------------===//
613// 'strcspn' Optimizations
614
615struct StrCSpnOpt : public LibCallOptimization {
616 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
617 const FunctionType *FT = Callee->getFunctionType();
618 if (FT->getNumParams() != 2 ||
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000619 FT->getParamType(0) != B.getInt8PtrTy() ||
Benjamin Kramer9510a252010-09-30 00:58:35 +0000620 FT->getParamType(1) != FT->getParamType(0) ||
621 !FT->getReturnType()->isIntegerTy())
622 return 0;
623
624 std::string S1, S2;
625 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
626 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
627
628 // strcspn("", s) -> 0
629 if (HasS1 && S1.empty())
630 return Constant::getNullValue(CI->getType());
631
632 // Constant folding.
633 if (HasS1 && HasS2)
634 return ConstantInt::get(CI->getType(), strcspn(S1.c_str(), S2.c_str()));
635
636 // strcspn(s, "") -> strlen(s)
637 if (TD && HasS2 && S2.empty())
638 return EmitStrLen(CI->getArgOperand(0), B, TD);
639
640 return 0;
641 }
642};
643
644//===---------------------------------------===//
Chris Lattner24604112009-12-16 09:32:05 +0000645// 'strstr' Optimizations
646
647struct StrStrOpt : public LibCallOptimization {
648 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
649 const FunctionType *FT = Callee->getFunctionType();
650 if (FT->getNumParams() != 2 ||
Duncan Sands1df98592010-02-16 11:11:14 +0000651 !FT->getParamType(0)->isPointerTy() ||
652 !FT->getParamType(1)->isPointerTy() ||
653 !FT->getReturnType()->isPointerTy())
Chris Lattner24604112009-12-16 09:32:05 +0000654 return 0;
655
656 // fold strstr(x, x) -> x.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000657 if (CI->getArgOperand(0) == CI->getArgOperand(1))
658 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000659
Benjamin Kramer386e9182010-06-15 21:34:25 +0000660 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
Gabor Greif8e1ebff2010-06-30 12:42:43 +0000661 if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
662 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD);
663 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000664 StrLen, B, TD);
665 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
666 UI != UE; ) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000667 ICmpInst *Old = cast<ICmpInst>(*UI++);
Benjamin Kramer386e9182010-06-15 21:34:25 +0000668 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
669 ConstantInt::getNullValue(StrNCmp->getType()),
670 "cmp");
671 Old->replaceAllUsesWith(Cmp);
672 Old->eraseFromParent();
673 }
674 return CI;
675 }
676
Chris Lattner24604112009-12-16 09:32:05 +0000677 // See if either input string is a constant string.
678 std::string SearchStr, ToFindStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +0000679 bool HasStr1 = GetConstantStringInfo(CI->getArgOperand(0), SearchStr);
680 bool HasStr2 = GetConstantStringInfo(CI->getArgOperand(1), ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000681
Chris Lattner24604112009-12-16 09:32:05 +0000682 // fold strstr(x, "") -> x.
683 if (HasStr2 && ToFindStr.empty())
Gabor Greifaee5dc12010-06-24 10:42:46 +0000684 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000685
Chris Lattner24604112009-12-16 09:32:05 +0000686 // If both strings are known, constant fold it.
687 if (HasStr1 && HasStr2) {
688 std::string::size_type Offset = SearchStr.find(ToFindStr);
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000689
Chris Lattner24604112009-12-16 09:32:05 +0000690 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
691 return Constant::getNullValue(CI->getType());
692
693 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +0000694 Value *Result = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner24604112009-12-16 09:32:05 +0000695 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
696 return B.CreateBitCast(Result, CI->getType());
697 }
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000698
Chris Lattner24604112009-12-16 09:32:05 +0000699 // fold strstr(x, "y") -> strchr(x, 'y').
700 if (HasStr2 && ToFindStr.size() == 1)
Gabor Greifa3997812010-07-22 10:37:47 +0000701 return B.CreateBitCast(EmitStrChr(CI->getArgOperand(0),
702 ToFindStr[0], B, TD), CI->getType());
Chris Lattner24604112009-12-16 09:32:05 +0000703 return 0;
704 }
705};
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +0000706
Nick Lewycky4c498412009-02-13 15:31:46 +0000707
708//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000709// 'memcmp' Optimizations
710
Chris Lattner3e8b6632009-09-02 06:11:42 +0000711struct MemCmpOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000712 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000713 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +0000714 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
715 !FT->getParamType(1)->isPointerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000716 !FT->getReturnType()->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000717 return 0;
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000718
Gabor Greifaee5dc12010-06-24 10:42:46 +0000719 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000720
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000721 if (LHS == RHS) // memcmp(s,s,x) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000722 return Constant::getNullValue(CI->getType());
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000723
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000724 // Make sure we have a constant length.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000725 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattner56b4f2b2008-05-01 06:39:12 +0000726 if (!LenC) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000727 uint64_t Len = LenC->getZExtValue();
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000728
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000729 if (Len == 0) // memcmp(s1,s2,0) -> 0
Owen Andersona7235ea2009-07-31 20:28:14 +0000730 return Constant::getNullValue(CI->getType());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000731
Benjamin Kramer48aefe12010-05-25 22:53:43 +0000732 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
733 if (Len == 1) {
734 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
735 CI->getType(), "lhsv");
736 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
737 CI->getType(), "rhsv");
Benjamin Kramer1464c1d2010-05-26 09:45:04 +0000738 return B.CreateSub(LHSV, RHSV, "chardiff");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000739 }
Duncan Sandsec00fcb2008-05-19 09:27:24 +0000740
Benjamin Kramer992a6372009-11-05 17:44:22 +0000741 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
742 std::string LHSStr, RHSStr;
743 if (GetConstantStringInfo(LHS, LHSStr) &&
744 GetConstantStringInfo(RHS, RHSStr)) {
745 // Make sure we're not reading out-of-bounds memory.
746 if (Len > LHSStr.length() || Len > RHSStr.length())
747 return 0;
748 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
749 return ConstantInt::get(CI->getType(), Ret);
750 }
751
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000752 return 0;
753 }
754};
755
756//===---------------------------------------===//
757// 'memcpy' Optimizations
758
Chris Lattner3e8b6632009-09-02 06:11:42 +0000759struct MemCpyOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000760 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000761 // These optimizations require TargetData.
762 if (!TD) return 0;
763
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000764 const FunctionType *FT = Callee->getFunctionType();
765 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000766 !FT->getParamType(0)->isPointerTy() ||
767 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000768 FT->getParamType(2) != TD->getIntPtrType(*Context))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000769 return 0;
770
771 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000772 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
773 CI->getArgOperand(2), 1);
Gabor Greifaee5dc12010-06-24 10:42:46 +0000774 return CI->getArgOperand(0);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000775 }
776};
777
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000778//===---------------------------------------===//
779// 'memmove' Optimizations
780
Chris Lattner3e8b6632009-09-02 06:11:42 +0000781struct MemMoveOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000782 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000783 // These optimizations require TargetData.
784 if (!TD) return 0;
785
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000786 const FunctionType *FT = Callee->getFunctionType();
787 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000788 !FT->getParamType(0)->isPointerTy() ||
789 !FT->getParamType(1)->isPointerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000790 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000791 return 0;
792
793 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000794 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
795 CI->getArgOperand(2), 1);
Gabor Greifaee5dc12010-06-24 10:42:46 +0000796 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000797 }
798};
799
800//===---------------------------------------===//
801// 'memset' Optimizations
802
Chris Lattner3e8b6632009-09-02 06:11:42 +0000803struct MemSetOpt : public LibCallOptimization {
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000804 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +0000805 // These optimizations require TargetData.
806 if (!TD) return 0;
807
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000808 const FunctionType *FT = Callee->getFunctionType();
809 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000810 !FT->getParamType(0)->isPointerTy() ||
811 !FT->getParamType(1)->isIntegerTy() ||
Owen Anderson1d0be152009-08-13 21:58:54 +0000812 FT->getParamType(2) != TD->getIntPtrType(*Context))
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000813 return 0;
814
815 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000816 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
817 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
Gabor Greifaee5dc12010-06-24 10:42:46 +0000818 return CI->getArgOperand(0);
Eli Friedmand83ae7d2008-11-30 08:32:11 +0000819 }
820};
821
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000822//===----------------------------------------------------------------------===//
823// Math Library Optimizations
824//===----------------------------------------------------------------------===//
825
826//===---------------------------------------===//
827// 'pow*' Optimizations
828
Chris Lattner3e8b6632009-09-02 06:11:42 +0000829struct PowOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000830 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000831 const FunctionType *FT = Callee->getFunctionType();
832 // Just make sure this has 2 arguments of the same FP type, which match the
833 // result type.
834 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
835 FT->getParamType(0) != FT->getParamType(1) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000836 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000837 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000838
Gabor Greifaee5dc12010-06-24 10:42:46 +0000839 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000840 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
841 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
842 return Op1C;
843 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
Dan Gohman76926b62009-09-26 18:10:13 +0000844 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000845 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000846
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000847 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
848 if (Op2C == 0) return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000849
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000850 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000851 return ConstantFP::get(CI->getType(), 1.0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000852
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000853 if (Op2C->isExactlyValue(0.5)) {
Dan Gohman79cb8402009-09-25 23:10:17 +0000854 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
855 // This is faster than calling pow, and still handles negative zero
856 // and negative infinite correctly.
857 // TODO: In fast-math mode, this could be just sqrt(x).
858 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Dan Gohmana23643d2009-09-25 23:40:21 +0000859 Value *Inf = ConstantFP::getInfinity(CI->getType());
860 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Dan Gohman76926b62009-09-26 18:10:13 +0000861 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
862 Callee->getAttributes());
863 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
864 Callee->getAttributes());
Dan Gohman79cb8402009-09-25 23:10:17 +0000865 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
866 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
867 return Sel;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000868 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000869
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000870 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
871 return Op1;
872 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000873 return B.CreateFMul(Op1, Op1, "pow2");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000874 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000875 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000876 Op1, "powrecip");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000877 return 0;
878 }
879};
880
881//===---------------------------------------===//
Chris Lattnere818f772008-05-02 18:43:35 +0000882// 'exp2' Optimizations
883
Chris Lattner3e8b6632009-09-02 06:11:42 +0000884struct Exp2Opt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000885 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnere818f772008-05-02 18:43:35 +0000886 const FunctionType *FT = Callee->getFunctionType();
887 // Just make sure this has 1 argument of FP type, which matches the
888 // result type.
889 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000890 !FT->getParamType(0)->isFloatingPointTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000891 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000892
Gabor Greifaee5dc12010-06-24 10:42:46 +0000893 Value *Op = CI->getArgOperand(0);
Chris Lattnere818f772008-05-02 18:43:35 +0000894 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
895 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
896 Value *LdExpArg = 0;
897 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
898 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000899 LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty(), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000900 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
901 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000902 LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty(), "tmp");
Chris Lattnere818f772008-05-02 18:43:35 +0000903 }
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000904
Chris Lattnere818f772008-05-02 18:43:35 +0000905 if (LdExpArg) {
906 const char *Name;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000907 if (Op->getType()->isFloatTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000908 Name = "ldexpf";
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000909 else if (Op->getType()->isDoubleTy())
Chris Lattnere818f772008-05-02 18:43:35 +0000910 Name = "ldexp";
911 else
912 Name = "ldexpl";
913
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000914 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000915 if (!Op->getType()->isFloatTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000916 One = ConstantExpr::getFPExtend(One, Op->getType());
Chris Lattnere818f772008-05-02 18:43:35 +0000917
918 Module *M = Caller->getParent();
919 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Eric Christopher37c8b862009-10-07 21:14:25 +0000920 Op->getType(),
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000921 B.getInt32Ty(), NULL);
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000922 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
923 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
924 CI->setCallingConv(F->getCallingConv());
925
926 return CI;
Chris Lattnere818f772008-05-02 18:43:35 +0000927 }
928 return 0;
929 }
930};
Chris Lattnere818f772008-05-02 18:43:35 +0000931
932//===---------------------------------------===//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000933// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
934
Chris Lattner3e8b6632009-09-02 06:11:42 +0000935struct UnaryDoubleFPOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000936 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000937 const FunctionType *FT = Callee->getFunctionType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000938 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
939 !FT->getParamType(0)->isDoubleTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000940 return 0;
Anton Korobeynikov9547cdf2009-06-18 20:05:31 +0000941
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000942 // If this is something like 'floor((double)floatval)', convert to floorf.
Gabor Greifaee5dc12010-06-24 10:42:46 +0000943 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000944 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000945 return 0;
946
947 // floor((double)floatval) -> (double)floorf(floatval)
948 Value *V = Cast->getOperand(0);
Dan Gohman76926b62009-09-26 18:10:13 +0000949 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
950 Callee->getAttributes());
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000951 return B.CreateFPExt(V, B.getDoubleTy());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000952 }
953};
954
955//===----------------------------------------------------------------------===//
956// Integer Optimizations
957//===----------------------------------------------------------------------===//
958
959//===---------------------------------------===//
960// 'ffs*' Optimizations
961
Chris Lattner3e8b6632009-09-02 06:11:42 +0000962struct FFSOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000963 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000964 const FunctionType *FT = Callee->getFunctionType();
965 // Just make sure this has 2 arguments of the same FP type, which match the
966 // result type.
Eric Christopher37c8b862009-10-07 21:14:25 +0000967 if (FT->getNumParams() != 1 ||
Nick Lewycky10d2f4d2010-07-06 03:53:43 +0000968 !FT->getReturnType()->isIntegerTy(32) ||
Duncan Sands1df98592010-02-16 11:11:14 +0000969 !FT->getParamType(0)->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000970 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +0000971
Gabor Greifaee5dc12010-06-24 10:42:46 +0000972 Value *Op = CI->getArgOperand(0);
Eric Christopher37c8b862009-10-07 21:14:25 +0000973
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000974 // Constant fold.
975 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
976 if (CI->getValue() == 0) // ffs(0) -> 0.
Owen Andersona7235ea2009-07-31 20:28:14 +0000977 return Constant::getNullValue(CI->getType());
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000978 // ffs(c) -> cttz(c)+1
979 return B.getInt32(CI->getValue().countTrailingZeros() + 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000980 }
Eric Christopher37c8b862009-10-07 21:14:25 +0000981
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000982 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
983 const Type *ArgType = Op->getType();
984 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
985 Intrinsic::cttz, &ArgType, 1);
986 Value *V = B.CreateCall(F, Op, "cttz");
Owen Andersoneed707b2009-07-24 23:12:02 +0000987 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000988 V = B.CreateIntCast(V, B.getInt32Ty(), false, "tmp");
Eric Christopher37c8b862009-10-07 21:14:25 +0000989
Owen Andersona7235ea2009-07-31 20:28:14 +0000990 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +0000991 return B.CreateSelect(Cond, V, B.getInt32(0));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +0000992 }
993};
994
995//===---------------------------------------===//
996// 'isdigit' Optimizations
997
Chris Lattner3e8b6632009-09-02 06:11:42 +0000998struct IsDigitOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +0000999 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001000 const FunctionType *FT = Callee->getFunctionType();
1001 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +00001002 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001003 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001004 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001005
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001006 // isdigit(c) -> (c-'0') <u 10
Gabor Greifaee5dc12010-06-24 10:42:46 +00001007 Value *Op = CI->getArgOperand(0);
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001008 Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
1009 Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001010 return B.CreateZExt(Op, CI->getType());
1011 }
1012};
1013
1014//===---------------------------------------===//
1015// 'isascii' Optimizations
1016
Chris Lattner3e8b6632009-09-02 06:11:42 +00001017struct IsAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001018 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001019 const FunctionType *FT = Callee->getFunctionType();
1020 // We require integer(i32)
Duncan Sands1df98592010-02-16 11:11:14 +00001021 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001022 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001023 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001024
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001025 // isascii(c) -> c <u 128
Gabor Greifaee5dc12010-06-24 10:42:46 +00001026 Value *Op = CI->getArgOperand(0);
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001027 Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001028 return B.CreateZExt(Op, CI->getType());
1029 }
1030};
Eric Christopher37c8b862009-10-07 21:14:25 +00001031
Chris Lattner313f0e62008-06-09 08:26:51 +00001032//===---------------------------------------===//
1033// 'abs', 'labs', 'llabs' Optimizations
1034
Chris Lattner3e8b6632009-09-02 06:11:42 +00001035struct AbsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001036 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattner313f0e62008-06-09 08:26:51 +00001037 const FunctionType *FT = Callee->getFunctionType();
1038 // We require integer(integer) where the types agree.
Duncan Sands1df98592010-02-16 11:11:14 +00001039 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
Chris Lattner313f0e62008-06-09 08:26:51 +00001040 FT->getParamType(0) != FT->getReturnType())
1041 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001042
Chris Lattner313f0e62008-06-09 08:26:51 +00001043 // abs(x) -> x >s -1 ? x : -x
Gabor Greifaee5dc12010-06-24 10:42:46 +00001044 Value *Op = CI->getArgOperand(0);
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001045 Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()),
Chris Lattner313f0e62008-06-09 08:26:51 +00001046 "ispos");
1047 Value *Neg = B.CreateNeg(Op, "neg");
1048 return B.CreateSelect(Pos, Op, Neg);
1049 }
1050};
Eric Christopher37c8b862009-10-07 21:14:25 +00001051
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001052
1053//===---------------------------------------===//
1054// 'toascii' Optimizations
1055
Chris Lattner3e8b6632009-09-02 06:11:42 +00001056struct ToAsciiOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001057 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001058 const FunctionType *FT = Callee->getFunctionType();
1059 // We require i32(i32)
1060 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001061 !FT->getParamType(0)->isIntegerTy(32))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001062 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001063
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001064 // isascii(c) -> c & 0x7f
Gabor Greifaee5dc12010-06-24 10:42:46 +00001065 return B.CreateAnd(CI->getArgOperand(0),
Owen Andersoneed707b2009-07-24 23:12:02 +00001066 ConstantInt::get(CI->getType(),0x7F));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001067 }
1068};
1069
1070//===----------------------------------------------------------------------===//
1071// Formatting and IO Optimizations
1072//===----------------------------------------------------------------------===//
1073
1074//===---------------------------------------===//
1075// 'printf' Optimizations
1076
Chris Lattner3e8b6632009-09-02 06:11:42 +00001077struct PrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001078 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001079 // Require one fixed pointer argument and an integer/void result.
1080 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001081 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1082 !(FT->getReturnType()->isIntegerTy() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001083 FT->getReturnType()->isVoidTy()))
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001084 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001085
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001086 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001087 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001088 if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001089 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001090
1091 // Empty format string -> noop.
1092 if (FormatStr.empty()) // Tolerate printf's declared void.
Eric Christopher37c8b862009-10-07 21:14:25 +00001093 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001094 ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001095
Daniel Dunbard02be242011-02-12 18:19:57 +00001096 // Do not do any of the following transformations if the printf return value
1097 // is used, in general the printf return value is not compatible with either
1098 // putchar() or puts().
1099 if (!CI->use_empty())
1100 return 0;
1101
1102 // printf("x") -> putchar('x'), even for '%'.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001103 if (FormatStr.size() == 1) {
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001104 Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD);
Chris Lattner74965f22009-11-09 04:57:04 +00001105 if (CI->use_empty()) return CI;
1106 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001107 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001108
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001109 // printf("foo\n") --> puts("foo")
1110 if (FormatStr[FormatStr.size()-1] == '\n' &&
1111 FormatStr.find('%') == std::string::npos) { // no format characters.
1112 // Create a string literal with no \n on it. We expect the constant merge
1113 // pass to be run after this pass, to merge duplicate strings.
1114 FormatStr.erase(FormatStr.end()-1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001115 Constant *C = ConstantArray::get(*Context, FormatStr, true);
Owen Andersone9b11b42009-07-08 19:03:57 +00001116 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1117 GlobalVariable::InternalLinkage, C, "str");
Eric Christopherb6174e32010-03-05 22:25:30 +00001118 EmitPutS(C, B, TD);
Eric Christopher37c8b862009-10-07 21:14:25 +00001119 return CI->use_empty() ? (Value*)CI :
Owen Andersoneed707b2009-07-24 23:12:02 +00001120 ConstantInt::get(CI->getType(), FormatStr.size()+1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001121 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001122
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001123 // Optimize specific format strings.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001124 // printf("%c", chr) --> putchar(chr)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001125 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
Gabor Greifaee5dc12010-06-24 10:42:46 +00001126 CI->getArgOperand(1)->getType()->isIntegerTy()) {
1127 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD);
Eric Christopher80bf1d52009-11-21 01:01:30 +00001128
Chris Lattner74965f22009-11-09 04:57:04 +00001129 if (CI->use_empty()) return CI;
1130 return B.CreateIntCast(Res, CI->getType(), true);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001131 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001132
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001133 // printf("%s\n", str) --> puts(str)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001134 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
Daniel Dunbard02be242011-02-12 18:19:57 +00001135 CI->getArgOperand(1)->getType()->isPointerTy()) {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001136 EmitPutS(CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001137 return CI;
1138 }
1139 return 0;
1140 }
1141};
1142
1143//===---------------------------------------===//
1144// 'sprintf' Optimizations
1145
Chris Lattner3e8b6632009-09-02 06:11:42 +00001146struct SPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001147 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001148 // Require two fixed pointer arguments and an integer result.
1149 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001150 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1151 !FT->getParamType(1)->isPointerTy() ||
1152 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001153 return 0;
1154
1155 // Check for a fixed format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001156 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001157 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001158 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001159
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001160 // If we just have a format string (nothing else crazy) transform it.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001161 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001162 // Make sure there's no % in the constant array. We could try to handle
1163 // %% -> % in the future if we cared.
1164 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1165 if (FormatStr[i] == '%')
1166 return 0; // we found a format specifier, bail out.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001167
1168 // These optimizations require TargetData.
1169 if (!TD) return 0;
1170
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001171 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001172 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
1173 ConstantInt::get(TD->getIntPtrType(*Context), // Copy the
1174 FormatStr.size() + 1), 1); // nul byte.
Owen Andersoneed707b2009-07-24 23:12:02 +00001175 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001176 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001177
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001178 // The remaining optimizations require the format string to be "%s" or "%c"
1179 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001180 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1181 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001182 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001183
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001184 // Decode the second character of the format string.
1185 if (FormatStr[1] == 'c') {
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001186 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Gabor Greifaee5dc12010-06-24 10:42:46 +00001187 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001188 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
Gabor Greifaee5dc12010-06-24 10:42:46 +00001189 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001190 B.CreateStore(V, Ptr);
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001191 Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");
1192 B.CreateStore(B.getInt8(0), Ptr);
Eric Christopher37c8b862009-10-07 21:14:25 +00001193
Owen Andersoneed707b2009-07-24 23:12:02 +00001194 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001195 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001196
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001197 if (FormatStr[1] == 's') {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001198 // These optimizations require TargetData.
1199 if (!TD) return 0;
1200
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001201 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001202 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001203
Gabor Greifaee5dc12010-06-24 10:42:46 +00001204 Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD);
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001205 Value *IncLen = B.CreateAdd(Len,
Owen Andersoneed707b2009-07-24 23:12:02 +00001206 ConstantInt::get(Len->getType(), 1),
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001207 "leninc");
Benjamin Kramera1bf4ca2010-12-27 00:16:46 +00001208 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
Eric Christopher37c8b862009-10-07 21:14:25 +00001209
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001210 // The sprintf result is the unincremented number of bytes in the string.
1211 return B.CreateIntCast(Len, CI->getType(), false);
1212 }
1213 return 0;
1214 }
1215};
1216
1217//===---------------------------------------===//
1218// 'fwrite' Optimizations
1219
Chris Lattner3e8b6632009-09-02 06:11:42 +00001220struct FWriteOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001221 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001222 // Require a pointer, an integer, an integer, a pointer, returning integer.
1223 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001224 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1225 !FT->getParamType(1)->isIntegerTy() ||
1226 !FT->getParamType(2)->isIntegerTy() ||
1227 !FT->getParamType(3)->isPointerTy() ||
1228 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001229 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001230
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001231 // Get the element size and count.
Gabor Greifaee5dc12010-06-24 10:42:46 +00001232 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1233 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001234 if (!SizeC || !CountC) return 0;
1235 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
Eric Christopher37c8b862009-10-07 21:14:25 +00001236
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001237 // If this is writing zero records, remove the call (it's a noop).
1238 if (Bytes == 0)
Owen Andersoneed707b2009-07-24 23:12:02 +00001239 return ConstantInt::get(CI->getType(), 0);
Eric Christopher37c8b862009-10-07 21:14:25 +00001240
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001241 // If this is writing one byte, turn it into fputc.
1242 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001243 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1244 EmitFPutC(Char, CI->getArgOperand(3), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001245 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001246 }
1247
1248 return 0;
1249 }
1250};
1251
1252//===---------------------------------------===//
1253// 'fputs' Optimizations
1254
Chris Lattner3e8b6632009-09-02 06:11:42 +00001255struct FPutsOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001256 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Dan Gohmanf14d9192009-08-18 00:48:13 +00001257 // These optimizations require TargetData.
1258 if (!TD) return 0;
1259
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001260 // Require two pointers. Also, we can't optimize if return value is used.
1261 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001262 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1263 !FT->getParamType(1)->isPointerTy() ||
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001264 !CI->use_empty())
1265 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001266
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001267 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
Gabor Greifaee5dc12010-06-24 10:42:46 +00001268 uint64_t Len = GetStringLength(CI->getArgOperand(0));
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001269 if (!Len) return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001270 EmitFWrite(CI->getArgOperand(0),
Owen Anderson1d0be152009-08-13 21:58:54 +00001271 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001272 CI->getArgOperand(1), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001273 return CI; // Known to have no uses (see above).
1274 }
1275};
1276
1277//===---------------------------------------===//
1278// 'fprintf' Optimizations
1279
Chris Lattner3e8b6632009-09-02 06:11:42 +00001280struct FPrintFOpt : public LibCallOptimization {
Eric Christopher7a61d702008-08-08 19:39:37 +00001281 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001282 // Require two fixed paramters as pointers and integer result.
1283 const FunctionType *FT = Callee->getFunctionType();
Duncan Sands1df98592010-02-16 11:11:14 +00001284 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1285 !FT->getParamType(1)->isPointerTy() ||
1286 !FT->getReturnType()->isIntegerTy())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001287 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001288
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001289 // All the optimizations depend on the format string.
Bill Wendling0582ae92009-03-13 04:39:26 +00001290 std::string FormatStr;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001291 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
Bill Wendling0582ae92009-03-13 04:39:26 +00001292 return 0;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001293
1294 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001295 if (CI->getNumArgOperands() == 2) {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001296 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1297 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
Chris Lattner56b4f2b2008-05-01 06:39:12 +00001298 return 0; // We found a format specifier.
Dan Gohmanf14d9192009-08-18 00:48:13 +00001299
1300 // These optimizations require TargetData.
1301 if (!TD) return 0;
1302
Gabor Greifaee5dc12010-06-24 10:42:46 +00001303 EmitFWrite(CI->getArgOperand(1),
Mikhail Glushenkoved5cb592010-01-04 07:55:25 +00001304 ConstantInt::get(TD->getIntPtrType(*Context),
1305 FormatStr.size()),
Gabor Greifaee5dc12010-06-24 10:42:46 +00001306 CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001307 return ConstantInt::get(CI->getType(), FormatStr.size());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001308 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001309
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001310 // The remaining optimizations require the format string to be "%s" or "%c"
1311 // and have an extra operand.
Gabor Greif8e1ebff2010-06-30 12:42:43 +00001312 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1313 CI->getNumArgOperands() < 3)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001314 return 0;
Eric Christopher37c8b862009-10-07 21:14:25 +00001315
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001316 // Decode the second character of the format string.
1317 if (FormatStr[1] == 'c') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001318 // fprintf(F, "%c", chr) --> fputc(chr, F)
1319 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1320 EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Owen Andersoneed707b2009-07-24 23:12:02 +00001321 return ConstantInt::get(CI->getType(), 1);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001322 }
Eric Christopher37c8b862009-10-07 21:14:25 +00001323
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001324 if (FormatStr[1] == 's') {
Gabor Greifaee5dc12010-06-24 10:42:46 +00001325 // fprintf(F, "%s", str) --> fputs(str, F)
1326 if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001327 return 0;
Gabor Greifaee5dc12010-06-24 10:42:46 +00001328 EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001329 return CI;
1330 }
1331 return 0;
1332 }
1333};
1334
Anders Carlsson303023d2010-11-30 06:19:18 +00001335//===---------------------------------------===//
1336// 'puts' Optimizations
1337
1338struct PutsOpt : public LibCallOptimization {
1339 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1340 // Require one fixed pointer argument and an integer/void result.
1341 const FunctionType *FT = Callee->getFunctionType();
1342 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1343 !(FT->getReturnType()->isIntegerTy() ||
1344 FT->getReturnType()->isVoidTy()))
1345 return 0;
1346
1347 // Check for a constant string.
1348 std::string Str;
1349 if (!GetConstantStringInfo(CI->getArgOperand(0), Str))
1350 return 0;
1351
Daniel Dunbard02be242011-02-12 18:19:57 +00001352 if (Str.empty() && CI->use_empty()) {
Anders Carlsson303023d2010-11-30 06:19:18 +00001353 // puts("") -> putchar('\n')
1354 Value *Res = EmitPutChar(B.getInt32('\n'), B, TD);
1355 if (CI->use_empty()) return CI;
1356 return B.CreateIntCast(Res, CI->getType(), true);
1357 }
1358
1359 return 0;
1360 }
1361};
1362
Bill Wendlingac178222008-05-05 21:37:59 +00001363} // end anonymous namespace.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001364
1365//===----------------------------------------------------------------------===//
1366// SimplifyLibCalls Pass Implementation
1367//===----------------------------------------------------------------------===//
1368
1369namespace {
1370 /// This pass optimizes well known library functions from libc and libm.
1371 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +00001372 class SimplifyLibCalls : public FunctionPass {
Chris Lattnerafbf4832011-02-24 07:16:14 +00001373 TargetLibraryInfo *TLI;
1374
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001375 StringMap<LibCallOptimization*> Optimizations;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001376 // String and Memory LibCall Optimizations
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001377 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrRChrOpt StrRChr;
1378 StrCmpOpt StrCmp; StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
Benjamin Kramer05f585e2010-09-29 23:52:12 +00001379 StrNCpyOpt StrNCpy; StrLenOpt StrLen; StrPBrkOpt StrPBrk;
Benjamin Kramer9510a252010-09-30 00:58:35 +00001380 StrToOpt StrTo; StrSpnOpt StrSpn; StrCSpnOpt StrCSpn; StrStrOpt StrStr;
Chris Lattner24604112009-12-16 09:32:05 +00001381 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001382 // Math Library Optimizations
Chris Lattnere818f772008-05-02 18:43:35 +00001383 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001384 // Integer Optimizations
Chris Lattner313f0e62008-06-09 08:26:51 +00001385 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1386 ToAsciiOpt ToAscii;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001387 // Formatting and IO Optimizations
1388 SPrintFOpt SPrintF; PrintFOpt PrintF;
1389 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
Anders Carlsson303023d2010-11-30 06:19:18 +00001390 PutsOpt Puts;
Chris Lattnerafbf4832011-02-24 07:16:14 +00001391
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001392 bool Modified; // This is only used by doInitialization.
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001393 public:
1394 static char ID; // Pass identification
Owen Anderson081c34b2010-10-19 17:21:58 +00001395 SimplifyLibCalls() : FunctionPass(ID), StrCpy(false), StrCpyChk(true) {
1396 initializeSimplifyLibCallsPass(*PassRegistry::getPassRegistry());
1397 }
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001398 void InitOptimizations();
1399 bool runOnFunction(Function &F);
1400
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001401 void setDoesNotAccessMemory(Function &F);
1402 void setOnlyReadsMemory(Function &F);
1403 void setDoesNotThrow(Function &F);
1404 void setDoesNotCapture(Function &F, unsigned n);
1405 void setDoesNotAlias(Function &F, unsigned n);
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001406 bool doInitialization(Module &M);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001407
Chris Lattnere265ad82011-02-24 07:12:12 +00001408 void inferPrototypeAttributes(Function &F);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001409 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerafbf4832011-02-24 07:16:14 +00001410 AU.addRequired<TargetLibraryInfo>();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001411 }
1412 };
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001413} // end anonymous namespace.
1414
Chris Lattnerafbf4832011-02-24 07:16:14 +00001415char SimplifyLibCalls::ID = 0;
1416
1417INITIALIZE_PASS_BEGIN(SimplifyLibCalls, "simplify-libcalls",
1418 "Simplify well-known library calls", false, false)
1419INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
1420INITIALIZE_PASS_END(SimplifyLibCalls, "simplify-libcalls",
1421 "Simplify well-known library calls", false, false)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001422
1423// Public interface to the Simplify LibCalls pass.
1424FunctionPass *llvm::createSimplifyLibCallsPass() {
Eric Christopher37c8b862009-10-07 21:14:25 +00001425 return new SimplifyLibCalls();
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001426}
1427
1428/// Optimizations - Populate the Optimizations map with all the optimizations
1429/// we know.
1430void SimplifyLibCalls::InitOptimizations() {
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001431 // String and Memory LibCall Optimizations
1432 Optimizations["strcat"] = &StrCat;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001433 Optimizations["strncat"] = &StrNCat;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001434 Optimizations["strchr"] = &StrChr;
Benjamin Kramer06f25cf2010-09-29 21:50:51 +00001435 Optimizations["strrchr"] = &StrRChr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001436 Optimizations["strcmp"] = &StrCmp;
1437 Optimizations["strncmp"] = &StrNCmp;
1438 Optimizations["strcpy"] = &StrCpy;
Chris Lattnerf5b6bc72009-04-12 05:06:39 +00001439 Optimizations["strncpy"] = &StrNCpy;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001440 Optimizations["strlen"] = &StrLen;
Benjamin Kramer05f585e2010-09-29 23:52:12 +00001441 Optimizations["strpbrk"] = &StrPBrk;
Nick Lewycky4c498412009-02-13 15:31:46 +00001442 Optimizations["strtol"] = &StrTo;
1443 Optimizations["strtod"] = &StrTo;
1444 Optimizations["strtof"] = &StrTo;
1445 Optimizations["strtoul"] = &StrTo;
1446 Optimizations["strtoll"] = &StrTo;
1447 Optimizations["strtold"] = &StrTo;
1448 Optimizations["strtoull"] = &StrTo;
Benjamin Kramer9510a252010-09-30 00:58:35 +00001449 Optimizations["strspn"] = &StrSpn;
1450 Optimizations["strcspn"] = &StrCSpn;
Chris Lattner24604112009-12-16 09:32:05 +00001451 Optimizations["strstr"] = &StrStr;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001452 Optimizations["memcmp"] = &MemCmp;
Chris Lattnerafbf4832011-02-24 07:16:14 +00001453 if (TLI->has(LibFunc::memcpy)) Optimizations["memcpy"] = &MemCpy;
Eli Friedmand83ae7d2008-11-30 08:32:11 +00001454 Optimizations["memmove"] = &MemMove;
Chris Lattnerafbf4832011-02-24 07:16:14 +00001455 if (TLI->has(LibFunc::memset)) Optimizations["memset"] = &MemSet;
Eric Christopher37c8b862009-10-07 21:14:25 +00001456
Evan Cheng0289b412010-03-23 15:48:04 +00001457 // _chk variants of String and Memory LibCall Optimizations.
Evan Cheng0289b412010-03-23 15:48:04 +00001458 Optimizations["__strcpy_chk"] = &StrCpyChk;
1459
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001460 // Math Library Optimizations
1461 Optimizations["powf"] = &Pow;
1462 Optimizations["pow"] = &Pow;
1463 Optimizations["powl"] = &Pow;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001464 Optimizations["llvm.pow.f32"] = &Pow;
1465 Optimizations["llvm.pow.f64"] = &Pow;
1466 Optimizations["llvm.pow.f80"] = &Pow;
1467 Optimizations["llvm.pow.f128"] = &Pow;
1468 Optimizations["llvm.pow.ppcf128"] = &Pow;
Chris Lattnere818f772008-05-02 18:43:35 +00001469 Optimizations["exp2l"] = &Exp2;
1470 Optimizations["exp2"] = &Exp2;
1471 Optimizations["exp2f"] = &Exp2;
Dale Johannesen53bfbbc2008-09-04 18:30:46 +00001472 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1473 Optimizations["llvm.exp2.f128"] = &Exp2;
1474 Optimizations["llvm.exp2.f80"] = &Exp2;
1475 Optimizations["llvm.exp2.f64"] = &Exp2;
1476 Optimizations["llvm.exp2.f32"] = &Exp2;
Eric Christopher37c8b862009-10-07 21:14:25 +00001477
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001478#ifdef HAVE_FLOORF
1479 Optimizations["floor"] = &UnaryDoubleFP;
1480#endif
1481#ifdef HAVE_CEILF
1482 Optimizations["ceil"] = &UnaryDoubleFP;
1483#endif
1484#ifdef HAVE_ROUNDF
1485 Optimizations["round"] = &UnaryDoubleFP;
1486#endif
1487#ifdef HAVE_RINTF
1488 Optimizations["rint"] = &UnaryDoubleFP;
1489#endif
1490#ifdef HAVE_NEARBYINTF
1491 Optimizations["nearbyint"] = &UnaryDoubleFP;
1492#endif
Eric Christopher37c8b862009-10-07 21:14:25 +00001493
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001494 // Integer Optimizations
1495 Optimizations["ffs"] = &FFS;
1496 Optimizations["ffsl"] = &FFS;
1497 Optimizations["ffsll"] = &FFS;
Chris Lattner313f0e62008-06-09 08:26:51 +00001498 Optimizations["abs"] = &Abs;
1499 Optimizations["labs"] = &Abs;
1500 Optimizations["llabs"] = &Abs;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001501 Optimizations["isdigit"] = &IsDigit;
1502 Optimizations["isascii"] = &IsAscii;
1503 Optimizations["toascii"] = &ToAscii;
Eric Christopher37c8b862009-10-07 21:14:25 +00001504
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001505 // Formatting and IO Optimizations
1506 Optimizations["sprintf"] = &SPrintF;
1507 Optimizations["printf"] = &PrintF;
1508 Optimizations["fwrite"] = &FWrite;
1509 Optimizations["fputs"] = &FPuts;
1510 Optimizations["fprintf"] = &FPrintF;
Anders Carlsson303023d2010-11-30 06:19:18 +00001511 Optimizations["puts"] = &Puts;
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001512}
1513
1514
1515/// runOnFunction - Top level algorithm.
1516///
1517bool SimplifyLibCalls::runOnFunction(Function &F) {
Chris Lattnerafbf4832011-02-24 07:16:14 +00001518 TLI = &getAnalysis<TargetLibraryInfo>();
1519
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001520 if (Optimizations.empty())
1521 InitOptimizations();
Eric Christopher37c8b862009-10-07 21:14:25 +00001522
Dan Gohmanf14d9192009-08-18 00:48:13 +00001523 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Eric Christopher37c8b862009-10-07 21:14:25 +00001524
Owen Andersone922c022009-07-22 00:24:57 +00001525 IRBuilder<> Builder(F.getContext());
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001526
1527 bool Changed = false;
1528 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1529 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1530 // Ignore non-calls.
1531 CallInst *CI = dyn_cast<CallInst>(I++);
1532 if (!CI) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001533
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001534 // Ignore indirect calls and calls to non-external functions.
1535 Function *Callee = CI->getCalledFunction();
1536 if (Callee == 0 || !Callee->isDeclaration() ||
1537 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1538 continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001539
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001540 // Ignore unknown calls.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001541 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1542 if (!LCO) continue;
Eric Christopher37c8b862009-10-07 21:14:25 +00001543
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001544 // Set the builder to the instruction after the call.
1545 Builder.SetInsertPoint(BB, I);
Eric Christopher37c8b862009-10-07 21:14:25 +00001546
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001547 // Try to optimize this call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001548 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001549 if (Result == 0) continue;
1550
David Greene6a6b90e2010-01-05 01:27:21 +00001551 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1552 dbgs() << " into: " << *Result << "\n");
Eric Christopher37c8b862009-10-07 21:14:25 +00001553
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001554 // Something changed!
1555 Changed = true;
1556 ++NumSimplified;
Eric Christopher37c8b862009-10-07 21:14:25 +00001557
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001558 // Inspect the instruction after the call (which was potentially just
1559 // added) next.
1560 I = CI; ++I;
Eric Christopher37c8b862009-10-07 21:14:25 +00001561
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00001562 if (CI != Result && !CI->use_empty()) {
1563 CI->replaceAllUsesWith(Result);
1564 if (!Result->hasName())
1565 Result->takeName(CI);
1566 }
1567 CI->eraseFromParent();
1568 }
1569 }
1570 return Changed;
1571}
1572
Nick Lewycky6cd0c042009-01-05 00:07:50 +00001573// Utility methods for doInitialization.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00001574
1575void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1576 if (!F.doesNotAccessMemory()) {
1577 F.setDoesNotAccessMemory();
1578 ++NumAnnotated;
1579 Modified = true;
1580 }
1581}
1582void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1583 if (!F.onlyReadsMemory()) {
1584 F.setOnlyReadsMemory();
1585 ++NumAnnotated;
1586 Modified = true;
1587 }
1588}
1589void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1590 if (!F.doesNotThrow()) {
1591 F.setDoesNotThrow();
1592 ++NumAnnotated;
1593 Modified = true;
1594 }
1595}
1596void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1597 if (!F.doesNotCapture(n)) {
1598 F.setDoesNotCapture(n);
1599 ++NumAnnotated;
1600 Modified = true;
1601 }
1602}
1603void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1604 if (!F.doesNotAlias(n)) {
1605 F.setDoesNotAlias(n);
1606 ++NumAnnotated;
1607 Modified = true;
1608 }
1609}
1610
Chris Lattnere265ad82011-02-24 07:12:12 +00001611
1612void SimplifyLibCalls::inferPrototypeAttributes(Function &F) {
1613 const FunctionType *FTy = F.getFunctionType();
1614
1615 StringRef Name = F.getName();
1616 switch (Name[0]) {
1617 case 's':
1618 if (Name == "strlen") {
1619 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1620 return;
1621 setOnlyReadsMemory(F);
1622 setDoesNotThrow(F);
1623 setDoesNotCapture(F, 1);
1624 } else if (Name == "strchr" ||
1625 Name == "strrchr") {
1626 if (FTy->getNumParams() != 2 ||
1627 !FTy->getParamType(0)->isPointerTy() ||
1628 !FTy->getParamType(1)->isIntegerTy())
1629 return;
1630 setOnlyReadsMemory(F);
1631 setDoesNotThrow(F);
1632 } else if (Name == "strcpy" ||
1633 Name == "stpcpy" ||
1634 Name == "strcat" ||
1635 Name == "strtol" ||
1636 Name == "strtod" ||
1637 Name == "strtof" ||
1638 Name == "strtoul" ||
1639 Name == "strtoll" ||
1640 Name == "strtold" ||
1641 Name == "strncat" ||
1642 Name == "strncpy" ||
1643 Name == "strtoull") {
1644 if (FTy->getNumParams() < 2 ||
1645 !FTy->getParamType(1)->isPointerTy())
1646 return;
1647 setDoesNotThrow(F);
1648 setDoesNotCapture(F, 2);
1649 } else if (Name == "strxfrm") {
1650 if (FTy->getNumParams() != 3 ||
1651 !FTy->getParamType(0)->isPointerTy() ||
1652 !FTy->getParamType(1)->isPointerTy())
1653 return;
1654 setDoesNotThrow(F);
1655 setDoesNotCapture(F, 1);
1656 setDoesNotCapture(F, 2);
1657 } else if (Name == "strcmp" ||
1658 Name == "strspn" ||
1659 Name == "strncmp" ||
1660 Name == "strcspn" ||
1661 Name == "strcoll" ||
1662 Name == "strcasecmp" ||
1663 Name == "strncasecmp") {
1664 if (FTy->getNumParams() < 2 ||
1665 !FTy->getParamType(0)->isPointerTy() ||
1666 !FTy->getParamType(1)->isPointerTy())
1667 return;
1668 setOnlyReadsMemory(F);
1669 setDoesNotThrow(F);
1670 setDoesNotCapture(F, 1);
1671 setDoesNotCapture(F, 2);
1672 } else if (Name == "strstr" ||
1673 Name == "strpbrk") {
1674 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1675 return;
1676 setOnlyReadsMemory(F);
1677 setDoesNotThrow(F);
1678 setDoesNotCapture(F, 2);
1679 } else if (Name == "strtok" ||
1680 Name == "strtok_r") {
1681 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1682 return;
1683 setDoesNotThrow(F);
1684 setDoesNotCapture(F, 2);
1685 } else if (Name == "scanf" ||
1686 Name == "setbuf" ||
1687 Name == "setvbuf") {
1688 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1689 return;
1690 setDoesNotThrow(F);
1691 setDoesNotCapture(F, 1);
1692 } else if (Name == "strdup" ||
1693 Name == "strndup") {
1694 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1695 !FTy->getParamType(0)->isPointerTy())
1696 return;
1697 setDoesNotThrow(F);
1698 setDoesNotAlias(F, 0);
1699 setDoesNotCapture(F, 1);
1700 } else if (Name == "stat" ||
1701 Name == "sscanf" ||
1702 Name == "sprintf" ||
1703 Name == "statvfs") {
1704 if (FTy->getNumParams() < 2 ||
1705 !FTy->getParamType(0)->isPointerTy() ||
1706 !FTy->getParamType(1)->isPointerTy())
1707 return;
1708 setDoesNotThrow(F);
1709 setDoesNotCapture(F, 1);
1710 setDoesNotCapture(F, 2);
1711 } else if (Name == "snprintf") {
1712 if (FTy->getNumParams() != 3 ||
1713 !FTy->getParamType(0)->isPointerTy() ||
1714 !FTy->getParamType(2)->isPointerTy())
1715 return;
1716 setDoesNotThrow(F);
1717 setDoesNotCapture(F, 1);
1718 setDoesNotCapture(F, 3);
1719 } else if (Name == "setitimer") {
1720 if (FTy->getNumParams() != 3 ||
1721 !FTy->getParamType(1)->isPointerTy() ||
1722 !FTy->getParamType(2)->isPointerTy())
1723 return;
1724 setDoesNotThrow(F);
1725 setDoesNotCapture(F, 2);
1726 setDoesNotCapture(F, 3);
1727 } else if (Name == "system") {
1728 if (FTy->getNumParams() != 1 ||
1729 !FTy->getParamType(0)->isPointerTy())
1730 return;
1731 // May throw; "system" is a valid pthread cancellation point.
1732 setDoesNotCapture(F, 1);
1733 }
1734 break;
1735 case 'm':
1736 if (Name == "malloc") {
1737 if (FTy->getNumParams() != 1 ||
1738 !FTy->getReturnType()->isPointerTy())
1739 return;
1740 setDoesNotThrow(F);
1741 setDoesNotAlias(F, 0);
1742 } else if (Name == "memcmp") {
1743 if (FTy->getNumParams() != 3 ||
1744 !FTy->getParamType(0)->isPointerTy() ||
1745 !FTy->getParamType(1)->isPointerTy())
1746 return;
1747 setOnlyReadsMemory(F);
1748 setDoesNotThrow(F);
1749 setDoesNotCapture(F, 1);
1750 setDoesNotCapture(F, 2);
1751 } else if (Name == "memchr" ||
1752 Name == "memrchr") {
1753 if (FTy->getNumParams() != 3)
1754 return;
1755 setOnlyReadsMemory(F);
1756 setDoesNotThrow(F);
1757 } else if (Name == "modf" ||
1758 Name == "modff" ||
1759 Name == "modfl" ||
1760 Name == "memcpy" ||
1761 Name == "memccpy" ||
1762 Name == "memmove") {
1763 if (FTy->getNumParams() < 2 ||
1764 !FTy->getParamType(1)->isPointerTy())
1765 return;
1766 setDoesNotThrow(F);
1767 setDoesNotCapture(F, 2);
1768 } else if (Name == "memalign") {
1769 if (!FTy->getReturnType()->isPointerTy())
1770 return;
1771 setDoesNotAlias(F, 0);
1772 } else if (Name == "mkdir" ||
1773 Name == "mktime") {
1774 if (FTy->getNumParams() == 0 ||
1775 !FTy->getParamType(0)->isPointerTy())
1776 return;
1777 setDoesNotThrow(F);
1778 setDoesNotCapture(F, 1);
1779 }
1780 break;
1781 case 'r':
1782 if (Name == "realloc") {
1783 if (FTy->getNumParams() != 2 ||
1784 !FTy->getParamType(0)->isPointerTy() ||
1785 !FTy->getReturnType()->isPointerTy())
1786 return;
1787 setDoesNotThrow(F);
1788 setDoesNotAlias(F, 0);
1789 setDoesNotCapture(F, 1);
1790 } else if (Name == "read") {
1791 if (FTy->getNumParams() != 3 ||
1792 !FTy->getParamType(1)->isPointerTy())
1793 return;
1794 // May throw; "read" is a valid pthread cancellation point.
1795 setDoesNotCapture(F, 2);
1796 } else if (Name == "rmdir" ||
1797 Name == "rewind" ||
1798 Name == "remove" ||
1799 Name == "realpath") {
1800 if (FTy->getNumParams() < 1 ||
1801 !FTy->getParamType(0)->isPointerTy())
1802 return;
1803 setDoesNotThrow(F);
1804 setDoesNotCapture(F, 1);
1805 } else if (Name == "rename" ||
1806 Name == "readlink") {
1807 if (FTy->getNumParams() < 2 ||
1808 !FTy->getParamType(0)->isPointerTy() ||
1809 !FTy->getParamType(1)->isPointerTy())
1810 return;
1811 setDoesNotThrow(F);
1812 setDoesNotCapture(F, 1);
1813 setDoesNotCapture(F, 2);
1814 }
1815 break;
1816 case 'w':
1817 if (Name == "write") {
1818 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1819 return;
1820 // May throw; "write" is a valid pthread cancellation point.
1821 setDoesNotCapture(F, 2);
1822 }
1823 break;
1824 case 'b':
1825 if (Name == "bcopy") {
1826 if (FTy->getNumParams() != 3 ||
1827 !FTy->getParamType(0)->isPointerTy() ||
1828 !FTy->getParamType(1)->isPointerTy())
1829 return;
1830 setDoesNotThrow(F);
1831 setDoesNotCapture(F, 1);
1832 setDoesNotCapture(F, 2);
1833 } else if (Name == "bcmp") {
1834 if (FTy->getNumParams() != 3 ||
1835 !FTy->getParamType(0)->isPointerTy() ||
1836 !FTy->getParamType(1)->isPointerTy())
1837 return;
1838 setDoesNotThrow(F);
1839 setOnlyReadsMemory(F);
1840 setDoesNotCapture(F, 1);
1841 setDoesNotCapture(F, 2);
1842 } else if (Name == "bzero") {
1843 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1844 return;
1845 setDoesNotThrow(F);
1846 setDoesNotCapture(F, 1);
1847 }
1848 break;
1849 case 'c':
1850 if (Name == "calloc") {
1851 if (FTy->getNumParams() != 2 ||
1852 !FTy->getReturnType()->isPointerTy())
1853 return;
1854 setDoesNotThrow(F);
1855 setDoesNotAlias(F, 0);
1856 } else if (Name == "chmod" ||
1857 Name == "chown" ||
1858 Name == "ctermid" ||
1859 Name == "clearerr" ||
1860 Name == "closedir") {
1861 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1862 return;
1863 setDoesNotThrow(F);
1864 setDoesNotCapture(F, 1);
1865 }
1866 break;
1867 case 'a':
1868 if (Name == "atoi" ||
1869 Name == "atol" ||
1870 Name == "atof" ||
1871 Name == "atoll") {
1872 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1873 return;
1874 setDoesNotThrow(F);
1875 setOnlyReadsMemory(F);
1876 setDoesNotCapture(F, 1);
1877 } else if (Name == "access") {
1878 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1879 return;
1880 setDoesNotThrow(F);
1881 setDoesNotCapture(F, 1);
1882 }
1883 break;
1884 case 'f':
1885 if (Name == "fopen") {
1886 if (FTy->getNumParams() != 2 ||
1887 !FTy->getReturnType()->isPointerTy() ||
1888 !FTy->getParamType(0)->isPointerTy() ||
1889 !FTy->getParamType(1)->isPointerTy())
1890 return;
1891 setDoesNotThrow(F);
1892 setDoesNotAlias(F, 0);
1893 setDoesNotCapture(F, 1);
1894 setDoesNotCapture(F, 2);
1895 } else if (Name == "fdopen") {
1896 if (FTy->getNumParams() != 2 ||
1897 !FTy->getReturnType()->isPointerTy() ||
1898 !FTy->getParamType(1)->isPointerTy())
1899 return;
1900 setDoesNotThrow(F);
1901 setDoesNotAlias(F, 0);
1902 setDoesNotCapture(F, 2);
1903 } else if (Name == "feof" ||
1904 Name == "free" ||
1905 Name == "fseek" ||
1906 Name == "ftell" ||
1907 Name == "fgetc" ||
1908 Name == "fseeko" ||
1909 Name == "ftello" ||
1910 Name == "fileno" ||
1911 Name == "fflush" ||
1912 Name == "fclose" ||
1913 Name == "fsetpos" ||
1914 Name == "flockfile" ||
1915 Name == "funlockfile" ||
1916 Name == "ftrylockfile") {
1917 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1918 return;
1919 setDoesNotThrow(F);
1920 setDoesNotCapture(F, 1);
1921 } else if (Name == "ferror") {
1922 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1923 return;
1924 setDoesNotThrow(F);
1925 setDoesNotCapture(F, 1);
1926 setOnlyReadsMemory(F);
1927 } else if (Name == "fputc" ||
1928 Name == "fstat" ||
1929 Name == "frexp" ||
1930 Name == "frexpf" ||
1931 Name == "frexpl" ||
1932 Name == "fstatvfs") {
1933 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1934 return;
1935 setDoesNotThrow(F);
1936 setDoesNotCapture(F, 2);
1937 } else if (Name == "fgets") {
1938 if (FTy->getNumParams() != 3 ||
1939 !FTy->getParamType(0)->isPointerTy() ||
1940 !FTy->getParamType(2)->isPointerTy())
1941 return;
1942 setDoesNotThrow(F);
1943 setDoesNotCapture(F, 3);
1944 } else if (Name == "fread" ||
1945 Name == "fwrite") {
1946 if (FTy->getNumParams() != 4 ||
1947 !FTy->getParamType(0)->isPointerTy() ||
1948 !FTy->getParamType(3)->isPointerTy())
1949 return;
1950 setDoesNotThrow(F);
1951 setDoesNotCapture(F, 1);
1952 setDoesNotCapture(F, 4);
1953 } else if (Name == "fputs" ||
1954 Name == "fscanf" ||
1955 Name == "fprintf" ||
1956 Name == "fgetpos") {
1957 if (FTy->getNumParams() < 2 ||
1958 !FTy->getParamType(0)->isPointerTy() ||
1959 !FTy->getParamType(1)->isPointerTy())
1960 return;
1961 setDoesNotThrow(F);
1962 setDoesNotCapture(F, 1);
1963 setDoesNotCapture(F, 2);
1964 }
1965 break;
1966 case 'g':
1967 if (Name == "getc" ||
1968 Name == "getlogin_r" ||
1969 Name == "getc_unlocked") {
1970 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1971 return;
1972 setDoesNotThrow(F);
1973 setDoesNotCapture(F, 1);
1974 } else if (Name == "getenv") {
1975 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1976 return;
1977 setDoesNotThrow(F);
1978 setOnlyReadsMemory(F);
1979 setDoesNotCapture(F, 1);
1980 } else if (Name == "gets" ||
1981 Name == "getchar") {
1982 setDoesNotThrow(F);
1983 } else if (Name == "getitimer") {
1984 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1985 return;
1986 setDoesNotThrow(F);
1987 setDoesNotCapture(F, 2);
1988 } else if (Name == "getpwnam") {
1989 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1990 return;
1991 setDoesNotThrow(F);
1992 setDoesNotCapture(F, 1);
1993 }
1994 break;
1995 case 'u':
1996 if (Name == "ungetc") {
1997 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1998 return;
1999 setDoesNotThrow(F);
2000 setDoesNotCapture(F, 2);
2001 } else if (Name == "uname" ||
2002 Name == "unlink" ||
2003 Name == "unsetenv") {
2004 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
2005 return;
2006 setDoesNotThrow(F);
2007 setDoesNotCapture(F, 1);
2008 } else if (Name == "utime" ||
2009 Name == "utimes") {
2010 if (FTy->getNumParams() != 2 ||
2011 !FTy->getParamType(0)->isPointerTy() ||
2012 !FTy->getParamType(1)->isPointerTy())
2013 return;
2014 setDoesNotThrow(F);
2015 setDoesNotCapture(F, 1);
2016 setDoesNotCapture(F, 2);
2017 }
2018 break;
2019 case 'p':
2020 if (Name == "putc") {
2021 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
2022 return;
2023 setDoesNotThrow(F);
2024 setDoesNotCapture(F, 2);
2025 } else if (Name == "puts" ||
2026 Name == "printf" ||
2027 Name == "perror") {
2028 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
2029 return;
2030 setDoesNotThrow(F);
2031 setDoesNotCapture(F, 1);
2032 } else if (Name == "pread" ||
2033 Name == "pwrite") {
2034 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
2035 return;
2036 // May throw; these are valid pthread cancellation points.
2037 setDoesNotCapture(F, 2);
2038 } else if (Name == "putchar") {
2039 setDoesNotThrow(F);
2040 } else if (Name == "popen") {
2041 if (FTy->getNumParams() != 2 ||
2042 !FTy->getReturnType()->isPointerTy() ||
2043 !FTy->getParamType(0)->isPointerTy() ||
2044 !FTy->getParamType(1)->isPointerTy())
2045 return;
2046 setDoesNotThrow(F);
2047 setDoesNotAlias(F, 0);
2048 setDoesNotCapture(F, 1);
2049 setDoesNotCapture(F, 2);
2050 } else if (Name == "pclose") {
2051 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
2052 return;
2053 setDoesNotThrow(F);
2054 setDoesNotCapture(F, 1);
2055 }
2056 break;
2057 case 'v':
2058 if (Name == "vscanf") {
2059 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
2060 return;
2061 setDoesNotThrow(F);
2062 setDoesNotCapture(F, 1);
2063 } else if (Name == "vsscanf" ||
2064 Name == "vfscanf") {
2065 if (FTy->getNumParams() != 3 ||
2066 !FTy->getParamType(1)->isPointerTy() ||
2067 !FTy->getParamType(2)->isPointerTy())
2068 return;
2069 setDoesNotThrow(F);
2070 setDoesNotCapture(F, 1);
2071 setDoesNotCapture(F, 2);
2072 } else if (Name == "valloc") {
2073 if (!FTy->getReturnType()->isPointerTy())
2074 return;
2075 setDoesNotThrow(F);
2076 setDoesNotAlias(F, 0);
2077 } else if (Name == "vprintf") {
2078 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
2079 return;
2080 setDoesNotThrow(F);
2081 setDoesNotCapture(F, 1);
2082 } else if (Name == "vfprintf" ||
2083 Name == "vsprintf") {
2084 if (FTy->getNumParams() != 3 ||
2085 !FTy->getParamType(0)->isPointerTy() ||
2086 !FTy->getParamType(1)->isPointerTy())
2087 return;
2088 setDoesNotThrow(F);
2089 setDoesNotCapture(F, 1);
2090 setDoesNotCapture(F, 2);
2091 } else if (Name == "vsnprintf") {
2092 if (FTy->getNumParams() != 4 ||
2093 !FTy->getParamType(0)->isPointerTy() ||
2094 !FTy->getParamType(2)->isPointerTy())
2095 return;
2096 setDoesNotThrow(F);
2097 setDoesNotCapture(F, 1);
2098 setDoesNotCapture(F, 3);
2099 }
2100 break;
2101 case 'o':
2102 if (Name == "open") {
2103 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
2104 return;
2105 // May throw; "open" is a valid pthread cancellation point.
2106 setDoesNotCapture(F, 1);
2107 } else if (Name == "opendir") {
2108 if (FTy->getNumParams() != 1 ||
2109 !FTy->getReturnType()->isPointerTy() ||
2110 !FTy->getParamType(0)->isPointerTy())
2111 return;
2112 setDoesNotThrow(F);
2113 setDoesNotAlias(F, 0);
2114 setDoesNotCapture(F, 1);
2115 }
2116 break;
2117 case 't':
2118 if (Name == "tmpfile") {
2119 if (!FTy->getReturnType()->isPointerTy())
2120 return;
2121 setDoesNotThrow(F);
2122 setDoesNotAlias(F, 0);
2123 } else if (Name == "times") {
2124 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
2125 return;
2126 setDoesNotThrow(F);
2127 setDoesNotCapture(F, 1);
2128 }
2129 break;
2130 case 'h':
2131 if (Name == "htonl" ||
2132 Name == "htons") {
2133 setDoesNotThrow(F);
2134 setDoesNotAccessMemory(F);
2135 }
2136 break;
2137 case 'n':
2138 if (Name == "ntohl" ||
2139 Name == "ntohs") {
2140 setDoesNotThrow(F);
2141 setDoesNotAccessMemory(F);
2142 }
2143 break;
2144 case 'l':
2145 if (Name == "lstat") {
2146 if (FTy->getNumParams() != 2 ||
2147 !FTy->getParamType(0)->isPointerTy() ||
2148 !FTy->getParamType(1)->isPointerTy())
2149 return;
2150 setDoesNotThrow(F);
2151 setDoesNotCapture(F, 1);
2152 setDoesNotCapture(F, 2);
2153 } else if (Name == "lchown") {
2154 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
2155 return;
2156 setDoesNotThrow(F);
2157 setDoesNotCapture(F, 1);
2158 }
2159 break;
2160 case 'q':
2161 if (Name == "qsort") {
2162 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
2163 return;
2164 // May throw; places call through function pointer.
2165 setDoesNotCapture(F, 4);
2166 }
2167 break;
2168 case '_':
2169 if (Name == "__strdup" ||
2170 Name == "__strndup") {
2171 if (FTy->getNumParams() < 1 ||
2172 !FTy->getReturnType()->isPointerTy() ||
2173 !FTy->getParamType(0)->isPointerTy())
2174 return;
2175 setDoesNotThrow(F);
2176 setDoesNotAlias(F, 0);
2177 setDoesNotCapture(F, 1);
2178 } else if (Name == "__strtok_r") {
2179 if (FTy->getNumParams() != 3 ||
2180 !FTy->getParamType(1)->isPointerTy())
2181 return;
2182 setDoesNotThrow(F);
2183 setDoesNotCapture(F, 2);
2184 } else if (Name == "_IO_getc") {
2185 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
2186 return;
2187 setDoesNotThrow(F);
2188 setDoesNotCapture(F, 1);
2189 } else if (Name == "_IO_putc") {
2190 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
2191 return;
2192 setDoesNotThrow(F);
2193 setDoesNotCapture(F, 2);
2194 }
2195 break;
2196 case 1:
2197 if (Name == "\1__isoc99_scanf") {
2198 if (FTy->getNumParams() < 1 ||
2199 !FTy->getParamType(0)->isPointerTy())
2200 return;
2201 setDoesNotThrow(F);
2202 setDoesNotCapture(F, 1);
2203 } else if (Name == "\1stat64" ||
2204 Name == "\1lstat64" ||
2205 Name == "\1statvfs64" ||
2206 Name == "\1__isoc99_sscanf") {
2207 if (FTy->getNumParams() < 1 ||
2208 !FTy->getParamType(0)->isPointerTy() ||
2209 !FTy->getParamType(1)->isPointerTy())
2210 return;
2211 setDoesNotThrow(F);
2212 setDoesNotCapture(F, 1);
2213 setDoesNotCapture(F, 2);
2214 } else if (Name == "\1fopen64") {
2215 if (FTy->getNumParams() != 2 ||
2216 !FTy->getReturnType()->isPointerTy() ||
2217 !FTy->getParamType(0)->isPointerTy() ||
2218 !FTy->getParamType(1)->isPointerTy())
2219 return;
2220 setDoesNotThrow(F);
2221 setDoesNotAlias(F, 0);
2222 setDoesNotCapture(F, 1);
2223 setDoesNotCapture(F, 2);
2224 } else if (Name == "\1fseeko64" ||
2225 Name == "\1ftello64") {
2226 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
2227 return;
2228 setDoesNotThrow(F);
2229 setDoesNotCapture(F, 1);
2230 } else if (Name == "\1tmpfile64") {
2231 if (!FTy->getReturnType()->isPointerTy())
2232 return;
2233 setDoesNotThrow(F);
2234 setDoesNotAlias(F, 0);
2235 } else if (Name == "\1fstat64" ||
2236 Name == "\1fstatvfs64") {
2237 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
2238 return;
2239 setDoesNotThrow(F);
2240 setDoesNotCapture(F, 2);
2241 } else if (Name == "\1open64") {
2242 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
2243 return;
2244 // May throw; "open" is a valid pthread cancellation point.
2245 setDoesNotCapture(F, 1);
2246 }
2247 break;
2248 }
2249}
2250
Nick Lewycky6cd0c042009-01-05 00:07:50 +00002251/// doInitialization - Add attributes to well-known functions.
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002252///
Nick Lewycky6cd0c042009-01-05 00:07:50 +00002253bool SimplifyLibCalls::doInitialization(Module &M) {
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002254 Modified = false;
2255 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
2256 Function &F = *I;
Chris Lattnere265ad82011-02-24 07:12:12 +00002257 if (F.isDeclaration() && F.hasName())
2258 inferPrototypeAttributes(F);
Nick Lewycky0f8df9a2009-01-04 20:27:34 +00002259 }
2260 return Modified;
2261}
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002262
2263// TODO:
2264// Additional cases that we need to add to this file:
2265//
2266// cbrt:
2267// * cbrt(expN(X)) -> expN(x/3)
2268// * cbrt(sqrt(x)) -> pow(x,1/6)
2269// * cbrt(sqrt(x)) -> pow(x,1/9)
2270//
2271// cos, cosf, cosl:
2272// * cos(-x) -> cos(x)
2273//
2274// exp, expf, expl:
2275// * exp(log(x)) -> x
2276//
2277// log, logf, logl:
2278// * log(exp(x)) -> x
2279// * log(x**y) -> y*log(x)
2280// * log(exp(y)) -> y*log(e)
2281// * log(exp2(y)) -> y*log(2)
2282// * log(exp10(y)) -> y*log(10)
2283// * log(sqrt(x)) -> 0.5*log(x)
2284// * log(pow(x,y)) -> y*log(x)
2285//
2286// lround, lroundf, lroundl:
2287// * lround(cnst) -> cnst'
2288//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002289// pow, powf, powl:
2290// * pow(exp(x),y) -> exp(x*y)
2291// * pow(sqrt(x),y) -> pow(x,y*0.5)
2292// * pow(pow(x,y),z)-> pow(x,y*z)
2293//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002294// round, roundf, roundl:
2295// * round(cnst) -> cnst'
2296//
2297// signbit:
2298// * signbit(cnst) -> cnst'
2299// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2300//
2301// sqrt, sqrtf, sqrtl:
2302// * sqrt(expN(x)) -> expN(x*0.5)
2303// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2304// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2305//
2306// stpcpy:
2307// * stpcpy(str, "literal") ->
2308// llvm.memcpy(str,"literal",strlen("literal")+1,1)
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002309//
Chris Lattnerfd1cbbe2008-05-01 06:25:24 +00002310// tan, tanf, tanl:
2311// * tan(atan(x)) -> x
2312//
2313// trunc, truncf, truncl:
2314// * trunc(cnst) -> cnst'
2315//
2316//