From: Meador Inge Date: Mon, 26 Nov 2012 20:37:20 +0000 (+0000) Subject: instcombine: Migrate printf optimizations X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=d7aa3231f75174810a3443e6c2e1993ddefc1dd2;p=oota-llvm.git instcombine: Migrate printf optimizations This patch migrates the printf optimizations from the simplify-libcalls pass into the instcombine library call simplifier. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168604 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/SimplifyLibCalls.cpp b/lib/Transforms/Scalar/SimplifyLibCalls.cpp index 4db15a62d52..0788f190143 100644 --- a/lib/Transforms/Scalar/SimplifyLibCalls.cpp +++ b/lib/Transforms/Scalar/SimplifyLibCalls.cpp @@ -99,93 +99,6 @@ namespace { // Formatting and IO Optimizations //===----------------------------------------------------------------------===// -//===---------------------------------------===// -// 'printf' Optimizations - -struct PrintFOpt : public LibCallOptimization { - Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI, - IRBuilder<> &B) { - // Check for a fixed format string. - StringRef FormatStr; - if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr)) - return 0; - - // Empty format string -> noop. - if (FormatStr.empty()) // Tolerate printf's declared void. - return CI->use_empty() ? (Value*)CI : - ConstantInt::get(CI->getType(), 0); - - // Do not do any of the following transformations if the printf return value - // is used, in general the printf return value is not compatible with either - // putchar() or puts(). - if (!CI->use_empty()) - return 0; - - // printf("x") -> putchar('x'), even for '%'. - if (FormatStr.size() == 1) { - Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD, TLI); - if (CI->use_empty() || !Res) return Res; - return B.CreateIntCast(Res, CI->getType(), true); - } - - // printf("foo\n") --> puts("foo") - if (FormatStr[FormatStr.size()-1] == '\n' && - FormatStr.find('%') == std::string::npos) { // no format characters. - // Create a string literal with no \n on it. We expect the constant merge - // pass to be run after this pass, to merge duplicate strings. - FormatStr = FormatStr.drop_back(); - Value *GV = B.CreateGlobalString(FormatStr, "str"); - Value *NewCI = EmitPutS(GV, B, TD, TLI); - return (CI->use_empty() || !NewCI) ? - NewCI : - ConstantInt::get(CI->getType(), FormatStr.size()+1); - } - - // Optimize specific format strings. - // printf("%c", chr) --> putchar(chr) - if (FormatStr == "%c" && CI->getNumArgOperands() > 1 && - CI->getArgOperand(1)->getType()->isIntegerTy()) { - Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD, TLI); - - if (CI->use_empty() || !Res) return Res; - return B.CreateIntCast(Res, CI->getType(), true); - } - - // printf("%s\n", str) --> puts(str) - if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 && - CI->getArgOperand(1)->getType()->isPointerTy()) { - return EmitPutS(CI->getArgOperand(1), B, TD, TLI); - } - return 0; - } - - virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { - // Require one fixed pointer argument and an integer/void result. - FunctionType *FT = Callee->getFunctionType(); - if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() || - !(FT->getReturnType()->isIntegerTy() || - FT->getReturnType()->isVoidTy())) - return 0; - - if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) { - return V; - } - - // printf(format, ...) -> iprintf(format, ...) if no floating point - // arguments. - if (TLI->has(LibFunc::iprintf) && !CallHasFloatingPointArgument(CI)) { - Module *M = B.GetInsertBlock()->getParent()->getParent(); - Constant *IPrintFFn = - M->getOrInsertFunction("iprintf", FT, Callee->getAttributes()); - CallInst *New = cast(CI->clone()); - New->setCalledFunction(IPrintFFn); - B.Insert(New); - return New; - } - return 0; - } -}; - //===---------------------------------------===// // 'sprintf' Optimizations @@ -463,7 +376,7 @@ namespace { StringMap Optimizations; // Formatting and IO Optimizations - SPrintFOpt SPrintF; PrintFOpt PrintF; + SPrintFOpt SPrintF; FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF; PutsOpt Puts; @@ -522,7 +435,6 @@ void SimplifyLibCalls::AddOpt(LibFunc::Func F1, LibFunc::Func F2, void SimplifyLibCalls::InitOptimizations() { // Formatting and IO Optimizations Optimizations["sprintf"] = &SPrintF; - Optimizations["printf"] = &PrintF; AddOpt(LibFunc::fwrite, &FWrite); AddOpt(LibFunc::fputs, &FPuts); Optimizations["fprintf"] = &FPrintF; diff --git a/lib/Transforms/Utils/SimplifyLibCalls.cpp b/lib/Transforms/Utils/SimplifyLibCalls.cpp index 27ccdc1095e..85de642b1ad 100644 --- a/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -102,6 +102,15 @@ static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) { return true; } +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; +} + //===----------------------------------------------------------------------===// // Fortified Library Call Optimizations //===----------------------------------------------------------------------===// @@ -1312,6 +1321,94 @@ struct ToAsciiOpt : public LibCallOptimization { } }; +//===----------------------------------------------------------------------===// +// Formatting and IO Library Call Optimizations +//===----------------------------------------------------------------------===// + +struct PrintFOpt : public LibCallOptimization { + Value *optimizeFixedFormatString(Function *Callee, CallInst *CI, + IRBuilder<> &B) { + // Check for a fixed format string. + StringRef FormatStr; + if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr)) + return 0; + + // Empty format string -> noop. + if (FormatStr.empty()) // Tolerate printf's declared void. + return CI->use_empty() ? (Value*)CI : + ConstantInt::get(CI->getType(), 0); + + // Do not do any of the following transformations if the printf return value + // is used, in general the printf return value is not compatible with either + // putchar() or puts(). + if (!CI->use_empty()) + return 0; + + // printf("x") -> putchar('x'), even for '%'. + if (FormatStr.size() == 1) { + Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD, TLI); + if (CI->use_empty() || !Res) return Res; + return B.CreateIntCast(Res, CI->getType(), true); + } + + // printf("foo\n") --> puts("foo") + if (FormatStr[FormatStr.size()-1] == '\n' && + FormatStr.find('%') == std::string::npos) { // no format characters. + // Create a string literal with no \n on it. We expect the constant merge + // pass to be run after this pass, to merge duplicate strings. + FormatStr = FormatStr.drop_back(); + Value *GV = B.CreateGlobalString(FormatStr, "str"); + Value *NewCI = EmitPutS(GV, B, TD, TLI); + return (CI->use_empty() || !NewCI) ? + NewCI : + ConstantInt::get(CI->getType(), FormatStr.size()+1); + } + + // Optimize specific format strings. + // printf("%c", chr) --> putchar(chr) + if (FormatStr == "%c" && CI->getNumArgOperands() > 1 && + CI->getArgOperand(1)->getType()->isIntegerTy()) { + Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD, TLI); + + if (CI->use_empty() || !Res) return Res; + return B.CreateIntCast(Res, CI->getType(), true); + } + + // printf("%s\n", str) --> puts(str) + if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 && + CI->getArgOperand(1)->getType()->isPointerTy()) { + return EmitPutS(CI->getArgOperand(1), B, TD, TLI); + } + return 0; + } + + virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { + // Require one fixed pointer argument and an integer/void result. + FunctionType *FT = Callee->getFunctionType(); + if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() || + !(FT->getReturnType()->isIntegerTy() || + FT->getReturnType()->isVoidTy())) + return 0; + + if (Value *V = optimizeFixedFormatString(Callee, CI, B)) { + return V; + } + + // printf(format, ...) -> iprintf(format, ...) if no floating point + // arguments. + if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) { + Module *M = B.GetInsertBlock()->getParent()->getParent(); + Constant *IPrintFFn = + M->getOrInsertFunction("iprintf", FT, Callee->getAttributes()); + CallInst *New = cast(CI->clone()); + New->setCalledFunction(IPrintFFn); + B.Insert(New); + return New; + } + return 0; + } +}; + } // End anonymous namespace. namespace llvm { @@ -1365,6 +1462,9 @@ class LibCallSimplifierImpl { IsAsciiOpt IsAscii; ToAsciiOpt ToAscii; + // Formatting and IO library call optimizations. + PrintFOpt PrintF; + void initOptimizations(); void addOpt(LibFunc::Func F, LibCallOptimization* Opt); void addOpt(LibFunc::Func F1, LibFunc::Func F2, LibCallOptimization* Opt); @@ -1485,6 +1585,9 @@ void LibCallSimplifierImpl::initOptimizations() { addOpt(LibFunc::isdigit, &IsDigit); addOpt(LibFunc::isascii, &IsAscii); addOpt(LibFunc::toascii, &ToAscii); + + // Formatting and IO library call optimizations. + addOpt(LibFunc::printf, &PrintF); } Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) { diff --git a/test/Transforms/InstCombine/debug-line.ll b/test/Transforms/InstCombine/debug-line.ll new file mode 100644 index 00000000000..084efdc989f --- /dev/null +++ b/test/Transforms/InstCombine/debug-line.ll @@ -0,0 +1,24 @@ +; RUN: opt -instcombine -S < %s | FileCheck %s + + +@.str = private constant [3 x i8] c"%c\00" + +define void @foo() nounwind ssp { +;CHECK: call i32 @putchar{{.+}} !dbg + %1 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([3 x i8]* @.str, i32 0, i32 0), i32 97), !dbg !5 + ret void, !dbg !7 +} + +declare i32 @printf(i8*, ...) + +!llvm.dbg.sp = !{!0} + +!0 = metadata !{i32 589870, i32 0, metadata !1, metadata !"foo", metadata !"foo", metadata !"", metadata !1, i32 4, metadata !3, i1 false, i1 true, i32 0, i32 0, null, i32 0, i1 false, void ()* @foo} ; [ DW_TAG_subprogram ] +!1 = metadata !{i32 589865, metadata !"m.c", metadata !"/private/tmp", metadata !2} ; [ DW_TAG_file_type ] +!2 = metadata !{i32 589841, i32 0, i32 12, metadata !"m.c", metadata !"/private/tmp", metadata !"clang", i1 true, i1 false, metadata !"", i32 0} ; [ DW_TAG_compile_unit ] +!3 = metadata !{i32 589845, metadata !1, metadata !"", metadata !1, i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !4, i32 0, null} ; [ DW_TAG_subroutine_type ] +!4 = metadata !{null} +!5 = metadata !{i32 5, i32 2, metadata !6, null} +!6 = metadata !{i32 589835, metadata !0, i32 4, i32 12, metadata !1, i32 0} ; [ DW_TAG_lexical_block ] +!7 = metadata !{i32 6, i32 1, metadata !6, null} + diff --git a/test/Transforms/InstCombine/printf-1.ll b/test/Transforms/InstCombine/printf-1.ll new file mode 100644 index 00000000000..3a910ea437b --- /dev/null +++ b/test/Transforms/InstCombine/printf-1.ll @@ -0,0 +1,119 @@ +; Test that the printf 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" + +@hello_world = constant [13 x i8] c"hello world\0A\00" +@h = constant [2 x i8] c"h\00" +@percent = constant [2 x i8] c"%\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 [4 x i8] c"%s\0A\00" +@empty = constant [1 x i8] c"\00" +; CHECK: [[STR:@[a-z0-9]+]] = private unnamed_addr constant [12 x i8] c"hello world\00" + +declare i32 @printf(i8*, ...) + +; Check printf("") -> noop. + +define void @test_simplify1() { +; CHECK: @test_simplify1 + %fmt = getelementptr [1 x i8]* @empty, i32 0, i32 0 + call i32 (i8*, ...)* @printf(i8* %fmt) + ret void +; CHECK-NEXT: ret void +} + +; Check printf("x") -> putchar('x'), even for '%'. + +define void @test_simplify2() { +; CHECK: @test_simplify2 + %fmt = getelementptr [2 x i8]* @h, i32 0, i32 0 + call i32 (i8*, ...)* @printf(i8* %fmt) +; CHECK-NEXT: call i32 @putchar(i32 104) + ret void +; CHECK-NEXT: ret void +} + +define void @test_simplify3() { +; CHECK: @test_simplify3 + %fmt = getelementptr [2 x i8]* @percent, i32 0, i32 0 + call i32 (i8*, ...)* @printf(i8* %fmt) +; CHECK-NEXT: call i32 @putchar(i32 37) + ret void +; CHECK-NEXT: ret void +} + +; Check printf("foo\n") -> puts("foo"). + +define void @test_simplify4() { +; CHECK: @test_simplify4 + %fmt = getelementptr [13 x i8]* @hello_world, i32 0, i32 0 + call i32 (i8*, ...)* @printf(i8* %fmt) +; CHECK-NEXT: call i32 @puts(i8* getelementptr inbounds ([12 x i8]* [[STR]], i32 0, i32 0)) + ret void +; CHECK-NEXT: ret void +} + +; Check printf("%c", chr) -> putchar(chr). + +define void @test_simplify5() { +; CHECK: @test_simplify5 + %fmt = getelementptr [3 x i8]* @percent_c, i32 0, i32 0 + call i32 (i8*, ...)* @printf(i8* %fmt, i8 104) +; CHECK-NEXT: call i32 @putchar(i32 104) + ret void +; CHECK-NEXT: ret void +} + +; Check printf("%s\n", str) -> puts(str). + +define void @test_simplify6() { +; CHECK: @test_simplify6 + %fmt = getelementptr [4 x i8]* @percent_s, i32 0, i32 0 + %str = getelementptr [13 x i8]* @hello_world, i32 0, i32 0 + call i32 (i8*, ...)* @printf(i8* %fmt, i8* %str) +; CHECK-NEXT: call i32 @puts(i8* getelementptr inbounds ([13 x i8]* @hello_world, i32 0, i32 0)) + ret void +; CHECK-NEXT: ret void +} + +; Check printf(format, ...) -> iprintf(format, ...) if no floating point. + +define void @test_simplify7() { +; CHECK-IPRINTF: @test_simplify7 + %fmt = getelementptr [3 x i8]* @percent_d, i32 0, i32 0 + call i32 (i8*, ...)* @printf(i8* %fmt, i32 187) +; CHECK-NEXT-IPRINTF: call i32 (i8*, ...)* @iprintf(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() { +; CHECK-IPRINTF: @test_no_simplify1 + %fmt = getelementptr [3 x i8]* @percent_f, i32 0, i32 0 + call i32 (i8*, ...)* @printf(i8* %fmt, double 1.87) +; CHECK-NEXT-IPRINTF: call i32 (i8*, ...)* @printf(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(i8* %fmt, double %d) { +; CHECK: @test_no_simplify2 + call i32 (i8*, ...)* @printf(i8* %fmt, double %d) +; CHECK-NEXT: call i32 (i8*, ...)* @printf(i8* %fmt, double %d) + ret void +; CHECK-NEXT: ret void +} + +define i32 @test_no_simplify3() { +; CHECK: @test_no_simplify3 + %fmt = getelementptr [2 x i8]* @h, i32 0, i32 0 + %ret = call i32 (i8*, ...)* @printf(i8* %fmt) +; CHECK-NEXT: call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([2 x i8]* @h, i32 0, i32 0)) + ret i32 %ret +; CHECK-NEXT: ret i32 %ret +} diff --git a/test/Transforms/SimplifyLibCalls/Printf.ll b/test/Transforms/SimplifyLibCalls/Printf.ll deleted file mode 100644 index 489c993f211..00000000000 --- a/test/Transforms/SimplifyLibCalls/Printf.ll +++ /dev/null @@ -1,37 +0,0 @@ -; RUN: opt < %s -simplify-libcalls -S | FileCheck %s - -@str = internal constant [13 x i8] c"hello world\0A\00" ; <[13 x i8]*> [#uses=1] -@str1 = internal constant [2 x i8] c"h\00" ; <[2 x i8]*> [#uses=1] - -; CHECK: private unnamed_addr constant [12 x i8] c"hello world\00" - -declare i32 @printf(i8*, ...) - -; CHECK: define void @f0 -; CHECK-NOT: printf -; CHECK: } -define void @f0() { -entry: - %tmp1 = tail call i32 (i8*, ...)* @printf( i8* getelementptr ([13 x i8]* @str, i32 0, i32 0) ) ; [#uses=0] - ret void -} - -; CHECK: define void @f1 -; CHECK-NOT: printf -; CHECK: } -define void @f1() { -entry: - %tmp1 = tail call i32 (i8*, ...)* @printf( i8* getelementptr ([2 x i8]* @str1, i32 0, i32 0) ) ; [#uses=0] - ret void -} - -; Verify that we don't turn this into a putchar call (thus changing the return -; value). -; -; CHECK: define i32 @f2 -; CHECK: printf -; CHECK: } -define i32 @f2() { - %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([2 x i8]* @str1, i32 0, i32 0)) - ret i32 %call -} diff --git a/test/Transforms/SimplifyLibCalls/debug-line.ll b/test/Transforms/SimplifyLibCalls/debug-line.ll deleted file mode 100644 index b668e4b9d34..00000000000 --- a/test/Transforms/SimplifyLibCalls/debug-line.ll +++ /dev/null @@ -1,24 +0,0 @@ -; RUN: opt -simplify-libcalls -S < %s | FileCheck %s - - -@.str = private constant [3 x i8] c"%c\00" - -define void @foo() nounwind ssp { -;CHECK: call i32 @putchar{{.+}} !dbg - %1 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([3 x i8]* @.str, i32 0, i32 0), i32 97), !dbg !5 - ret void, !dbg !7 -} - -declare i32 @printf(i8*, ...) - -!llvm.dbg.sp = !{!0} - -!0 = metadata !{i32 589870, i32 0, metadata !1, metadata !"foo", metadata !"foo", metadata !"", metadata !1, i32 4, metadata !3, i1 false, i1 true, i32 0, i32 0, null, i32 0, i1 false, void ()* @foo} ; [ DW_TAG_subprogram ] -!1 = metadata !{i32 589865, metadata !"m.c", metadata !"/private/tmp", metadata !2} ; [ DW_TAG_file_type ] -!2 = metadata !{i32 589841, i32 0, i32 12, metadata !"m.c", metadata !"/private/tmp", metadata !"clang", i1 true, i1 false, metadata !"", i32 0} ; [ DW_TAG_compile_unit ] -!3 = metadata !{i32 589845, metadata !1, metadata !"", metadata !1, i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !4, i32 0, null} ; [ DW_TAG_subroutine_type ] -!4 = metadata !{null} -!5 = metadata !{i32 5, i32 2, metadata !6, null} -!6 = metadata !{i32 589835, metadata !0, i32 4, i32 12, metadata !1, i32 0} ; [ DW_TAG_lexical_block ] -!7 = metadata !{i32 6, i32 1, metadata !6, null} - diff --git a/test/Transforms/SimplifyLibCalls/iprintf.ll b/test/Transforms/SimplifyLibCalls/iprintf.ll index 7f036fe3ab8..d6a7074db14 100644 --- a/test/Transforms/SimplifyLibCalls/iprintf.ll +++ b/test/Transforms/SimplifyLibCalls/iprintf.ll @@ -6,26 +6,6 @@ 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 printf with no floating point arguments is transformed to iprintf -define i32 @f0(i32 %x) nounwind { -entry: -; CHECK: define i32 @f0 -; CHECK: @iprintf -; CHECK: } - %0 = tail call i32 (i8*, ...)* @printf(i8* getelementptr ([4 x i8]* @.str1, i32 0, i32 0), i32 %x) ; [#uses=0] - ret i32 %0 -} - -; Verify we don't turn this into an iprintf call -define void @f1(double %x) nounwind { -entry: -; CHECK: define void @f1 -; CHECK: @printf -; CHECK: } - %0 = tail call i32 (i8*, ...)* @printf(i8* getelementptr ([4 x i8]* @.str, i32 0, i32 0), double %x) nounwind ; [#uses=0] - ret void -} - ; Verify sprintf with no floating point arguments is transformed to siprintf define i32 @f2(i8* %p, i32 %x) nounwind { entry: @@ -66,6 +46,5 @@ entry: 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