From 7bdaecb7f497fa82f13fb640a4bc1e6a5245ede8 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 6 Aug 2008 05:13:06 +0000 Subject: Zap sitofp/fptoui pairs. In all cases when the sign difference matters, the result is undefined anyway. llvm-svn: 54396 --- .../lib/Transforms/Scalar/InstructionCombining.cpp | 46 ++++++++++++++-------- 1 file changed, 30 insertions(+), 16 deletions(-) (limited to 'llvm/lib/Transforms') diff --git a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp index fbd0d200717..44083bc2d00 100644 --- a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp @@ -7750,27 +7750,41 @@ Instruction *InstCombiner::visitFPExt(CastInst &CI) { } Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) { - // fptoui(uitofp(X)) --> X if the intermediate type has enough bits in its - // mantissa to accurately represent all values of X. For example, do not - // do this with i64->float->i64. - if (UIToFPInst *SrcI = dyn_cast(FI.getOperand(0))) - if (SrcI->getOperand(0)->getType() == FI.getType() && - (int)FI.getType()->getPrimitiveSizeInBits() < /*extra bit for sign */ - SrcI->getType()->getFPMantissaWidth()) - return ReplaceInstUsesWith(FI, SrcI->getOperand(0)); + Instruction *OpI = dyn_cast(FI.getOperand(0)); + if (OpI == 0) + return commonCastTransforms(FI); + + // fptoui(uitofp(X)) --> X + // fptoui(sitofp(X)) --> X + // This is safe if the intermediate type has enough bits in its mantissa to + // 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(OpI) || isa(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); } Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) { - // fptosi(sitofp(X)) --> X if the intermediate type has enough bits in its - // mantissa to accurately represent all values of X. For example, do not - // do this with i64->float->i64. - if (SIToFPInst *SrcI = dyn_cast(FI.getOperand(0))) - if (SrcI->getOperand(0)->getType() == FI.getType() && - (int)FI.getType()->getPrimitiveSizeInBits() <= - SrcI->getType()->getFPMantissaWidth()) - return ReplaceInstUsesWith(FI, SrcI->getOperand(0)); + Instruction *OpI = dyn_cast(FI.getOperand(0)); + if (OpI == 0) + return commonCastTransforms(FI); + + // fptosi(sitofp(X)) --> X + // fptosi(uitofp(X)) --> X + // This is safe if the intermediate type has enough bits in its mantissa to + // 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(OpI) || isa(OpI)) && + OpI->getOperand(0)->getType() == FI.getType() && + (int)FI.getType()->getPrimitiveSizeInBits() <= + OpI->getType()->getFPMantissaWidth()) + return ReplaceInstUsesWith(FI, OpI->getOperand(0)); return commonCastTransforms(FI); } -- cgit v1.2.3