Skip to content

Commit 491ac8f

Browse files
committed
[LibCalls] Cast Char argument to 'int' before calling emitFPutC
The helpers in BuildLibCalls normally expect that the Value arguments already have the correct type (matching the lib call signature). And exception has been emitFPutC which casted the Char argument to 'int' using CreateIntCast. This patch moves the cast to the caller instead of doing it inside emitFPutC. I think it makes sense to make the BuildLibCall API:s a bit more consistent this way, despite the need to handle the int cast in two different places now. Differential Revision: https://reviews.llvm.org/D135066
1 parent aa1b64c commit 491ac8f

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

llvm/include/llvm/Transforms/Utils/BuildLibCalls.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ namespace llvm {
226226
/// Emit a call to the puts function. This assumes that Str is some pointer.
227227
Value *emitPutS(Value *Str, IRBuilderBase &B, const TargetLibraryInfo *TLI);
228228

229-
/// Emit a call to the fputc function. This assumes that Char can be casted to
230-
/// int (currently assuming int is i32), and File is a pointer to FILE.
229+
/// Emit a call to the fputc function. This assumes that Char is an 'int', and
230+
/// File is a pointer to FILE.
231231
Value *emitFPutC(Value *Char, Value *File, IRBuilderBase &B,
232232
const TargetLibraryInfo *TLI);
233233

llvm/lib/Transforms/Utils/BuildLibCalls.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,8 +1841,6 @@ Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilderBase &B,
18411841
IntTy, File->getType());
18421842
if (File->getType()->isPointerTy())
18431843
inferNonMandatoryLibFuncAttrs(M, FPutcName, *TLI);
1844-
Char = B.CreateIntCast(Char, IntTy, /*isSigned*/true,
1845-
"chari");
18461844
CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName);
18471845

18481846
if (const Function *Fn =

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,11 +3209,13 @@ Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI,
32093209

32103210
// Decode the second character of the format string.
32113211
if (FormatStr[1] == 'c') {
3212-
// fprintf(F, "%c", chr) --> fputc(chr, F)
3212+
// fprintf(F, "%c", chr) --> fputc((int)chr, F)
32133213
if (!CI->getArgOperand(2)->getType()->isIntegerTy())
32143214
return nullptr;
3215-
return copyFlags(
3216-
*CI, emitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI));
3215+
Type *IntTy = B.getIntNTy(TLI->getIntSize());
3216+
Value *V = B.CreateIntCast(CI->getArgOperand(2), IntTy, /*isSigned*/ true,
3217+
"chari");
3218+
return copyFlags(*CI, emitFPutC(V, CI->getArgOperand(0), B, TLI));
32173219
}
32183220

32193221
if (FormatStr[1] == 's') {
@@ -3280,7 +3282,9 @@ Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilderBase &B) {
32803282
if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
32813283
Value *Char = B.CreateLoad(B.getInt8Ty(),
32823284
castToCStr(CI->getArgOperand(0), B), "char");
3283-
Value *NewCI = emitFPutC(Char, CI->getArgOperand(3), B, TLI);
3285+
Type *IntTy = B.getIntNTy(TLI->getIntSize());
3286+
Value *Cast = B.CreateIntCast(Char, IntTy, /*isSigned*/ true, "chari");
3287+
Value *NewCI = emitFPutC(Cast, CI->getArgOperand(3), B, TLI);
32843288
return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
32853289
}
32863290
}

0 commit comments

Comments
 (0)