Only replace fwrite with fputc, if the return value is unused.
[oota-llvm.git] / lib / Transforms / Scalar / SimplifyLibCalls.cpp
index 075adc01873dabac6ad16f488562804a04cc3773..f3184ecc860103d75177daae813a23f4547def7f 100644 (file)
@@ -1293,7 +1293,8 @@ struct FWriteOpt : public LibCallOptimization {
       return ConstantInt::get(CI->getType(), 0);
 
     // If this is writing one byte, turn it into fputc.
-    if (Bytes == 1) {  // fwrite(S,1,1,F) -> fputc(S[0],F)
+    // This optimisation is only valid, if the return value is unused.
+    if (Bytes == 1 && CI->use_empty()) {  // fwrite(S,1,1,F) -> fputc(S[0],F)
       Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
       EmitFPutC(Char, CI->getArgOperand(3), B, TD);
       return ConstantInt::get(CI->getType(), 1);