diff options
| author | Anders Carlsson <andersca@mac.com> | 2010-11-30 06:19:18 +0000 | 
|---|---|---|
| committer | Anders Carlsson <andersca@mac.com> | 2010-11-30 06:19:18 +0000 | 
| commit | e3ea1cba791e269c5b78072ad28f69d02c703973 (patch) | |
| tree | 634e39539d354fa495c456cecc7131d63f710299 /llvm/lib/Transforms | |
| parent | 7ac52b921b4c5c220dc863f70285ecfd5b91e0f8 (diff) | |
| download | bcm5719-llvm-e3ea1cba791e269c5b78072ad28f69d02c703973.tar.gz bcm5719-llvm-e3ea1cba791e269c5b78072ad28f69d02c703973.zip | |
Add a puts optimization that converts puts() to putchar('\n').
llvm-svn: 120398
Diffstat (limited to 'llvm/lib/Transforms')
| -rw-r--r-- | llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp | 33 | 
1 files changed, 30 insertions, 3 deletions
| diff --git a/llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp index 5b22b23b6b0..43c6f6dda25 100644 --- a/llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp @@ -1345,6 +1345,34 @@ struct FPrintFOpt : public LibCallOptimization {    }  }; +//===---------------------------------------===// +// 'puts' Optimizations + +struct PutsOpt : public LibCallOptimization { +  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { +    // Require one fixed pointer argument and an integer/void result. +    const FunctionType *FT = Callee->getFunctionType(); +    if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() || +        !(FT->getReturnType()->isIntegerTy() || +          FT->getReturnType()->isVoidTy())) +      return 0; + +    // Check for a constant string. +    std::string Str; +    if (!GetConstantStringInfo(CI->getArgOperand(0), Str)) +      return 0; + +    if (Str.empty()) { +      // puts("") -> putchar('\n') +      Value *Res = EmitPutChar(B.getInt32('\n'), B, TD); +      if (CI->use_empty()) return CI; +      return B.CreateIntCast(Res, CI->getType(), true); +    } + +    return 0; +  } +}; +  } // end anonymous namespace.  //===----------------------------------------------------------------------===// @@ -1370,6 +1398,7 @@ namespace {      // Formatting and IO Optimizations      SPrintFOpt SPrintF; PrintFOpt PrintF;      FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF; +    PutsOpt Puts;      bool Modified;  // This is only used by doInitialization.    public: @@ -1484,6 +1513,7 @@ void SimplifyLibCalls::InitOptimizations() {    Optimizations["fwrite"] = &FWrite;    Optimizations["fputs"] = &FPuts;    Optimizations["fprintf"] = &FPrintF; +  Optimizations["puts"] = &Puts;  } @@ -2298,9 +2328,6 @@ bool SimplifyLibCalls::doInitialization(Module &M) {  //   * pow(sqrt(x),y) -> pow(x,y*0.5)  //   * pow(pow(x,y),z)-> pow(x,y*z)  // -// puts: -//   * puts("") -> putchar('\n') -//  // round, roundf, roundl:  //   * round(cnst) -> cnst'  // | 

