OSDN Git Service

Optimize fprintf -> iprintf if there are no floating point arguments
authorRichard Osborne <richard@xmos.com>
Thu, 3 Mar 2011 14:20:22 +0000 (14:20 +0000)
committerRichard Osborne <richard@xmos.com>
Thu, 3 Mar 2011 14:20:22 +0000 (14:20 +0000)
and siprintf is available on the target.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126940 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Target/TargetLibraryInfo.h
lib/Target/TargetLibraryInfo.cpp
lib/Transforms/Scalar/SimplifyLibCalls.cpp
test/Transforms/SimplifyLibCalls/iprintf.ll

index 8474fc5..1847a37 100644 (file)
@@ -32,6 +32,9 @@ namespace llvm {
       /// int siprintf(char *str, const char *format, ...);
       siprintf,
       
+      /// int fiprintf(FILE *stream, const char *format, ...);
+      fiprintf,
+      
       NumLibFuncs
     };
   }
index 345d914..90ea343 100644 (file)
@@ -35,6 +35,7 @@ static void initialize(TargetLibraryInfo &TLI, const Triple &T) {
   if (T.getArch() != Triple::xcore) {
     TLI.setUnavailable(LibFunc::iprintf);
     TLI.setUnavailable(LibFunc::siprintf);
+    TLI.setUnavailable(LibFunc::fiprintf);
   }
 }
 
index 60f235c..0c08c5e 100644 (file)
@@ -1330,14 +1330,8 @@ struct FPutsOpt : public LibCallOptimization {
 // 'fprintf' Optimizations
 
 struct FPrintFOpt : public LibCallOptimization {
-  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
-    // Require two fixed paramters as pointers and integer result.
-    const FunctionType *FT = Callee->getFunctionType();
-    if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
-        !FT->getParamType(1)->isPointerTy() ||
-        !FT->getReturnType()->isIntegerTy())
-      return 0;
-
+  Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
+                                   IRBuilder<> &B) {
     // All the optimizations depend on the format string.
     std::string FormatStr;
     if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
@@ -1382,6 +1376,32 @@ struct FPrintFOpt : public LibCallOptimization {
     }
     return 0;
   }
+
+  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
+    // Require two fixed paramters as pointers and integer result.
+    const FunctionType *FT = Callee->getFunctionType();
+    if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
+        !FT->getParamType(1)->isPointerTy() ||
+        !FT->getReturnType()->isIntegerTy())
+      return 0;
+
+    if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
+      return V;
+    }
+
+    // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
+    // floating point arguments.
+    if (TLI->has(LibFunc::fiprintf) && !CallHasFloatingPointArgument(CI)) {
+      Module *M = B.GetInsertBlock()->getParent()->getParent();
+      Constant *FIPrintFFn =
+        M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
+      CallInst *New = cast<CallInst>(CI->clone());
+      New->setCalledFunction(FIPrintFFn);
+      B.Insert(New);
+      return New;
+    }
+    return 0;
+  }
 };
 
 //===---------------------------------------===//
index b99de6e..7f036fe 100644 (file)
@@ -46,5 +46,26 @@ entry:
        ret i32 %0
 }
 
+; Verify fprintf with no floating point arguments is transformed to fiprintf
+define i32 @f4(i8* %p, i32 %x) nounwind {
+entry:
+; CHECK: define i32 @f4
+; CHECK: @fiprintf
+; CHECK: }
+       %0 = tail call i32 (i8*, i8*, ...)* @fprintf(i8 *%p, i8* getelementptr ([4 x i8]* @.str1, i32 0, i32 0), i32 %x)
+       ret i32 %0
+}
+
+; Verify we don't turn this into an fiprintf call
+define i32 @f5(i8* %p, double %x) nounwind {
+entry:
+; CHECK: define i32 @f5
+; CHECK: @fprintf
+; CHECK: }
+       %0 = tail call i32 (i8*, i8*, ...)* @fprintf(i8 *%p, i8* getelementptr ([4 x i8]* @.str, i32 0, i32 0), double %x)
+       ret i32 %0
+}
+
 declare i32 @printf(i8* nocapture, ...) nounwind
 declare i32 @sprintf(i8* nocapture, i8* nocapture, ...) nounwind
+declare i32 @fprintf(i8* nocapture, i8* nocapture, ...) nounwind