From 16ff304e2ce087e99c283bcb2d3f8be764b5a281 Mon Sep 17 00:00:00 2001 From: Dale Johannesen Date: Wed, 4 Apr 2007 19:16:42 +0000 Subject: [PATCH] Prevent transformConstExprCastCall from generating conversions that assert elsewhere. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35668 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/InstructionCombining.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index d217fca8890..d9b15de1afb 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -7469,6 +7469,7 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { const Type *ParamTy = FT->getParamType(i); const Type *ActTy = (*AI)->getType(); ConstantInt *c = dyn_cast(*AI); + //Some conversions are safe even if we do not have a body. //Either we can cast directly, or we can upconvert the argument bool isConvertible = ActTy == ParamTy || (isa(ParamTy) && isa(ActTy)) || @@ -7477,6 +7478,40 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits() && c->getValue().isStrictlyPositive()); if (Callee->isDeclaration() && !isConvertible) return false; + + // Most other conversions can be done if we have a body, even if these + // lose information, e.g. int->short. + // Some conversions cannot be done at all, e.g. float to pointer. + // Logic here parallels CastInst::getCastOpcode (the design there + // requires legality checks like this be done before calling it). + if (ParamTy->isInteger()) { + if (const VectorType *VActTy = dyn_cast(ActTy)) { + if (VActTy->getBitWidth() != ParamTy->getPrimitiveSizeInBits()) + return false; + } + if (!ActTy->isInteger() && !ActTy->isFloatingPoint() && + !isa(ActTy)) + return false; + } else if (ParamTy->isFloatingPoint()) { + if (const VectorType *VActTy = dyn_cast(ActTy)) { + if (VActTy->getBitWidth() != ParamTy->getPrimitiveSizeInBits()) + return false; + } + if (!ActTy->isInteger() && !ActTy->isFloatingPoint()) + return false; + } else if (const VectorType *VParamTy = dyn_cast(ParamTy)) { + if (const VectorType *VActTy = dyn_cast(ActTy)) { + if (VActTy->getBitWidth() != VParamTy->getBitWidth()) + return false; + } + if (VParamTy->getBitWidth() != ActTy->getPrimitiveSizeInBits()) + return false; + } else if (isa(ParamTy)) { + if (!ActTy->isInteger() && !isa(ActTy)) + return false; + } else { + return false; + } } if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && -- 2.34.1