From fade1bf83850fb03482ef8f66862413cb702ca4b Mon Sep 17 00:00:00 2001 From: Sjoerd Meijer Date: Thu, 7 Jul 2016 13:56:23 +0000 Subject: [PATCH] Code size optimisation: don't rewrite fputs to fwrite when optimising for size because fwrite requires more arguments and thus extra MOVs are required. llvm-svn: 274753 --- lib/Transforms/Utils/SimplifyLibCalls.cpp | 2 ++ test/Transforms/InstCombine/fputs-opt-size.ll | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 test/Transforms/InstCombine/fputs-opt-size.ll diff --git a/lib/Transforms/Utils/SimplifyLibCalls.cpp b/lib/Transforms/Utils/SimplifyLibCalls.cpp index d0fb0d32b3a..9bba359ce71 100644 --- a/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -2056,6 +2056,8 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI) { case LibFunc::fwrite: return optimizeFWrite(CI, Builder); case LibFunc::fputs: + if (CI->getParent()->getParent()->optForSize()) + return nullptr; return optimizeFPuts(CI, Builder); case LibFunc::log: case LibFunc::log10: diff --git a/test/Transforms/InstCombine/fputs-opt-size.ll b/test/Transforms/InstCombine/fputs-opt-size.ll new file mode 100644 index 00000000000..ea8ef4203e9 --- /dev/null +++ b/test/Transforms/InstCombine/fputs-opt-size.ll @@ -0,0 +1,28 @@ +; When optimising for size, we don't want to rewrite fputs to fwrite +; because it requires more arguments and thus extra MOVs are required. +; +; RUN: opt < %s -instcombine -S | FileCheck %s + +%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*, i8*, i8*, i32, i32, [40 x i8] } +%struct._IO_marker = type { %struct._IO_marker*, %struct._IO_FILE*, i32 } + +@.str = private unnamed_addr constant [10 x i8] c"mylog.txt\00", align 1 +@.str.1 = private unnamed_addr constant [2 x i8] c"a\00", align 1 +@.str.2 = private unnamed_addr constant [27 x i8] c"Hello world this is a test\00", align 1 + +define i32 @main() local_unnamed_addr #0 { +entry: +; CHECK-LABEL: @main( +; CHECK-NOT: call i64 @fwrite +; CHECK: call i32 @fputs + + %call = tail call %struct._IO_FILE* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i32 0, i32 0)) #2 + %call1 = tail call i32 @fputs(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i32 0, i32 0), %struct._IO_FILE* %call) #2 + ret i32 0 +} + +declare noalias %struct._IO_FILE* @fopen(i8* nocapture readonly, i8* nocapture readonly) local_unnamed_addr #1 +declare i32 @fputs(i8* nocapture readonly, %struct._IO_FILE* nocapture) local_unnamed_addr #1 + +attributes #0 = { nounwind optsize } +attributes #1 = { nounwind optsize }