[llvm-commits] CVS: llvm/lib/VMCore/ConstantFolding.cpp
Chris Lattner
clattner at apple.com
Mon Dec 18 22:45:29 PST 2006
On Dec 18, 2006, at 7:16 PM, Reid Spencer wrote:
> Rewrite ConstantFoldCastInstruction so that it doesn't use any of the
> ConstRules. Remove the casting rules from ConstRules and
> subclasses. This
> cleans up ConstantFolding significantly. Passes all tests.
Nice!
> case Instruction::UIToFP: {
> + // First, extract the unsigned integer value
> + uint64_t Val;
> + if (isa<ConstantInt>(V))
> + Val = cast<ConstantIntegral>(V)->getZExtValue();
> + else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
> + Val = CB->getValue() ? 1 : 0;
ConstantBool is ConstantIntegral. You should be able to do:
> + if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V))
> + Val = CI->getZExtValue();
to handle both cases.
> + // Now generate the equivalent floating point value
> + double dVal = (double) Val;
> + return ConstantFP::get(DestTy, dVal);
> + }
> + case Instruction::SIToFP: {
> + // First, extract the signed integer value
> + int64_t Val;
> + if (isa<ConstantInt>(V))
> + Val = cast<ConstantIntegral>(V)->getSExtValue();
> + else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
> + Val = CB->getValue() ? -1 : 0;
Likewise.
> + // Now generate the equivalent floating point value
> + double dVal = (double) Val;
> + return ConstantFP::get(DestTy, dVal);
> + }
> case Instruction::ZExt:
> + // Handle trunc directly here if it is a ConstantIntegral.
> if (isa<ConstantInt>(V))
> + return ConstantInt::get(DestTy, cast<ConstantInt>(V)-
> >getZExtValue());
> + else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
> + return ConstantInt::get(DestTy, CB->getValue() ? 1 : 0);
Likewise, also, please correct the comment.
> + return 0;
> case Instruction::SExt:
> // A SExt always produces a signed value so we need to cast
> the value
> // now before we try to cast it to the destiniation type.
> if (isa<ConstantInt>(V))
> + return ConstantInt::get(DestTy, cast<ConstantInt>(V)-
> >getSExtValue());
> else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
> + return ConstantInt::get(DestTy, CB->getValue() ? -1 : 0);
Likewise.
> + return 0;
> case Instruction::Trunc:
> // We just handle trunc directly here. The code below doesn't
> work for
> // trunc to bool.
> @@ -896,7 +743,8 @@
> return ConstantIntegral::get(DestTy, CI->getZExtValue());
> return 0;
> case Instruction::BitCast:
> - if (SrcTy == DestTy) return (Constant*)V; // no-op cast
> + if (SrcTy == DestTy)
> + return (Constant*)V; // no-op cast
Does this fold (int)4U ?
-Chris
More information about the llvm-commits
mailing list