From: Evan Cheng Date: Fri, 16 Jun 2006 04:52:30 +0000 (+0000) Subject: Simplify fprintf(file, "%s", str) to fputs(str, file). X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=952895243e79f57f1e5c2789ffacca8c8d3e4f02;p=oota-llvm.git Simplify fprintf(file, "%s", str) to fputs(str, file). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28814 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/IPO/SimplifyLibCalls.cpp b/lib/Transforms/IPO/SimplifyLibCalls.cpp index fefb100c37f..c16cdef13e2 100644 --- a/lib/Transforms/IPO/SimplifyLibCalls.cpp +++ b/lib/Transforms/IPO/SimplifyLibCalls.cpp @@ -229,6 +229,15 @@ public: return fputc_func; } + /// @brief Return a Function* for the fputs libcall + Function* get_fputs(const Type* FILEptr_type) { + if (!fputs_func) + fputs_func = M->getOrInsertFunction("fputs", Type::IntTy, + PointerType::get(Type::SByteTy), + FILEptr_type, NULL); + return fputs_func; + } + /// @brief Return a Function* for the fwrite libcall Function* get_fwrite(const Type* FILEptr_type) { if (!fwrite_func) @@ -310,6 +319,7 @@ private: M = &mod; TD = &getAnalysis(); fputc_func = 0; + fputs_func = 0; fwrite_func = 0; memcpy_func = 0; memchr_func = 0; @@ -325,7 +335,7 @@ private: private: /// Caches for function pointers. - Function *fputc_func, *fwrite_func; + Function *fputc_func, *fputs_func, *fwrite_func; Function *memcpy_func, *memchr_func; Function* sqrt_func; Function *strcpy_func, *strlen_func; @@ -1340,21 +1350,31 @@ public: { uint64_t len = 0; ConstantArray* CA = 0; - if (!getConstantStringLength(ci->getOperand(3), len, &CA)) - return false; - - // fprintf(file,"%s",str) -> fwrite(fmt,strlen(fmt),1,file) - const Type* FILEptr_type = ci->getOperand(1)->getType(); - Function* fwrite_func = SLC.get_fwrite(FILEptr_type); - if (!fwrite_func) - return false; - std::vector args; - args.push_back(CastToCStr(ci->getOperand(3), *ci)); - args.push_back(ConstantUInt::get(SLC.getIntPtrType(),len)); - args.push_back(ConstantUInt::get(SLC.getIntPtrType(),1)); - args.push_back(ci->getOperand(1)); - new CallInst(fwrite_func,args,ci->getName(),ci); - ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len)); + if (getConstantStringLength(ci->getOperand(3), len, &CA)) { + // fprintf(file,"%s",str) -> fwrite(str,strlen(str),1,file) + const Type* FILEptr_type = ci->getOperand(1)->getType(); + Function* fwrite_func = SLC.get_fwrite(FILEptr_type); + if (!fwrite_func) + return false; + std::vector args; + args.push_back(CastToCStr(ci->getOperand(3), *ci)); + args.push_back(ConstantUInt::get(SLC.getIntPtrType(),len)); + args.push_back(ConstantUInt::get(SLC.getIntPtrType(),1)); + args.push_back(ci->getOperand(1)); + new CallInst(fwrite_func,args,ci->getName(),ci); + ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len)); + } else { + // fprintf(file,"%s",str) -> fputs(str,file) + const Type* FILEptr_type = ci->getOperand(1)->getType(); + Function* fputs_func = SLC.get_fputs(FILEptr_type); + if (!fputs_func) + return false; + std::vector args; + args.push_back(ci->getOperand(3)); + args.push_back(ci->getOperand(1)); + new CallInst(fputs_func,args,ci->getName(),ci); + ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len)); + } break; } case 'c':