From 56b4f2bdf6072651d44d8e48a8dce14101e70b0b Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 1 May 2008 06:39:12 +0000 Subject: [PATCH] 1) add '-debug' output 2) Return NULL instead of false in several places for tidiness. 3) fix a bug optimizing sprintf(p, "%c", x) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50521 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/SimplifyLibCalls.cpp | 30 ++++++++++++++-------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/Transforms/Scalar/SimplifyLibCalls.cpp b/lib/Transforms/Scalar/SimplifyLibCalls.cpp index a03bc7e9cf3..e9c089dbd07 100644 --- a/lib/Transforms/Scalar/SimplifyLibCalls.cpp +++ b/lib/Transforms/Scalar/SimplifyLibCalls.cpp @@ -28,6 +28,7 @@ #include "llvm/ADT/StringMap.h" #include "llvm/ADT/Statistic.h" #include "llvm/Support/Compiler.h" +#include "llvm/Support/Debug.h" #include "llvm/Config/config.h" using namespace llvm; @@ -509,7 +510,7 @@ struct VISIBILITY_HIDDEN StrCatOpt : public LibCallOptimization { // See if we can get the length of the input string. uint64_t Len = GetStringLength(Src); - if (Len == 0) return false; + if (Len == 0) return 0; --Len; // Unbias length. // Handle the simple, do-nothing case: strcat(x, "") -> x @@ -562,7 +563,7 @@ struct VISIBILITY_HIDDEN StrChrOpt : public LibCallOptimization { // a string literal. If so, we can constant fold. std::string Str; if (!GetConstantStringInfo(SrcStr, Str)) - return false; + return 0; // strchr can find the nul character. Str += '\0'; @@ -682,7 +683,7 @@ struct VISIBILITY_HIDDEN StrCpyOpt : public LibCallOptimization { // See if we can get the length of the input string. uint64_t Len = GetStringLength(Src); - if (Len == 0) return false; + if (Len == 0) return 0; // We have enough information to now generate the memcpy call to do the // concatenation for us. Make a memcpy to copy the nul byte with align = 1. @@ -737,7 +738,7 @@ struct VISIBILITY_HIDDEN MemCmpOpt : public LibCallOptimization { // Make sure we have a constant length. ConstantInt *LenC = dyn_cast(CI->getOperand(3)); - if (!LenC) return false; + if (!LenC) return 0; uint64_t Len = LenC->getZExtValue(); if (Len == 0) // memcmp(s1,s2,0) -> 0 @@ -969,7 +970,7 @@ struct VISIBILITY_HIDDEN PrintFOpt : public LibCallOptimization { // Check for a fixed format string. std::string FormatStr; if (!GetConstantStringInfo(CI->getOperand(1), FormatStr)) - return false; + return 0; // Empty format string -> noop. if (FormatStr.empty()) // Tolerate printf's declared void. @@ -1029,7 +1030,7 @@ struct VISIBILITY_HIDDEN SPrintFOpt : public LibCallOptimization { // Check for a fixed format string. std::string FormatStr; if (!GetConstantStringInfo(CI->getOperand(2), FormatStr)) - return false; + return 0; // If we just have a format string (nothing else crazy) transform it. if (CI->getNumOperands() == 3) { @@ -1052,10 +1053,14 @@ struct VISIBILITY_HIDDEN SPrintFOpt : public LibCallOptimization { // Decode the second character of the format string. if (FormatStr[1] == 'c') { - // sprintf(dst, "%c", chr) --> *(i8*)dst = chr + // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0 if (!isa(CI->getOperand(3)->getType())) return 0; Value *V = B.CreateTrunc(CI->getOperand(3), Type::Int8Ty, "char"); - B.CreateStore(V, CastToCStr(CI->getOperand(1), B)); + Value *Ptr = CastToCStr(CI->getOperand(1), B); + B.CreateStore(V, Ptr); + Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::Int32Ty, 1), "nul"); + B.CreateStore(Constant::getNullValue(Type::Int8Ty), Ptr); + return ConstantInt::get(CI->getType(), 1); } @@ -1124,7 +1129,7 @@ struct VISIBILITY_HIDDEN FPutsOpt : public LibCallOptimization { // fputs(s,F) --> fwrite(s,1,strlen(s),F) uint64_t Len = GetStringLength(CI->getOperand(1)); - if (!Len) return false; + if (!Len) return 0; EmitFWrite(CI->getOperand(1), ConstantInt::get(TD->getIntPtrType(), Len-1), CI->getOperand(2), B); return CI; // Known to have no uses (see above). @@ -1146,13 +1151,13 @@ struct VISIBILITY_HIDDEN FPrintFOpt : public LibCallOptimization { // All the optimizations depend on the format string. std::string FormatStr; if (!GetConstantStringInfo(CI->getOperand(2), FormatStr)) - return false; + return 0; // fprintf(F, "foo") --> fwrite("foo", 3, 1, F) if (CI->getNumOperands() == 3) { for (unsigned i = 0, e = FormatStr.size(); i != e; ++i) if (FormatStr[i] == '%') // Could handle %% -> % if we cared. - return false; // We found a format specifier. + return 0; // We found a format specifier. EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(), FormatStr.size()), @@ -1317,6 +1322,9 @@ bool SimplifyLibCalls::runOnFunction(Function &F) { Value *Result = OMI->second->OptimizeCall(CI, TD, Builder); if (Result == 0) continue; + DEBUG(DOUT << "SimplifyLibCalls simplified: " << *CI; + DOUT << " into: " << *Result << "\n"); + // Something changed! Changed = true; ++NumSimplified; -- 2.34.1