blob: a03bc7e9cf3750a6eb87629f88175f54ccbd9446 [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
12// library functions). For example, a call to the function "exit(3)" that
13// occurs within the main() function can be transformed into a simple "return 3"
14// instruction. Any optimization that takes this form (replace call to library
15// function with simpler code that provides the same result) belongs in this
16// file.
17//
18//===----------------------------------------------------------------------===//
19
20#define DEBUG_TYPE "simplify-libcalls"
21#include "llvm/Transforms/Scalar.h"
22#include "llvm/Intrinsics.h"
23#include "llvm/Module.h"
24#include "llvm/Pass.h"
25#include "llvm/Support/IRBuilder.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/ADT/SmallPtrSet.h"
28#include "llvm/ADT/StringMap.h"
29#include "llvm/ADT/Statistic.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Config/config.h"
32using namespace llvm;
33
34STATISTIC(NumSimplified, "Number of library calls simplified");
35
36//===----------------------------------------------------------------------===//
37// Optimizer Base Class
38//===----------------------------------------------------------------------===//
39
40/// This class is the abstract base class for the set of optimizations that
41/// corresponds to one library call.
42namespace {
43class VISIBILITY_HIDDEN LibCallOptimization {
44protected:
45 Function *Caller;
46 const TargetData *TD;
47public:
48 LibCallOptimization() { }
49 virtual ~LibCallOptimization() {}
50
51 /// CallOptimizer - This pure virtual method is implemented by base classes to
52 /// do various optimizations. If this returns null then no transformation was
53 /// performed. If it returns CI, then it transformed the call and CI is to be
54 /// deleted. If it returns something else, replace CI with the new value and
55 /// delete CI.
56 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) =0;
57
58 Value *OptimizeCall(CallInst *CI, const TargetData &TD, IRBuilder &B) {
59 Caller = CI->getParent()->getParent();
60 this->TD = &TD;
61 return CallOptimizer(CI->getCalledFunction(), CI, B);
62 }
63
64 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
65 Value *CastToCStr(Value *V, IRBuilder &B);
66
67 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
68 /// specified pointer. Ptr is required to be some pointer type, and the
69 /// return value has 'intptr_t' type.
70 Value *EmitStrLen(Value *Ptr, IRBuilder &B);
71
72 /// EmitMemCpy - Emit a call to the memcpy function to the builder. This
73 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
74 Value *EmitMemCpy(Value *Dst, Value *Src, Value *Len,
75 unsigned Align, IRBuilder &B);
76
77 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
78 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
79 Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder &B);
80
81 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
82 /// 'floor'). This function is known to take a single of type matching 'Op'
83 /// and returns one value with the same type. If 'Op' is a long double, 'l'
84 /// is added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
85 Value *EmitUnaryFloatFnCall(Value *Op, const char *Name, IRBuilder &B);
86
87 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
88 /// is an integer.
89 void EmitPutChar(Value *Char, IRBuilder &B);
90
91 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
92 /// some pointer.
93 void EmitPutS(Value *Str, IRBuilder &B);
94
95 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
96 /// an i32, and File is a pointer to FILE.
97 void EmitFPutC(Value *Char, Value *File, IRBuilder &B);
98
99 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
100 /// pointer and File is a pointer to FILE.
101 void EmitFPutS(Value *Str, Value *File, IRBuilder &B);
102
103 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
104 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
105 void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder &B);
106
107};
108} // End anonymous namespace.
109
110/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
111Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder &B) {
112 return B.CreateBitCast(V, PointerType::getUnqual(Type::Int8Ty), "cstr");
113}
114
115/// EmitStrLen - Emit a call to the strlen function to the builder, for the
116/// specified pointer. This always returns an integer value of size intptr_t.
117Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder &B) {
118 Module *M = Caller->getParent();
119 Constant *StrLen =M->getOrInsertFunction("strlen", TD->getIntPtrType(),
120 PointerType::getUnqual(Type::Int8Ty),
121 NULL);
122 return B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
123}
124
125/// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
126/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
127Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
128 unsigned Align, IRBuilder &B) {
129 Module *M = Caller->getParent();
130 Intrinsic::ID IID = TD->getIntPtrType() == Type::Int32Ty ?
131 Intrinsic::memcpy_i32 : Intrinsic::memcpy_i64;
132 Value *MemCpy = Intrinsic::getDeclaration(M, IID);
133 return B.CreateCall4(MemCpy, CastToCStr(Dst, B), CastToCStr(Src, B), Len,
134 ConstantInt::get(Type::Int32Ty, Align));
135}
136
137/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
138/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
139Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
140 Value *Len, IRBuilder &B) {
141 Module *M = Caller->getParent();
142 Value *MemChr = M->getOrInsertFunction("memchr",
143 PointerType::getUnqual(Type::Int8Ty),
144 PointerType::getUnqual(Type::Int8Ty),
145 Type::Int32Ty, TD->getIntPtrType(),
146 NULL);
147 return B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
148}
149
150/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
151/// 'floor'). This function is known to take a single of type matching 'Op' and
152/// returns one value with the same type. If 'Op' is a long double, 'l' is
153/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
154Value *LibCallOptimization::EmitUnaryFloatFnCall(Value *Op, const char *Name,
155 IRBuilder &B) {
156 char NameBuffer[20];
157 if (Op->getType() != Type::DoubleTy) {
158 // If we need to add a suffix, copy into NameBuffer.
159 unsigned NameLen = strlen(Name);
160 assert(NameLen < sizeof(NameBuffer)-2);
161 memcpy(NameBuffer, Name, NameLen);
162 if (Op->getType() == Type::FloatTy)
163 NameBuffer[NameLen] = 'f'; // floorf
164 else
165 NameBuffer[NameLen] = 'l'; // floorl
166 NameBuffer[NameLen+1] = 0;
167 Name = NameBuffer;
168 }
169
170 Module *M = Caller->getParent();
171 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
172 Op->getType(), NULL);
173 return B.CreateCall(Callee, Op, Name);
174}
175
176/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
177/// is an integer.
178void LibCallOptimization::EmitPutChar(Value *Char, IRBuilder &B) {
179 Module *M = Caller->getParent();
180 Value *F = M->getOrInsertFunction("putchar", Type::Int32Ty,
181 Type::Int32Ty, NULL);
182 B.CreateCall(F, B.CreateIntCast(Char, Type::Int32Ty, "chari"), "putchar");
183}
184
185/// EmitPutS - Emit a call to the puts function. This assumes that Str is
186/// some pointer.
187void LibCallOptimization::EmitPutS(Value *Str, IRBuilder &B) {
188 Module *M = Caller->getParent();
189 Value *F = M->getOrInsertFunction("puts", Type::Int32Ty,
190 PointerType::getUnqual(Type::Int8Ty), NULL);
191 B.CreateCall(F, CastToCStr(Str, B), "puts");
192}
193
194/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
195/// an integer and File is a pointer to FILE.
196void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder &B) {
197 Module *M = Caller->getParent();
198 Constant *F = M->getOrInsertFunction("fputc", Type::Int32Ty, Type::Int32Ty,
199 File->getType(), NULL);
200 Char = B.CreateIntCast(Char, Type::Int32Ty, "chari");
201 B.CreateCall2(F, Char, File, "fputc");
202}
203
204/// EmitFPutS - Emit a call to the puts function. Str is required to be a
205/// pointer and File is a pointer to FILE.
206void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder &B) {
207 Module *M = Caller->getParent();
208 Constant *F = M->getOrInsertFunction("fputs", Type::Int32Ty,
209 PointerType::getUnqual(Type::Int8Ty),
210 File->getType(), NULL);
211 B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
212}
213
214/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
215/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
216void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
217 IRBuilder &B) {
218 Module *M = Caller->getParent();
219 Constant *F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(),
220 PointerType::getUnqual(Type::Int8Ty),
221 TD->getIntPtrType(), TD->getIntPtrType(),
222 File->getType(), NULL);
223 B.CreateCall4(F, CastToCStr(Ptr, B), Size,
224 ConstantInt::get(TD->getIntPtrType(), 1), File);
225}
226
227//===----------------------------------------------------------------------===//
228// Helper Functions
229//===----------------------------------------------------------------------===//
230
231/// GetConstantStringInfo - This function computes the length of a
232/// null-terminated C string pointed to by V. If successful, it returns true
233/// and returns the string in Str. If unsuccessful, it returns false.
234static bool GetConstantStringInfo(Value *V, std::string &Str) {
235 // Look bitcast instructions.
236 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
237 return GetConstantStringInfo(BCI->getOperand(0), Str);
238
239 // If the value is not a GEP instruction nor a constant expression with a
240 // GEP instruction, then return false because ConstantArray can't occur
241 // any other way
242 User *GEP = 0;
243 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
244 GEP = GEPI;
245 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
246 if (CE->getOpcode() != Instruction::GetElementPtr)
247 return false;
248 GEP = CE;
249 } else {
250 return false;
251 }
252
253 // Make sure the GEP has exactly three arguments.
254 if (GEP->getNumOperands() != 3)
255 return false;
256
257 // Check to make sure that the first operand of the GEP is an integer and
258 // has value 0 so that we are sure we're indexing into the initializer.
259 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
260 if (!Idx->isZero())
261 return false;
262 } else
263 return false;
264
265 // If the second index isn't a ConstantInt, then this is a variable index
266 // into the array. If this occurs, we can't say anything meaningful about
267 // the string.
268 uint64_t StartIdx = 0;
269 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
270 StartIdx = CI->getZExtValue();
271 else
272 return false;
273
274 // The GEP instruction, constant or instruction, must reference a global
275 // variable that is a constant and is initialized. The referenced constant
276 // initializer is the array that we'll use for optimization.
277 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
278 if (!GV || !GV->isConstant() || !GV->hasInitializer())
279 return false;
280 Constant *GlobalInit = GV->getInitializer();
281
282 // Handle the ConstantAggregateZero case
283 if (isa<ConstantAggregateZero>(GlobalInit)) {
284 // This is a degenerate case. The initializer is constant zero so the
285 // length of the string must be zero.
286 Str.clear();
287 return true;
288 }
289
290 // Must be a Constant Array
291 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
292 if (Array == 0 || Array->getType()->getElementType() != Type::Int8Ty)
293 return false;
294
295 // Get the number of elements in the array
296 uint64_t NumElts = Array->getType()->getNumElements();
297
298 // Traverse the constant array from StartIdx (derived above) which is
299 // the place the GEP refers to in the array.
300 for (unsigned i = StartIdx; i < NumElts; ++i) {
301 Constant *Elt = Array->getOperand(i);
302 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
303 if (!CI) // This array isn't suitable, non-int initializer.
304 return false;
305 if (CI->isZero())
306 return true; // we found end of string, success!
307 Str += (char)CI->getZExtValue();
308 }
309
310 return false; // The array isn't null terminated.
311}
312
313/// GetStringLengthH - If we can compute the length of the string pointed to by
314/// the specified pointer, return 'len+1'. If we can't, return 0.
315static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
316 // Look through noop bitcast instructions.
317 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
318 return GetStringLengthH(BCI->getOperand(0), PHIs);
319
320 // If this is a PHI node, there are two cases: either we have already seen it
321 // or we haven't.
322 if (PHINode *PN = dyn_cast<PHINode>(V)) {
323 if (!PHIs.insert(PN))
324 return ~0ULL; // already in the set.
325
326 // If it was new, see if all the input strings are the same length.
327 uint64_t LenSoFar = ~0ULL;
328 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
329 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
330 if (Len == 0) return 0; // Unknown length -> unknown.
331
332 if (Len == ~0ULL) continue;
333
334 if (Len != LenSoFar && LenSoFar != ~0ULL)
335 return 0; // Disagree -> unknown.
336 LenSoFar = Len;
337 }
338
339 // Success, all agree.
340 return LenSoFar;
341 }
342
343 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
344 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
345 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
346 if (Len1 == 0) return 0;
347 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
348 if (Len2 == 0) return 0;
349 if (Len1 == ~0ULL) return Len2;
350 if (Len2 == ~0ULL) return Len1;
351 if (Len1 != Len2) return 0;
352 return Len1;
353 }
354
355 // If the value is not a GEP instruction nor a constant expression with a
356 // GEP instruction, then return unknown.
357 User *GEP = 0;
358 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
359 GEP = GEPI;
360 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
361 if (CE->getOpcode() != Instruction::GetElementPtr)
362 return 0;
363 GEP = CE;
364 } else {
365 return 0;
366 }
367
368 // Make sure the GEP has exactly three arguments.
369 if (GEP->getNumOperands() != 3)
370 return 0;
371
372 // Check to make sure that the first operand of the GEP is an integer and
373 // has value 0 so that we are sure we're indexing into the initializer.
374 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
375 if (!Idx->isZero())
376 return 0;
377 } else
378 return 0;
379
380 // If the second index isn't a ConstantInt, then this is a variable index
381 // into the array. If this occurs, we can't say anything meaningful about
382 // the string.
383 uint64_t StartIdx = 0;
384 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
385 StartIdx = CI->getZExtValue();
386 else
387 return 0;
388
389 // The GEP instruction, constant or instruction, must reference a global
390 // variable that is a constant and is initialized. The referenced constant
391 // initializer is the array that we'll use for optimization.
392 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
393 if (!GV || !GV->isConstant() || !GV->hasInitializer())
394 return 0;
395 Constant *GlobalInit = GV->getInitializer();
396
397 // Handle the ConstantAggregateZero case, which is a degenerate case. The
398 // initializer is constant zero so the length of the string must be zero.
399 if (isa<ConstantAggregateZero>(GlobalInit))
400 return 1; // Len = 0 offset by 1.
401
402 // Must be a Constant Array
403 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
404 if (!Array || Array->getType()->getElementType() != Type::Int8Ty)
405 return false;
406
407 // Get the number of elements in the array
408 uint64_t NumElts = Array->getType()->getNumElements();
409
410 // Traverse the constant array from StartIdx (derived above) which is
411 // the place the GEP refers to in the array.
412 for (unsigned i = StartIdx; i != NumElts; ++i) {
413 Constant *Elt = Array->getOperand(i);
414 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
415 if (!CI) // This array isn't suitable, non-int initializer.
416 return 0;
417 if (CI->isZero())
418 return i-StartIdx+1; // We found end of string, success!
419 }
420
421 return 0; // The array isn't null terminated, conservatively return 'unknown'.
422}
423
424/// GetStringLength - If we can compute the length of the string pointed to by
425/// the specified pointer, return 'len+1'. If we can't, return 0.
426static uint64_t GetStringLength(Value *V) {
427 if (!isa<PointerType>(V->getType())) return 0;
428
429 SmallPtrSet<PHINode*, 32> PHIs;
430 uint64_t Len = GetStringLengthH(V, PHIs);
431 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
432 // an empty string as a length.
433 return Len == ~0ULL ? 1 : Len;
434}
435
436/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
437/// value is equal or not-equal to zero.
438static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
439 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
440 UI != E; ++UI) {
441 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
442 if (IC->isEquality())
443 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
444 if (C->isNullValue())
445 continue;
446 // Unknown instruction.
447 return false;
448 }
449 return true;
450}
451
452//===----------------------------------------------------------------------===//
453// Miscellaneous LibCall Optimizations
454//===----------------------------------------------------------------------===//
455
456//===---------------------------------------===//
457// 'exit' Optimizations
458
459/// ExitOpt - int main() { exit(4); } --> int main() { return 4; }
460struct VISIBILITY_HIDDEN ExitOpt : public LibCallOptimization {
461 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
462 // Verify we have a reasonable prototype for exit.
463 if (Callee->arg_size() == 0 || !CI->use_empty())
464 return 0;
465
466 // Verify the caller is main, and that the result type of main matches the
467 // argument type of exit.
468 if (!Caller->isName("main") || !Caller->hasExternalLinkage() ||
469 Caller->getReturnType() != CI->getOperand(1)->getType())
470 return 0;
471
472 TerminatorInst *OldTI = CI->getParent()->getTerminator();
473
474 // Create the return after the call.
475 ReturnInst *RI = B.CreateRet(CI->getOperand(1));
476
477 // Drop all successor phi node entries.
478 for (unsigned i = 0, e = OldTI->getNumSuccessors(); i != e; ++i)
479 OldTI->getSuccessor(i)->removePredecessor(CI->getParent());
480
481 // Erase all instructions from after our return instruction until the end of
482 // the block.
483 BasicBlock::iterator FirstDead = RI; ++FirstDead;
484 CI->getParent()->getInstList().erase(FirstDead, CI->getParent()->end());
485 return CI;
486 }
487};
488
489//===----------------------------------------------------------------------===//
490// String and Memory LibCall Optimizations
491//===----------------------------------------------------------------------===//
492
493//===---------------------------------------===//
494// 'strcat' Optimizations
495
496struct VISIBILITY_HIDDEN StrCatOpt : public LibCallOptimization {
497 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
498 // Verify the "strcat" function prototype.
499 const FunctionType *FT = Callee->getFunctionType();
500 if (FT->getNumParams() != 2 ||
501 FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
502 FT->getParamType(0) != FT->getReturnType() ||
503 FT->getParamType(1) != FT->getReturnType())
504 return 0;
505
506 // Extract some information from the instruction
507 Value *Dst = CI->getOperand(1);
508 Value *Src = CI->getOperand(2);
509
510 // See if we can get the length of the input string.
511 uint64_t Len = GetStringLength(Src);
512 if (Len == 0) return false;
513 --Len; // Unbias length.
514
515 // Handle the simple, do-nothing case: strcat(x, "") -> x
516 if (Len == 0)
517 return Dst;
518
519 // We need to find the end of the destination string. That's where the
520 // memory is to be moved to. We just generate a call to strlen.
521 Value *DstLen = EmitStrLen(Dst, B);
522
523 // Now that we have the destination's length, we must index into the
524 // destination's pointer to get the actual memcpy destination (end of
525 // the string .. we're concatenating).
526 Dst = B.CreateGEP(Dst, DstLen, "endptr");
527
528 // We have enough information to now generate the memcpy call to do the
529 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
530 EmitMemCpy(Dst, Src, ConstantInt::get(TD->getIntPtrType(), Len+1), 1, B);
531 return Dst;
532 }
533};
534
535//===---------------------------------------===//
536// 'strchr' Optimizations
537
538struct VISIBILITY_HIDDEN StrChrOpt : public LibCallOptimization {
539 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
540 // Verify the "strchr" function prototype.
541 const FunctionType *FT = Callee->getFunctionType();
542 if (FT->getNumParams() != 2 ||
543 FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
544 FT->getParamType(0) != FT->getReturnType())
545 return 0;
546
547 Value *SrcStr = CI->getOperand(1);
548
549 // If the second operand is non-constant, see if we can compute the length
550 // of the input string and turn this into memchr.
551 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
552 if (CharC == 0) {
553 uint64_t Len = GetStringLength(SrcStr);
554 if (Len == 0 || FT->getParamType(1) != Type::Int32Ty) // memchr needs i32.
555 return 0;
556
557 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
558 ConstantInt::get(TD->getIntPtrType(), Len), B);
559 }
560
561 // Otherwise, the character is a constant, see if the first argument is
562 // a string literal. If so, we can constant fold.
563 std::string Str;
564 if (!GetConstantStringInfo(SrcStr, Str))
565 return false;
566
567 // strchr can find the nul character.
568 Str += '\0';
569 char CharValue = CharC->getSExtValue();
570
571 // Compute the offset.
572 uint64_t i = 0;
573 while (1) {
574 if (i == Str.size()) // Didn't find the char. strchr returns null.
575 return Constant::getNullValue(CI->getType());
576 // Did we find our match?
577 if (Str[i] == CharValue)
578 break;
579 ++i;
580 }
581
582 // strchr(s+n,c) -> gep(s+n+i,c)
583 Value *Idx = ConstantInt::get(Type::Int64Ty, i);
584 return B.CreateGEP(SrcStr, Idx, "strchr");
585 }
586};
587
588//===---------------------------------------===//
589// 'strcmp' Optimizations
590
591struct VISIBILITY_HIDDEN StrCmpOpt : public LibCallOptimization {
592 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
593 // Verify the "strcmp" function prototype.
594 const FunctionType *FT = Callee->getFunctionType();
595 if (FT->getNumParams() != 2 || FT->getReturnType() != Type::Int32Ty ||
596 FT->getParamType(0) != FT->getParamType(1) ||
597 FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty))
598 return 0;
599
600 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
601 if (Str1P == Str2P) // strcmp(x,x) -> 0
602 return ConstantInt::get(CI->getType(), 0);
603
604 std::string Str1, Str2;
605 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
606 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
607
608 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
609 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
610
611 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
612 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
613
614 // strcmp(x, y) -> cnst (if both x and y are constant strings)
615 if (HasStr1 && HasStr2)
616 return ConstantInt::get(CI->getType(), strcmp(Str1.c_str(),Str2.c_str()));
617 return 0;
618 }
619};
620
621//===---------------------------------------===//
622// 'strncmp' Optimizations
623
624struct VISIBILITY_HIDDEN StrNCmpOpt : public LibCallOptimization {
625 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
626 // Verify the "strncmp" function prototype.
627 const FunctionType *FT = Callee->getFunctionType();
628 if (FT->getNumParams() != 3 || FT->getReturnType() != Type::Int32Ty ||
629 FT->getParamType(0) != FT->getParamType(1) ||
630 FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
631 !isa<IntegerType>(FT->getParamType(2)))
632 return 0;
633
634 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
635 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
636 return ConstantInt::get(CI->getType(), 0);
637
638 // Get the length argument if it is constant.
639 uint64_t Length;
640 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
641 Length = LengthArg->getZExtValue();
642 else
643 return 0;
644
645 if (Length == 0) // strncmp(x,y,0) -> 0
646 return ConstantInt::get(CI->getType(), 0);
647
648 std::string Str1, Str2;
649 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
650 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
651
652 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
653 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
654
655 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
656 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
657
658 // strncmp(x, y) -> cnst (if both x and y are constant strings)
659 if (HasStr1 && HasStr2)
660 return ConstantInt::get(CI->getType(),
661 strncmp(Str1.c_str(), Str2.c_str(), Length));
662 return 0;
663 }
664};
665
666
667//===---------------------------------------===//
668// 'strcpy' Optimizations
669
670struct VISIBILITY_HIDDEN StrCpyOpt : public LibCallOptimization {
671 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
672 // Verify the "strcpy" function prototype.
673 const FunctionType *FT = Callee->getFunctionType();
674 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
675 FT->getParamType(0) != FT->getParamType(1) ||
676 FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty))
677 return 0;
678
679 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
680 if (Dst == Src) // strcpy(x,x) -> x
681 return Src;
682
683 // See if we can get the length of the input string.
684 uint64_t Len = GetStringLength(Src);
685 if (Len == 0) return false;
686
687 // We have enough information to now generate the memcpy call to do the
688 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
689 EmitMemCpy(Dst, Src, ConstantInt::get(TD->getIntPtrType(), Len), 1, B);
690 return Dst;
691 }
692};
693
694
695
696//===---------------------------------------===//
697// 'strlen' Optimizations
698
699struct VISIBILITY_HIDDEN StrLenOpt : public LibCallOptimization {
700 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
701 const FunctionType *FT = Callee->getFunctionType();
702 if (FT->getNumParams() != 1 ||
703 FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
704 !isa<IntegerType>(FT->getReturnType()))
705 return 0;
706
707 Value *Src = CI->getOperand(1);
708
709 // Constant folding: strlen("xyz") -> 3
710 if (uint64_t Len = GetStringLength(Src))
711 return ConstantInt::get(CI->getType(), Len-1);
712
713 // Handle strlen(p) != 0.
714 if (!IsOnlyUsedInZeroEqualityComparison(CI)) return 0;
715
716 // strlen(x) != 0 --> *x != 0
717 // strlen(x) == 0 --> *x == 0
718 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
719 }
720};
721
722//===---------------------------------------===//
723// 'memcmp' Optimizations
724
725struct VISIBILITY_HIDDEN MemCmpOpt : public LibCallOptimization {
726 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
727 const FunctionType *FT = Callee->getFunctionType();
728 if (FT->getNumParams() != 3 || !isa<PointerType>(FT->getParamType(0)) ||
729 !isa<PointerType>(FT->getParamType(1)) ||
730 FT->getReturnType() != Type::Int32Ty)
731 return 0;
732
733 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
734
735 if (LHS == RHS) // memcmp(s,s,x) -> 0
736 return Constant::getNullValue(CI->getType());
737
738 // Make sure we have a constant length.
739 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
740 if (!LenC) return false;
741 uint64_t Len = LenC->getZExtValue();
742
743 if (Len == 0) // memcmp(s1,s2,0) -> 0
744 return Constant::getNullValue(CI->getType());
745
746 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
747 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
748 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
749 return B.CreateZExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
750 }
751
752 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS ^ *(short*)RHS) != 0
753 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS ^ *(int*)RHS) != 0
754 if ((Len == 2 || Len == 4) && IsOnlyUsedInZeroEqualityComparison(CI)) {
755 LHS = B.CreateBitCast(LHS, PointerType::getUnqual(Type::Int16Ty), "tmp");
756 RHS = B.CreateBitCast(RHS, LHS->getType(), "tmp");
757 LoadInst *LHSV = B.CreateLoad(LHS, "lhsv");
758 LoadInst *RHSV = B.CreateLoad(RHS, "rhsv");
759 LHSV->setAlignment(1); RHSV->setAlignment(1); // Unaligned loads.
760 return B.CreateZExt(B.CreateXor(LHSV, RHSV, "shortdiff"), CI->getType());
761 }
762
763 return 0;
764 }
765};
766
767//===---------------------------------------===//
768// 'memcpy' Optimizations
769
770struct VISIBILITY_HIDDEN MemCpyOpt : public LibCallOptimization {
771 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
772 const FunctionType *FT = Callee->getFunctionType();
773 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
774 !isa<PointerType>(FT->getParamType(0)) ||
775 !isa<PointerType>(FT->getParamType(1)) ||
776 FT->getParamType(2) != TD->getIntPtrType())
777 return 0;
778
779 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
780 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
781 return CI->getOperand(1);
782 }
783};
784
785//===----------------------------------------------------------------------===//
786// Math Library Optimizations
787//===----------------------------------------------------------------------===//
788
789//===---------------------------------------===//
790// 'pow*' Optimizations
791
792struct VISIBILITY_HIDDEN PowOpt : public LibCallOptimization {
793 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
794 const FunctionType *FT = Callee->getFunctionType();
795 // Just make sure this has 2 arguments of the same FP type, which match the
796 // result type.
797 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
798 FT->getParamType(0) != FT->getParamType(1) ||
799 !FT->getParamType(0)->isFloatingPoint())
800 return 0;
801
802 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
803 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
804 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
805 return Op1C;
806 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
807 return EmitUnaryFloatFnCall(Op2, "exp2", B);
808 }
809
810 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
811 if (Op2C == 0) return 0;
812
813 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
814 return ConstantFP::get(CI->getType(), 1.0);
815
816 if (Op2C->isExactlyValue(0.5)) {
817 // FIXME: This is not safe for -0.0 and -inf. This can only be done when
818 // 'unsafe' math optimizations are allowed.
819 // x pow(x, 0.5) sqrt(x)
820 // ---------------------------------------------
821 // -0.0 +0.0 -0.0
822 // -inf +inf NaN
823#if 0
824 // pow(x, 0.5) -> sqrt(x)
825 return B.CreateCall(get_sqrt(), Op1, "sqrt");
826#endif
827 }
828
829 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
830 return Op1;
831 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
832 return B.CreateMul(Op1, Op1, "pow2");
833 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
834 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), Op1, "powrecip");
835 return 0;
836 }
837};
838
839//===---------------------------------------===//
840// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
841
842struct VISIBILITY_HIDDEN UnaryDoubleFPOpt : public LibCallOptimization {
843 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
844 const FunctionType *FT = Callee->getFunctionType();
845 if (FT->getNumParams() != 1 || FT->getReturnType() != Type::DoubleTy ||
846 FT->getParamType(0) != Type::DoubleTy)
847 return 0;
848
849 // If this is something like 'floor((double)floatval)', convert to floorf.
850 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
851 if (Cast == 0 || Cast->getOperand(0)->getType() != Type::FloatTy)
852 return 0;
853
854 // floor((double)floatval) -> (double)floorf(floatval)
855 Value *V = Cast->getOperand(0);
856 V = EmitUnaryFloatFnCall(V, Callee->getNameStart(), B);
857 return B.CreateFPExt(V, Type::DoubleTy);
858 }
859};
860
861//===----------------------------------------------------------------------===//
862// Integer Optimizations
863//===----------------------------------------------------------------------===//
864
865//===---------------------------------------===//
866// 'ffs*' Optimizations
867
868struct VISIBILITY_HIDDEN FFSOpt : public LibCallOptimization {
869 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
870 const FunctionType *FT = Callee->getFunctionType();
871 // Just make sure this has 2 arguments of the same FP type, which match the
872 // result type.
873 if (FT->getNumParams() != 1 || FT->getReturnType() != Type::Int32Ty ||
874 !isa<IntegerType>(FT->getParamType(0)))
875 return 0;
876
877 Value *Op = CI->getOperand(1);
878
879 // Constant fold.
880 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
881 if (CI->getValue() == 0) // ffs(0) -> 0.
882 return Constant::getNullValue(CI->getType());
883 return ConstantInt::get(Type::Int32Ty, // ffs(c) -> cttz(c)+1
884 CI->getValue().countTrailingZeros()+1);
885 }
886
887 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
888 const Type *ArgType = Op->getType();
889 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
890 Intrinsic::cttz, &ArgType, 1);
891 Value *V = B.CreateCall(F, Op, "cttz");
892 V = B.CreateAdd(V, ConstantInt::get(Type::Int32Ty, 1), "tmp");
893 V = B.CreateIntCast(V, Type::Int32Ty, false, "tmp");
894
895 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
896 return B.CreateSelect(Cond, V, ConstantInt::get(Type::Int32Ty, 0));
897 }
898};
899
900//===---------------------------------------===//
901// 'isdigit' Optimizations
902
903struct VISIBILITY_HIDDEN IsDigitOpt : public LibCallOptimization {
904 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
905 const FunctionType *FT = Callee->getFunctionType();
906 // We require integer(i32)
907 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
908 FT->getParamType(0) != Type::Int32Ty)
909 return 0;
910
911 // isdigit(c) -> (c-'0') <u 10
912 Value *Op = CI->getOperand(1);
913 Op = B.CreateSub(Op, ConstantInt::get(Type::Int32Ty, '0'), "isdigittmp");
914 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::Int32Ty, 10), "isdigit");
915 return B.CreateZExt(Op, CI->getType());
916 }
917};
918
919//===---------------------------------------===//
920// 'isascii' Optimizations
921
922struct VISIBILITY_HIDDEN IsAsciiOpt : public LibCallOptimization {
923 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
924 const FunctionType *FT = Callee->getFunctionType();
925 // We require integer(i32)
926 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
927 FT->getParamType(0) != Type::Int32Ty)
928 return 0;
929
930 // isascii(c) -> c <u 128
931 Value *Op = CI->getOperand(1);
932 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::Int32Ty, 128), "isascii");
933 return B.CreateZExt(Op, CI->getType());
934 }
935};
936
937//===---------------------------------------===//
938// 'toascii' Optimizations
939
940struct VISIBILITY_HIDDEN ToAsciiOpt : public LibCallOptimization {
941 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
942 const FunctionType *FT = Callee->getFunctionType();
943 // We require i32(i32)
944 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
945 FT->getParamType(0) != Type::Int32Ty)
946 return 0;
947
948 // isascii(c) -> c & 0x7f
949 return B.CreateAnd(CI->getOperand(1), ConstantInt::get(CI->getType(),0x7F));
950 }
951};
952
953//===----------------------------------------------------------------------===//
954// Formatting and IO Optimizations
955//===----------------------------------------------------------------------===//
956
957//===---------------------------------------===//
958// 'printf' Optimizations
959
960struct VISIBILITY_HIDDEN PrintFOpt : public LibCallOptimization {
961 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
962 // Require one fixed pointer argument and an integer/void result.
963 const FunctionType *FT = Callee->getFunctionType();
964 if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
965 !(isa<IntegerType>(FT->getReturnType()) ||
966 FT->getReturnType() == Type::VoidTy))
967 return 0;
968
969 // Check for a fixed format string.
970 std::string FormatStr;
971 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
972 return false;
973
974 // Empty format string -> noop.
975 if (FormatStr.empty()) // Tolerate printf's declared void.
976 return CI->use_empty() ? (Value*)CI : ConstantInt::get(CI->getType(), 0);
977
978 // printf("x") -> putchar('x'), even for '%'.
979 if (FormatStr.size() == 1) {
980 EmitPutChar(ConstantInt::get(Type::Int32Ty, FormatStr[0]), B);
981 return CI->use_empty() ? (Value*)CI : ConstantInt::get(CI->getType(), 1);
982 }
983
984 // printf("foo\n") --> puts("foo")
985 if (FormatStr[FormatStr.size()-1] == '\n' &&
986 FormatStr.find('%') == std::string::npos) { // no format characters.
987 // Create a string literal with no \n on it. We expect the constant merge
988 // pass to be run after this pass, to merge duplicate strings.
989 FormatStr.erase(FormatStr.end()-1);
990 Constant *C = ConstantArray::get(FormatStr, true);
991 C = new GlobalVariable(C->getType(), true,GlobalVariable::InternalLinkage,
992 C, "str", Callee->getParent());
993 EmitPutS(C, B);
994 return CI->use_empty() ? (Value*)CI :
995 ConstantInt::get(CI->getType(), FormatStr.size()+1);
996 }
997
998 // Optimize specific format strings.
999 // printf("%c", chr) --> putchar(*(i8*)dst)
1000 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
1001 isa<IntegerType>(CI->getOperand(2)->getType())) {
1002 EmitPutChar(CI->getOperand(2), B);
1003 return CI->use_empty() ? (Value*)CI : ConstantInt::get(CI->getType(), 1);
1004 }
1005
1006 // printf("%s\n", str) --> puts(str)
1007 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
1008 isa<PointerType>(CI->getOperand(2)->getType()) &&
1009 CI->use_empty()) {
1010 EmitPutS(CI->getOperand(2), B);
1011 return CI;
1012 }
1013 return 0;
1014 }
1015};
1016
1017//===---------------------------------------===//
1018// 'sprintf' Optimizations
1019
1020struct VISIBILITY_HIDDEN SPrintFOpt : public LibCallOptimization {
1021 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
1022 // Require two fixed pointer arguments and an integer result.
1023 const FunctionType *FT = Callee->getFunctionType();
1024 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1025 !isa<PointerType>(FT->getParamType(1)) ||
1026 !isa<IntegerType>(FT->getReturnType()))
1027 return 0;
1028
1029 // Check for a fixed format string.
1030 std::string FormatStr;
1031 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1032 return false;
1033
1034 // If we just have a format string (nothing else crazy) transform it.
1035 if (CI->getNumOperands() == 3) {
1036 // Make sure there's no % in the constant array. We could try to handle
1037 // %% -> % in the future if we cared.
1038 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1039 if (FormatStr[i] == '%')
1040 return 0; // we found a format specifier, bail out.
1041
1042 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1043 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
1044 ConstantInt::get(TD->getIntPtrType(), FormatStr.size()+1),1,B);
1045 return ConstantInt::get(CI->getType(), FormatStr.size());
1046 }
1047
1048 // The remaining optimizations require the format string to be "%s" or "%c"
1049 // and have an extra operand.
1050 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1051 return 0;
1052
1053 // Decode the second character of the format string.
1054 if (FormatStr[1] == 'c') {
1055 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr
1056 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1057 Value *V = B.CreateTrunc(CI->getOperand(3), Type::Int8Ty, "char");
1058 B.CreateStore(V, CastToCStr(CI->getOperand(1), B));
1059 return ConstantInt::get(CI->getType(), 1);
1060 }
1061
1062 if (FormatStr[1] == 's') {
1063 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1064 if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
1065
1066 Value *Len = EmitStrLen(CI->getOperand(3), B);
1067 Value *IncLen = B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1),
1068 "leninc");
1069 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
1070
1071 // The sprintf result is the unincremented number of bytes in the string.
1072 return B.CreateIntCast(Len, CI->getType(), false);
1073 }
1074 return 0;
1075 }
1076};
1077
1078//===---------------------------------------===//
1079// 'fwrite' Optimizations
1080
1081struct VISIBILITY_HIDDEN FWriteOpt : public LibCallOptimization {
1082 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
1083 // Require a pointer, an integer, an integer, a pointer, returning integer.
1084 const FunctionType *FT = Callee->getFunctionType();
1085 if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
1086 !isa<IntegerType>(FT->getParamType(1)) ||
1087 !isa<IntegerType>(FT->getParamType(2)) ||
1088 !isa<PointerType>(FT->getParamType(3)) ||
1089 !isa<IntegerType>(FT->getReturnType()))
1090 return 0;
1091
1092 // Get the element size and count.
1093 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1094 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1095 if (!SizeC || !CountC) return 0;
1096 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
1097
1098 // If this is writing zero records, remove the call (it's a noop).
1099 if (Bytes == 0)
1100 return ConstantInt::get(CI->getType(), 0);
1101
1102 // If this is writing one byte, turn it into fputc.
1103 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1104 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1105 EmitFPutC(Char, CI->getOperand(4), B);
1106 return ConstantInt::get(CI->getType(), 1);
1107 }
1108
1109 return 0;
1110 }
1111};
1112
1113//===---------------------------------------===//
1114// 'fputs' Optimizations
1115
1116struct VISIBILITY_HIDDEN FPutsOpt : public LibCallOptimization {
1117 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
1118 // Require two pointers. Also, we can't optimize if return value is used.
1119 const FunctionType *FT = Callee->getFunctionType();
1120 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1121 !isa<PointerType>(FT->getParamType(1)) ||
1122 !CI->use_empty())
1123 return 0;
1124
1125 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1126 uint64_t Len = GetStringLength(CI->getOperand(1));
1127 if (!Len) return false;
1128 EmitFWrite(CI->getOperand(1), ConstantInt::get(TD->getIntPtrType(), Len-1),
1129 CI->getOperand(2), B);
1130 return CI; // Known to have no uses (see above).
1131 }
1132};
1133
1134//===---------------------------------------===//
1135// 'fprintf' Optimizations
1136
1137struct VISIBILITY_HIDDEN FPrintFOpt : public LibCallOptimization {
1138 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
1139 // Require two fixed paramters as pointers and integer result.
1140 const FunctionType *FT = Callee->getFunctionType();
1141 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1142 !isa<PointerType>(FT->getParamType(1)) ||
1143 !isa<IntegerType>(FT->getReturnType()))
1144 return 0;
1145
1146 // All the optimizations depend on the format string.
1147 std::string FormatStr;
1148 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1149 return false;
1150
1151 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1152 if (CI->getNumOperands() == 3) {
1153 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1154 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
1155 return false; // We found a format specifier.
1156
1157 EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(),
1158 FormatStr.size()),
1159 CI->getOperand(1), B);
1160 return ConstantInt::get(CI->getType(), FormatStr.size());
1161 }
1162
1163 // The remaining optimizations require the format string to be "%s" or "%c"
1164 // and have an extra operand.
1165 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1166 return 0;
1167
1168 // Decode the second character of the format string.
1169 if (FormatStr[1] == 'c') {
1170 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1171 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1172 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
1173 return ConstantInt::get(CI->getType(), 1);
1174 }
1175
1176 if (FormatStr[1] == 's') {
1177 // fprintf(F, "%s", str) -> fputs(str, F)
1178 if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
1179 return 0;
1180 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1181 return CI;
1182 }
1183 return 0;
1184 }
1185};
1186
1187
1188//===----------------------------------------------------------------------===//
1189// SimplifyLibCalls Pass Implementation
1190//===----------------------------------------------------------------------===//
1191
1192namespace {
1193 /// This pass optimizes well known library functions from libc and libm.
1194 ///
1195 class VISIBILITY_HIDDEN SimplifyLibCalls : public FunctionPass {
1196 StringMap<LibCallOptimization*> Optimizations;
1197 // Miscellaneous LibCall Optimizations
1198 ExitOpt Exit;
1199 // String and Memory LibCall Optimizations
1200 StrCatOpt StrCat; StrChrOpt StrChr; StrCmpOpt StrCmp; StrNCmpOpt StrNCmp;
1201 StrCpyOpt StrCpy; StrLenOpt StrLen; MemCmpOpt MemCmp; MemCpyOpt MemCpy;
1202 // Math Library Optimizations
1203 PowOpt Pow; UnaryDoubleFPOpt UnaryDoubleFP;
1204 // Integer Optimizations
1205 FFSOpt FFS; IsDigitOpt IsDigit; IsAsciiOpt IsAscii; ToAsciiOpt ToAscii;
1206 // Formatting and IO Optimizations
1207 SPrintFOpt SPrintF; PrintFOpt PrintF;
1208 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
1209 public:
1210 static char ID; // Pass identification
1211 SimplifyLibCalls() : FunctionPass((intptr_t)&ID) {}
1212
1213 void InitOptimizations();
1214 bool runOnFunction(Function &F);
1215
1216 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1217 AU.addRequired<TargetData>();
1218 }
1219 };
1220 char SimplifyLibCalls::ID = 0;
1221} // end anonymous namespace.
1222
1223static RegisterPass<SimplifyLibCalls>
1224X("simplify-libcalls", "Simplify well-known library calls");
1225
1226// Public interface to the Simplify LibCalls pass.
1227FunctionPass *llvm::createSimplifyLibCallsPass() {
1228 return new SimplifyLibCalls();
1229}
1230
1231/// Optimizations - Populate the Optimizations map with all the optimizations
1232/// we know.
1233void SimplifyLibCalls::InitOptimizations() {
1234 // Miscellaneous LibCall Optimizations
1235 Optimizations["exit"] = &Exit;
1236
1237 // String and Memory LibCall Optimizations
1238 Optimizations["strcat"] = &StrCat;
1239 Optimizations["strchr"] = &StrChr;
1240 Optimizations["strcmp"] = &StrCmp;
1241 Optimizations["strncmp"] = &StrNCmp;
1242 Optimizations["strcpy"] = &StrCpy;
1243 Optimizations["strlen"] = &StrLen;
1244 Optimizations["memcmp"] = &MemCmp;
1245 Optimizations["memcpy"] = &MemCpy;
1246
1247 // Math Library Optimizations
1248 Optimizations["powf"] = &Pow;
1249 Optimizations["pow"] = &Pow;
1250 Optimizations["powl"] = &Pow;
1251#ifdef HAVE_FLOORF
1252 Optimizations["floor"] = &UnaryDoubleFP;
1253#endif
1254#ifdef HAVE_CEILF
1255 Optimizations["ceil"] = &UnaryDoubleFP;
1256#endif
1257#ifdef HAVE_ROUNDF
1258 Optimizations["round"] = &UnaryDoubleFP;
1259#endif
1260#ifdef HAVE_RINTF
1261 Optimizations["rint"] = &UnaryDoubleFP;
1262#endif
1263#ifdef HAVE_NEARBYINTF
1264 Optimizations["nearbyint"] = &UnaryDoubleFP;
1265#endif
1266
1267 // Integer Optimizations
1268 Optimizations["ffs"] = &FFS;
1269 Optimizations["ffsl"] = &FFS;
1270 Optimizations["ffsll"] = &FFS;
1271 Optimizations["isdigit"] = &IsDigit;
1272 Optimizations["isascii"] = &IsAscii;
1273 Optimizations["toascii"] = &ToAscii;
1274
1275 // Formatting and IO Optimizations
1276 Optimizations["sprintf"] = &SPrintF;
1277 Optimizations["printf"] = &PrintF;
1278 Optimizations["fwrite"] = &FWrite;
1279 Optimizations["fputs"] = &FPuts;
1280 Optimizations["fprintf"] = &FPrintF;
1281}
1282
1283
1284/// runOnFunction - Top level algorithm.
1285///
1286bool SimplifyLibCalls::runOnFunction(Function &F) {
1287 if (Optimizations.empty())
1288 InitOptimizations();
1289
1290 const TargetData &TD = getAnalysis<TargetData>();
1291
1292 IRBuilder Builder;
1293
1294 bool Changed = false;
1295 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1296 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1297 // Ignore non-calls.
1298 CallInst *CI = dyn_cast<CallInst>(I++);
1299 if (!CI) continue;
1300
1301 // Ignore indirect calls and calls to non-external functions.
1302 Function *Callee = CI->getCalledFunction();
1303 if (Callee == 0 || !Callee->isDeclaration() ||
1304 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1305 continue;
1306
1307 // Ignore unknown calls.
1308 const char *CalleeName = Callee->getNameStart();
1309 StringMap<LibCallOptimization*>::iterator OMI =
1310 Optimizations.find(CalleeName, CalleeName+Callee->getNameLen());
1311 if (OMI == Optimizations.end()) continue;
1312
1313 // Set the builder to the instruction after the call.
1314 Builder.SetInsertPoint(BB, I);
1315
1316 // Try to optimize this call.
1317 Value *Result = OMI->second->OptimizeCall(CI, TD, Builder);
1318 if (Result == 0) continue;
1319
1320 // Something changed!
1321 Changed = true;
1322 ++NumSimplified;
1323
1324 // Inspect the instruction after the call (which was potentially just
1325 // added) next.
1326 I = CI; ++I;
1327
1328 if (CI != Result && !CI->use_empty()) {
1329 CI->replaceAllUsesWith(Result);
1330 if (!Result->hasName())
1331 Result->takeName(CI);
1332 }
1333 CI->eraseFromParent();
1334 }
1335 }
1336 return Changed;
1337}
1338
1339
1340// TODO:
1341// Additional cases that we need to add to this file:
1342//
1343// cbrt:
1344// * cbrt(expN(X)) -> expN(x/3)
1345// * cbrt(sqrt(x)) -> pow(x,1/6)
1346// * cbrt(sqrt(x)) -> pow(x,1/9)
1347//
1348// cos, cosf, cosl:
1349// * cos(-x) -> cos(x)
1350//
1351// exp, expf, expl:
1352// * exp(log(x)) -> x
1353//
1354// log, logf, logl:
1355// * log(exp(x)) -> x
1356// * log(x**y) -> y*log(x)
1357// * log(exp(y)) -> y*log(e)
1358// * log(exp2(y)) -> y*log(2)
1359// * log(exp10(y)) -> y*log(10)
1360// * log(sqrt(x)) -> 0.5*log(x)
1361// * log(pow(x,y)) -> y*log(x)
1362//
1363// lround, lroundf, lroundl:
1364// * lround(cnst) -> cnst'
1365//
1366// memcmp:
1367// * memcmp(x,y,l) -> cnst
1368// (if all arguments are constant and strlen(x) <= l and strlen(y) <= l)
1369//
1370// memmove:
1371// * memmove(d,s,l,a) -> memcpy(d,s,l,a)
1372// (if s is a global constant array)
1373//
1374// pow, powf, powl:
1375// * pow(exp(x),y) -> exp(x*y)
1376// * pow(sqrt(x),y) -> pow(x,y*0.5)
1377// * pow(pow(x,y),z)-> pow(x,y*z)
1378//
1379// puts:
1380// * puts("") -> putchar("\n")
1381//
1382// round, roundf, roundl:
1383// * round(cnst) -> cnst'
1384//
1385// signbit:
1386// * signbit(cnst) -> cnst'
1387// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
1388//
1389// sqrt, sqrtf, sqrtl:
1390// * sqrt(expN(x)) -> expN(x*0.5)
1391// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
1392// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
1393//
1394// stpcpy:
1395// * stpcpy(str, "literal") ->
1396// llvm.memcpy(str,"literal",strlen("literal")+1,1)
1397// strrchr:
1398// * strrchr(s,c) -> reverse_offset_of_in(c,s)
1399// (if c is a constant integer and s is a constant string)
1400// * strrchr(s1,0) -> strchr(s1,0)
1401//
1402// strncat:
1403// * strncat(x,y,0) -> x
1404// * strncat(x,y,0) -> x (if strlen(y) = 0)
1405// * strncat(x,y,l) -> strcat(x,y) (if y and l are constants an l > strlen(y))
1406//
1407// strncpy:
1408// * strncpy(d,s,0) -> d
1409// * strncpy(d,s,l) -> memcpy(d,s,l,1)
1410// (if s and l are constants)
1411//
1412// strpbrk:
1413// * strpbrk(s,a) -> offset_in_for(s,a)
1414// (if s and a are both constant strings)
1415// * strpbrk(s,"") -> 0
1416// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
1417//
1418// strspn, strcspn:
1419// * strspn(s,a) -> const_int (if both args are constant)
1420// * strspn("",a) -> 0
1421// * strspn(s,"") -> 0
1422// * strcspn(s,a) -> const_int (if both args are constant)
1423// * strcspn("",a) -> 0
1424// * strcspn(s,"") -> strlen(a)
1425//
1426// strstr:
1427// * strstr(x,x) -> x
1428// * strstr(s1,s2) -> offset_of_s2_in(s1)
1429// (if s1 and s2 are constant strings)
1430//
1431// tan, tanf, tanl:
1432// * tan(atan(x)) -> x
1433//
1434// trunc, truncf, truncl:
1435// * trunc(cnst) -> cnst'
1436//
1437//