diff options
-rw-r--r-- | lib/Transforms/Scalar/SimplifyLibCalls.cpp | 94 | ||||
-rw-r--r-- | lib/Transforms/Utils/SimplifyLibCalls.cpp | 77 | ||||
-rw-r--r-- | test/Transforms/InstCombine/fprintf-1.ll | 79 | ||||
-rw-r--r-- | test/Transforms/InstCombine/osx-names.ll (renamed from test/Transforms/SimplifyLibCalls/osx-names.ll) | 2 | ||||
-rw-r--r-- | test/Transforms/SimplifyLibCalls/FPrintF.ll | 29 | ||||
-rw-r--r-- | test/Transforms/SimplifyLibCalls/iprintf.ll | 29 |
6 files changed, 158 insertions, 152 deletions
diff --git a/lib/Transforms/Scalar/SimplifyLibCalls.cpp b/lib/Transforms/Scalar/SimplifyLibCalls.cpp index c4e8a87..61b5725 100644 --- a/lib/Transforms/Scalar/SimplifyLibCalls.cpp +++ b/lib/Transforms/Scalar/SimplifyLibCalls.cpp @@ -81,19 +81,6 @@ public: } // End anonymous namespace. -//===----------------------------------------------------------------------===// -// Helper Functions -//===----------------------------------------------------------------------===// - -static bool CallHasFloatingPointArgument(const CallInst *CI) { - for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end(); - it != e; ++it) { - if ((*it)->getType()->isFloatingPointTy()) - return true; - } - return false; -} - namespace { //===----------------------------------------------------------------------===// // Formatting and IO Optimizations @@ -161,84 +148,6 @@ struct FPutsOpt : public LibCallOptimization { }; //===---------------------------------------===// -// 'fprintf' Optimizations - -struct FPrintFOpt : public LibCallOptimization { - Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI, - IRBuilder<> &B) { - // All the optimizations depend on the format string. - StringRef FormatStr; - if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr)) - return 0; - - // fprintf(F, "foo") --> fwrite("foo", 3, 1, F) - if (CI->getNumArgOperands() == 2) { - for (unsigned i = 0, e = FormatStr.size(); i != e; ++i) - if (FormatStr[i] == '%') // Could handle %% -> % if we cared. - return 0; // We found a format specifier. - - // These optimizations require DataLayout. - if (!TD) return 0; - - Value *NewCI = EmitFWrite(CI->getArgOperand(1), - ConstantInt::get(TD->getIntPtrType(*Context), - FormatStr.size()), - CI->getArgOperand(0), B, TD, TLI); - return NewCI ? ConstantInt::get(CI->getType(), FormatStr.size()) : 0; - } - - // The remaining optimizations require the format string to be "%s" or "%c" - // and have an extra operand. - if (FormatStr.size() != 2 || FormatStr[0] != '%' || - CI->getNumArgOperands() < 3) - return 0; - - // Decode the second character of the format string. - if (FormatStr[1] == 'c') { - // fprintf(F, "%c", chr) --> fputc(chr, F) - if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0; - Value *NewCI = EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, - TD, TLI); - return NewCI ? ConstantInt::get(CI->getType(), 1) : 0; - } - - if (FormatStr[1] == 's') { - // fprintf(F, "%s", str) --> fputs(str, F) - if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty()) - return 0; - return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD, TLI); - } - return 0; - } - - virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { - // Require two fixed paramters as pointers and integer result. - 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; - } -}; - -//===---------------------------------------===// // 'puts' Optimizations struct PutsOpt : public LibCallOptimization { @@ -280,7 +189,7 @@ namespace { StringMap<LibCallOptimization*> Optimizations; // Formatting and IO Optimizations - FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF; + FWriteOpt FWrite; FPutsOpt FPuts; PutsOpt Puts; bool Modified; // This is only used by doInitialization. @@ -339,7 +248,6 @@ void SimplifyLibCalls::InitOptimizations() { // Formatting and IO Optimizations AddOpt(LibFunc::fwrite, &FWrite); AddOpt(LibFunc::fputs, &FPuts); - Optimizations["fprintf"] = &FPrintF; Optimizations["puts"] = &Puts; } diff --git a/lib/Transforms/Utils/SimplifyLibCalls.cpp b/lib/Transforms/Utils/SimplifyLibCalls.cpp index 96f09d2..5d217bb 100644 --- a/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -1502,6 +1502,81 @@ struct SPrintFOpt : public LibCallOptimization { } }; +struct FPrintFOpt : public LibCallOptimization { + Value *optimizeFixedFormatString(Function *Callee, CallInst *CI, + IRBuilder<> &B) { + // All the optimizations depend on the format string. + StringRef FormatStr; + if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr)) + return 0; + + // fprintf(F, "foo") --> fwrite("foo", 3, 1, F) + if (CI->getNumArgOperands() == 2) { + for (unsigned i = 0, e = FormatStr.size(); i != e; ++i) + if (FormatStr[i] == '%') // Could handle %% -> % if we cared. + return 0; // We found a format specifier. + + // These optimizations require DataLayout. + if (!TD) return 0; + + Value *NewCI = EmitFWrite(CI->getArgOperand(1), + ConstantInt::get(TD->getIntPtrType(*Context), + FormatStr.size()), + CI->getArgOperand(0), B, TD, TLI); + return NewCI ? ConstantInt::get(CI->getType(), FormatStr.size()) : 0; + } + + // The remaining optimizations require the format string to be "%s" or "%c" + // and have an extra operand. + if (FormatStr.size() != 2 || FormatStr[0] != '%' || + CI->getNumArgOperands() < 3) + return 0; + + // Decode the second character of the format string. + if (FormatStr[1] == 'c') { + // fprintf(F, "%c", chr) --> fputc(chr, F) + if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0; + Value *NewCI = EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, + TD, TLI); + return NewCI ? ConstantInt::get(CI->getType(), 1) : 0; + } + + if (FormatStr[1] == 's') { + // fprintf(F, "%s", str) --> fputs(str, F) + if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty()) + return 0; + return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD, TLI); + } + return 0; + } + + virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { + // Require two fixed paramters as pointers and integer result. + 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; + } +}; + } // End anonymous namespace. namespace llvm { @@ -1558,6 +1633,7 @@ class LibCallSimplifierImpl { // Formatting and IO library call optimizations. PrintFOpt PrintF; SPrintFOpt SPrintF; + FPrintFOpt FPrintF; void initOptimizations(); void addOpt(LibFunc::Func F, LibCallOptimization* Opt); @@ -1683,6 +1759,7 @@ void LibCallSimplifierImpl::initOptimizations() { // Formatting and IO library call optimizations. addOpt(LibFunc::printf, &PrintF); addOpt(LibFunc::sprintf, &SPrintF); + addOpt(LibFunc::fprintf, &FPrintF); } Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) { diff --git a/test/Transforms/InstCombine/fprintf-1.ll b/test/Transforms/InstCombine/fprintf-1.ll new file mode 100644 index 0000000..97cd770 --- /dev/null +++ b/test/Transforms/InstCombine/fprintf-1.ll @@ -0,0 +1,79 @@ +; Test that the fprintf library call simplifier works correctly. +; +; RUN: opt < %s -instcombine -S | FileCheck %s +; RUN: opt < %s -mtriple xcore-xmos-elf -instcombine -S | FileCheck %s -check-prefix=IPRINTF + +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128" + +%FILE = type { } + +@hello_world = constant [13 x i8] c"hello world\0A\00" +@percent_c = constant [3 x i8] c"%c\00" +@percent_d = constant [3 x i8] c"%d\00" +@percent_f = constant [3 x i8] c"%f\00" +@percent_s = constant [3 x i8] c"%s\00" + +declare i32 @fprintf(%FILE*, i8*, ...) + +; Check fprintf(fp, "foo") -> fwrite("foo", 3, 1, fp). + +define void @test_simplify1(%FILE* %fp) { +; CHECK: @test_simplify1 + %fmt = getelementptr [13 x i8]* @hello_world, i32 0, i32 0 + call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt) +; CHECK-NEXT: call i32 @fwrite(i8* getelementptr inbounds ([13 x i8]* @hello_world, i32 0, i32 0), i32 12, i32 1, %FILE* %fp) + ret void +; CHECK-NEXT: ret void +} + +; Check fprintf(fp, "%c", chr) -> fputc(chr, fp). + +define void @test_simplify2(%FILE* %fp) { +; CHECK: @test_simplify2 + %fmt = getelementptr [3 x i8]* @percent_c, i32 0, i32 0 + call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, i8 104) +; CHECK-NEXT: call i32 @fputc(i32 104, %FILE* %fp) + ret void +; CHECK-NEXT: ret void +} + +; Check fprintf(fp, "%s", str) -> fputs(str, fp). + +define void @test_simplify3(%FILE* %fp) { +; CHECK: @test_simplify3 + %fmt = getelementptr [3 x i8]* @percent_s, i32 0, i32 0 + %str = getelementptr [13 x i8]* @hello_world, i32 0, i32 0 + call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, i8* %str) +; CHECK-NEXT: call i32 @fputs(i8* getelementptr inbounds ([13 x i8]* @hello_world, i32 0, i32 0), %FILE* %fp) + ret void +; CHECK-NEXT: ret void +} + +; Check fprintf(fp, fmt, ...) -> fiprintf(fp, fmt, ...) if no floating point. + +define void @test_simplify4(%FILE* %fp) { +; CHECK-IPRINTF: @test_simplify4 + %fmt = getelementptr [3 x i8]* @percent_d, i32 0, i32 0 + call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, i32 187) +; CHECK-NEXT-IPRINTF: call i32 (%FILE*, i8*, ...)* @fiprintf(%FILE* %fp, i8* getelementptr inbounds ([3 x i8]* @percent_d, i32 0, i32 0), i32 187) + ret void +; CHECK-NEXT-IPRINTF: ret void +} + +define void @test_no_simplify1(%FILE* %fp) { +; CHECK-IPRINTF: @test_no_simplify1 + %fmt = getelementptr [3 x i8]* @percent_f, i32 0, i32 0 + call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, double 1.87) +; CHECK-NEXT-IPRINTF: call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* getelementptr inbounds ([3 x i8]* @percent_f, i32 0, i32 0), double 1.870000e+00) + ret void +; CHECK-NEXT-IPRINTF: ret void +} + +define void @test_no_simplify2(%FILE* %fp, double %d) { +; CHECK: @test_no_simplify2 + %fmt = getelementptr [3 x i8]* @percent_f, i32 0, i32 0 + call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, double %d) +; CHECK-NEXT: call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* getelementptr inbounds ([3 x i8]* @percent_f, i32 0, i32 0), double %d) + ret void +; CHECK-NEXT: ret void +} diff --git a/test/Transforms/SimplifyLibCalls/osx-names.ll b/test/Transforms/InstCombine/osx-names.ll index e321d1d..7b83526 100644 --- a/test/Transforms/SimplifyLibCalls/osx-names.ll +++ b/test/Transforms/InstCombine/osx-names.ll @@ -1,4 +1,4 @@ -; RUN: opt < %s -simplify-libcalls -S | FileCheck %s +; RUN: opt < %s -instcombine -S | FileCheck %s ; <rdar://problem/9815881> ; On OSX x86-32, fwrite and fputs aren't called fwrite and fputs. ; Make sure we use the correct names. diff --git a/test/Transforms/SimplifyLibCalls/FPrintF.ll b/test/Transforms/SimplifyLibCalls/FPrintF.ll deleted file mode 100644 index d1410a9..0000000 --- a/test/Transforms/SimplifyLibCalls/FPrintF.ll +++ /dev/null @@ -1,29 +0,0 @@ -; Test that the FPrintFOptimizer works correctly -; RUN: opt < %s -simplify-libcalls -S | FileCheck %s - -; This transformation requires the pointer size, as it assumes that size_t is -; the size of a pointer. -target datalayout = "p:64:64:64" - - %struct._IO_FILE = type { i32, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, %struct._IO_marker*, %struct._IO_FILE*, i32, i32, i32, i16, i8, [1 x i8], i8*, i64, i8*, i8*, i32, [52 x i8] } - %struct._IO_marker = type { %struct._IO_marker*, %struct._IO_FILE*, i32 } -@str = constant [3 x i8] c"%s\00" ; <[3 x i8]*> [#uses=1] -@chr = constant [3 x i8] c"%c\00" ; <[3 x i8]*> [#uses=1] -@hello = constant [13 x i8] c"hello world\0A\00" ; <[13 x i8]*> [#uses=1] -@stdout = external global %struct._IO_FILE* ; <%struct._IO_FILE**> [#uses=3] - -declare i32 @fprintf(%struct._IO_FILE*, i8*, ...) - -; CHECK: define i32 @foo() { -define i32 @foo() { -entry: - %tmp.1 = load %struct._IO_FILE** @stdout ; <%struct._IO_FILE*> [#uses=1] - %tmp.0 = call i32 (%struct._IO_FILE*, i8*, ...)* @fprintf( %struct._IO_FILE* %tmp.1, i8* getelementptr ([13 x i8]* @hello, i32 0, i32 0) ) ; <i32> [#uses=0] - %tmp.4 = load %struct._IO_FILE** @stdout ; <%struct._IO_FILE*> [#uses=1] - %tmp.3 = call i32 (%struct._IO_FILE*, i8*, ...)* @fprintf( %struct._IO_FILE* %tmp.4, i8* getelementptr ([3 x i8]* @str, i32 0, i32 0), i8* getelementptr ([13 x i8]* @hello, i32 0, i32 0) ) ; <i32> [#uses=0] - %tmp.8 = load %struct._IO_FILE** @stdout ; <%struct._IO_FILE*> [#uses=1] - %tmp.7 = call i32 (%struct._IO_FILE*, i8*, ...)* @fprintf( %struct._IO_FILE* %tmp.8, i8* getelementptr ([3 x i8]* @chr, i32 0, i32 0), i32 33 ) ; <i32> [#uses=0] - ret i32 0 - -; CHECK-NOT: @fprintf( -} diff --git a/test/Transforms/SimplifyLibCalls/iprintf.ll b/test/Transforms/SimplifyLibCalls/iprintf.ll deleted file mode 100644 index 81c1d2f..0000000 --- a/test/Transforms/SimplifyLibCalls/iprintf.ll +++ /dev/null @@ -1,29 +0,0 @@ -; RUN: opt < %s -simplify-libcalls -S -o %t -; RUN: FileCheck < %t %s -target datalayout = "e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:64:64-v128:128:128-a0:0:32" -target triple = "xcore-xmos-elf" - -@.str = internal constant [4 x i8] c"%f\0A\00" ; <[4 x i8]*> [#uses=1] -@.str1 = internal constant [4 x i8] c"%d\0A\00" ; <[4 x i8]*> [#uses=1] - -; 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 @fprintf(i8* nocapture, i8* nocapture, ...) nounwind |