1
0
mirror of https://github.com/c64scene-ar/llvm-6502.git synced 2025-03-14 15:33:34 +00:00

Zap sitofp/fptoui pairs. In all cases when the sign difference

matters, the result is undefined anyway.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54396 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2008-08-06 05:13:06 +00:00
parent b30591ec64
commit 5af5f463e1
2 changed files with 43 additions and 17 deletions
lib/Transforms/Scalar
test/Transforms/InstCombine

@ -7750,27 +7750,41 @@ Instruction *InstCombiner::visitFPExt(CastInst &CI) {
} }
Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) { Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) {
// fptoui(uitofp(X)) --> X if the intermediate type has enough bits in its Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
// mantissa to accurately represent all values of X. For example, do not if (OpI == 0)
// do this with i64->float->i64. return commonCastTransforms(FI);
if (UIToFPInst *SrcI = dyn_cast<UIToFPInst>(FI.getOperand(0)))
if (SrcI->getOperand(0)->getType() == FI.getType() && // fptoui(uitofp(X)) --> X
(int)FI.getType()->getPrimitiveSizeInBits() < /*extra bit for sign */ // fptoui(sitofp(X)) --> X
SrcI->getType()->getFPMantissaWidth()) // This is safe if the intermediate type has enough bits in its mantissa to
return ReplaceInstUsesWith(FI, SrcI->getOperand(0)); // accurately represent all values of X. For example, do not do this with
// i64->float->i64. This is also safe for sitofp case, because any negative
// 'X' value would cause an undefined result for the fptoui.
if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
OpI->getOperand(0)->getType() == FI.getType() &&
(int)FI.getType()->getPrimitiveSizeInBits() < /*extra bit for sign */
OpI->getType()->getFPMantissaWidth())
return ReplaceInstUsesWith(FI, OpI->getOperand(0));
return commonCastTransforms(FI); return commonCastTransforms(FI);
} }
Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) { Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) {
// fptosi(sitofp(X)) --> X if the intermediate type has enough bits in its Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
// mantissa to accurately represent all values of X. For example, do not if (OpI == 0)
// do this with i64->float->i64. return commonCastTransforms(FI);
if (SIToFPInst *SrcI = dyn_cast<SIToFPInst>(FI.getOperand(0)))
if (SrcI->getOperand(0)->getType() == FI.getType() && // fptosi(sitofp(X)) --> X
(int)FI.getType()->getPrimitiveSizeInBits() <= // fptosi(uitofp(X)) --> X
SrcI->getType()->getFPMantissaWidth()) // This is safe if the intermediate type has enough bits in its mantissa to
return ReplaceInstUsesWith(FI, SrcI->getOperand(0)); // accurately represent all values of X. For example, do not do this with
// i64->float->i64. This is also safe for sitofp case, because any negative
// 'X' value would cause an undefined result for the fptoui.
if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
OpI->getOperand(0)->getType() == FI.getType() &&
(int)FI.getType()->getPrimitiveSizeInBits() <=
OpI->getType()->getFPMantissaWidth())
return ReplaceInstUsesWith(FI, OpI->getOperand(0));
return commonCastTransforms(FI); return commonCastTransforms(FI);
} }

@ -1,4 +1,4 @@
; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep sitofp ; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep itofp
define i1 @test1(i8 %A) { define i1 @test1(i8 %A) {
%B = sitofp i8 %A to double %B = sitofp i8 %A to double
@ -41,3 +41,15 @@ define i32 @test6(i32 %A) {
ret i32 %G ret i32 %G
} }
define i32 @test7(i32 %a) nounwind {
%b = sitofp i32 %a to double ; <double> [#uses=1]
%c = fptoui double %b to i32 ; <i32> [#uses=1]
ret i32 %c
}
define i32 @test8(i32 %a) nounwind {
%b = uitofp i32 %a to double ; <double> [#uses=1]
%c = fptosi double %b to i32 ; <i32> [#uses=1]
ret i32 %c
}