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