Skip to content

Commit 45c4164

Browse files
committed
Only replace fwrite with fputc, if the return value is unused.
llvm-svn: 146411
1 parent 27a7489 commit 45c4164

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,8 @@ struct FWriteOpt : public LibCallOptimization {
12931293
return ConstantInt::get(CI->getType(), 0);
12941294

12951295
// If this is writing one byte, turn it into fputc.
1296-
if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1296+
// This optimisation is only valid, if the return value is unused.
1297+
if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
12971298
Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
12981299
EmitFPutC(Char, CI->getArgOperand(3), B, TD);
12991300
return ConstantInt::get(CI->getType(), 1);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; RUN: opt < %s -simplify-libcalls -S | FileCheck %s
2+
3+
%FILE = type { i32 }
4+
5+
@.str = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
6+
7+
define i64 @foo(%FILE* %f) {
8+
; CHECK: %retval = call i64 @fwrite
9+
%retval = call i64 @fwrite(i8* getelementptr inbounds ([1 x i8]* @.str, i64 0, i64 0), i64 1, i64 1, %FILE* %f)
10+
ret i64 %retval
11+
}
12+
13+
declare i64 @fwrite(i8*, i64, i64, %FILE *)

0 commit comments

Comments
 (0)