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